Skip to content

Commit

Permalink
CR-1785 simulate network configuration for IOS device & BrowserStack …
Browse files Browse the repository at this point in the history
…Lab.
  • Loading branch information
ManojTestsigma committed Jan 7, 2025
1 parent 56aa41e commit 2896378
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
105 changes: 105 additions & 0 deletions network_configuration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>network_configuration</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<testsigma.sdk.version>1.2.18_cloud</testsigma.sdk.version>
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
<lombok.version>1.18.20</lombok.version>

</properties>

<dependencies>
<dependency>
<groupId>com.testsigma</groupId>
<artifactId>testsigma-java-sdk</artifactId>
<version>${testsigma.sdk.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.14.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>

<!-- Additional Dependencies-->
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>

</dependencies>
<build>
<finalName>network_configuration</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testsigma-sdk.api.key=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjMyMmM2Ni04NWYzLWIyN2UtN2FiOS0zM2U2M2Q4OWM1MGIiLCJ1bmlxdWVJZCI6IjQxMjgiLCJpZGVudGl0eUFjY291bnRVVUlkIjoiMzUifQ.t8f9EYj_PaSXMOhJb4_saHyek-afaZxuqMLbGl-hvndNZ8XVisZQSDKavKls_P8CmHNGs_QQLtBd7zm1hmlKfA

0 comments on commit 2896378

Please sign in to comment.