From 2896378048f15c9391ad126cf8cfe89ccc8bd0e5 Mon Sep 17 00:00:00 2001 From: Manoj Date: Tue, 7 Jan 2025 12:18:31 +0530 Subject: [PATCH] CR-1785 simulate network configuration for IOS device & BrowserStack Lab. --- network_configuration/pom.xml | 105 ++++++++++++++++++ .../addons/ios/SwitchToNetworkProfile.java | 96 ++++++++++++++++ .../main/resources/testsigma-sdk.properties | 1 + 3 files changed, 202 insertions(+) create mode 100644 network_configuration/pom.xml create mode 100644 network_configuration/src/main/java/com/testsigma/addons/ios/SwitchToNetworkProfile.java create mode 100644 network_configuration/src/main/resources/testsigma-sdk.properties diff --git a/network_configuration/pom.xml b/network_configuration/pom.xml new file mode 100644 index 00000000..382e1b1b --- /dev/null +++ b/network_configuration/pom.xml @@ -0,0 +1,105 @@ + + + 4.0.0 + com.testsigma.addons + network_configuration + 1.0.0 + jar + + + UTF-8 + 11 + 11 + 1.2.18_cloud + 5.8.0-M1 + 1.0.0 + 3.2.1 + 1.18.20 + + + + + + com.testsigma + testsigma-java-sdk + ${testsigma.sdk.version} + + + org.projectlombok + lombok + ${lombok.version} + true + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.testng + testng + 6.14.3 + + + + org.seleniumhq.selenium + selenium-java + 4.14.1 + + + + io.appium + java-client + 9.0.0 + + + com.fasterxml.jackson.core + jackson-annotations + 2.13.0 + + + + + + org.json + json + 20230227 + + + + + network_configuration + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven.source.plugin.version} + + + attach-sources + + jar + + + + + + + diff --git a/network_configuration/src/main/java/com/testsigma/addons/ios/SwitchToNetworkProfile.java b/network_configuration/src/main/java/com/testsigma/addons/ios/SwitchToNetworkProfile.java new file mode 100644 index 00000000..c0912bd3 --- /dev/null +++ b/network_configuration/src/main/java/com/testsigma/addons/ios/SwitchToNetworkProfile.java @@ -0,0 +1,96 @@ +package com.testsigma.addons.ios; + +import com.testsigma.sdk.ApplicationType; +import com.testsigma.sdk.IOSAction; +import com.testsigma.sdk.annotation.Action; +import com.testsigma.sdk.annotation.TestData; +import io.appium.java_client.ios.IOSDriver; +import lombok.Data; +import org.json.JSONObject; +import org.openqa.selenium.NoSuchElementException; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + + +@Data +@Action(actionText = "switch to network configuration profile-name", + description = "switches the network type to no-network/3G/4G & default resets the initial network configuration", + applicationType = ApplicationType.IOS, + useCustomScreenshot = false) +public class SwitchToNetworkProfile extends IOSAction { + @TestData(reference = "profile-name", allowedValues = {"no-network", "default", "2G", "3G", "4G"}) + com.testsigma.sdk.TestData profileName; + + @Override + protected com.testsigma.sdk.Result execute() throws NoSuchElementException { + com.testsigma.sdk.Result result = com.testsigma.sdk.Result.SUCCESS; + logger.info("Initiating execution to simulate network configuration"); + + final String successMessage = "Successfully switched to network-configuration : " + profileName.getValue().toString(); + final String errorMessage = "Failed to switch to network configuration"; + IOSDriver iosDriver = (IOSDriver) this.driver; + String sessionId = iosDriver.getSessionId().toString().toLowerCase(); + + String apiUrl = "https://api-cloud.browserstack.com/app-automate/sessions/" + sessionId + "/update_network.json"; + + try { + HttpClient client = HttpClient.newHttpClient(); + String modifiedProfileName = getBrowserStackProfileName(profileName.getValue().toString()); + + // Prepare the request body + JSONObject requestBody = new JSONObject(); + requestBody.put("networkProfile", modifiedProfileName); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(apiUrl)) + .header("Content-Type", "application/json") + .header("Authorization", "Basic cnVrbWFuZ2FkYTE6UHpwelNGRUdOUVVhV1hwek5vazU=") + .PUT(HttpRequest.BodyPublishers.ofString(requestBody.toString())) + .build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + int statusCode = response.statusCode(); + if (statusCode == 200) { + logger.info("response code: " + statusCode); + } else { + logger.warn("Failed to switch to network profile. Response Status Code: " + statusCode + ", Response Body: " + response.body()); + setErrorMessage("Failed to switch to network configuration " + modifiedProfileName +" Response Status Code: " + statusCode ); + result = com.testsigma.sdk.Result.FAILED; + } + } catch (IOException | InterruptedException e) { + logger.warn(errorMessage + e.getMessage()); + setErrorMessage(errorMessage + e.getMessage()); + result = com.testsigma.sdk.Result.FAILED; + } + + setSuccessMessage(successMessage + profileName.getValue().toString()); + return result; + } + + private String getBrowserStackProfileName(String modifiedProfileName) { + switch (modifiedProfileName) { + case "no-network": + modifiedProfileName = "no-network"; + break; + case "2g": + modifiedProfileName = "2g-gprs-good"; + break; + case "3g": + modifiedProfileName = "3g-umts-good"; + break; + case "4g": + modifiedProfileName = "4g-umts-good"; + break; + case "default": + modifiedProfileName = "reset"; + break; + } + return modifiedProfileName; + } + +} \ No newline at end of file diff --git a/network_configuration/src/main/resources/testsigma-sdk.properties b/network_configuration/src/main/resources/testsigma-sdk.properties new file mode 100644 index 00000000..aef712af --- /dev/null +++ b/network_configuration/src/main/resources/testsigma-sdk.properties @@ -0,0 +1 @@ +testsigma-sdk.api.key=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjMyMmM2Ni04NWYzLWIyN2UtN2FiOS0zM2U2M2Q4OWM1MGIiLCJ1bmlxdWVJZCI6IjQxMjgiLCJpZGVudGl0eUFjY291bnRVVUlkIjoiMzUifQ.t8f9EYj_PaSXMOhJb4_saHyek-afaZxuqMLbGl-hvndNZ8XVisZQSDKavKls_P8CmHNGs_QQLtBd7zm1hmlKfA \ No newline at end of file