Skip to content

Commit

Permalink
add RDC Android example for network throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi-sl committed Jun 5, 2024
1 parent 30e7d4f commit 1032038
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.examples.network_throttling;

import com.google.common.collect.ImmutableMap;
import com.helpers.SauceAppiumTestWatcher;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.openqa.selenium.By;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;

import static com.helpers.Constants.SAUCE_EU_URL;
import static com.helpers.Constants.SAUCE_US_URL;
import static com.helpers.Constants.region;

public class NetworkThrottlingAndroidRDCTest {

private final static By viewMenuButton = new AppiumBy.ByAccessibilityId("View menu");
private final static By webviewButton = By.xpath("//android.widget.TextView[@resource-id=\"com.saucelabs.mydemoapp.android:id/itemTV\" and @text=\"WebView\"]");
private final static By urlInput = By.id("com.saucelabs.mydemoapp.android:id/urlET");
private final static By goButton = new AppiumBy.ByAccessibilityId("Tap to view content of given url");
private final static By moreInfoButton = new AppiumBy.ByAccessibilityId("Show more info");

@Rule
public TestName name = new TestName();

// This rule allows us to set test status with Junit
@Rule
public SauceAppiumTestWatcher resultReportingTestWatcher = new SauceAppiumTestWatcher();

private AndroidDriver driver;
private WebDriverWait wait;

@Before
public void setUp() {
MutableCapabilities capabilities = new MutableCapabilities();
MutableCapabilities sauceOptions = new MutableCapabilities();

capabilities.setCapability("platformName", "android");
capabilities.setCapability("deviceName", ".*");
capabilities.setCapability("automationName", "uiautomator2");

// Sauce capabilities
sauceOptions.setCapability("username", System.getenv("SAUCE_USERNAME"));
sauceOptions.setCapability("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
sauceOptions.setCapability("appiumVersion", "latest");

String appName = "SauceLabs-Demo-App.apk";

String testName = name.getMethodName();
switch (testName) {
case "regularNetworkSpeedTest":
System.out.println("Running regular network speed test");
capabilities.setCapability("appium:app", "storage:filename=" + appName);
sauceOptions.setCapability("name", "Regular network speed test");
break;
case "throttledNetworkProfileTest":
System.out.println("Running throttled 2G Slow test");
capabilities.setCapability("appium:app", "storage:filename=" + appName);
sauceOptions.setCapability("name", "Throttled 2G Slow test");
// Set a predefined network profile
sauceOptions.setCapability("networkProfile", "2G");
break;
case "throttledCustomNetworkConditionsTest":
System.out.println("Running throttled custom network conditions test");
capabilities.setCapability("appium:app", "storage:filename=" + appName);
sauceOptions.setCapability("name", "Throttled custom network conditions test");
// Set custom network conditions
sauceOptions.setCapability("networkConditions", ImmutableMap.of(
"downloadSpeed", 5000,
"uploadSpeed", 3000,
"latency", 200,
"loss", 5
));
break;
case "throttledNetworkSpeedWebTest":
System.out.println("Running throttled 2G Slow web test");
capabilities.setCapability("browserName", "Chrome");
sauceOptions.setCapability("name", "Throttled 2G Slow web test");
// Set a predefined network profile
sauceOptions.setCapability("networkProfile", "2G");
break;
default:
System.out.println("No test configuration found for " + testName);
break;
}

capabilities.setCapability("sauce:options", sauceOptions);

try {
driver = new AndroidDriver(getAppiumUrl(), capabilities);
} catch (Exception e){
System.out.println("Error to create the Android Driver: " + e.getMessage());
}

// Setting the driver so that we can report results
resultReportingTestWatcher.setDriver(driver);
}

@Test
public void regularNetworkSpeedTest() {
testNetworkConditions();
}

@Test
public void throttledNetworkProfileTest() {
testNetworkConditions();
}

@Test
public void throttledCustomNetworkConditionsTest() {
testNetworkConditions();
}

@Test
public void throttledNetworkSpeedWebTest() {
driver.get("https://www.fast.com");
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("show-more-details-link")));
}

private void testNetworkConditions() {
wait = new WebDriverWait(driver, Duration.ofSeconds(5));

wait.until(ExpectedConditions.presenceOfElementLocated(viewMenuButton)).click();
wait.until(ExpectedConditions.presenceOfElementLocated(webviewButton)).click();
wait.until(ExpectedConditions.presenceOfElementLocated(urlInput)).sendKeys("fast.com");
wait.until(ExpectedConditions.presenceOfElementLocated(goButton)).click();

wait = new WebDriverWait(driver, Duration.ofSeconds(45));
wait.until(ExpectedConditions.visibilityOfElementLocated(moreInfoButton));
driver.getScreenshotAs(OutputType.BASE64);
}

private URL getAppiumUrl() throws MalformedURLException {
switch (region) {
case "us":
return new URL(SAUCE_US_URL);
case "eu":
default:
return new URL(SAUCE_EU_URL);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void testNetworkConditions() {
wait.until(ExpectedConditions.presenceOfElementLocated(goButton)).click();

wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.presenceOfElementLocated(moreInfoLink));
wait.until(ExpectedConditions.visibilityOfElementLocated(moreInfoLink));
}

private URL getAppiumUrl() throws MalformedURLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This folder contains examples for using Network Throttling on real devices for:

- [iOS real devices on the Sauce Labs Cloud](#run-tests-on-sauce-labs-ios-real-devices)
- [Android real devices on the Sauce Labs Cloud](#run-tests-on-sauce-labs-android-real-devices)

## Important information
### Environment variables for Sauce Labs
Expand Down Expand Up @@ -56,4 +57,21 @@ The tests, which can be found [here](NetworkThrottlingIosRDCTest.java), will be
> The devices use *dynamic* allocation, meaning they will try to find an available device that matches a regular
expression.
> NOTE: Make sure you are in the folder `appium-app-examples` when you execute this command
>

## Run tests on Sauce Labs Android real devices
If you want to run the tests on Android Sauce Labs Real Devices then you can run the Android test with

// If using the US DC
mvn clean test -Dtest=NetworkThrottlingAndroidRDCTest -Dregion=us

// If using the EU DC
mvn clean test -Dtest=NetworkThrottlingAndroidRDCTest -Dregion=eu

The tests, which can be found [here](NetworkThrottlingAndroidRDCTest.java), will be executed on:

- Any available Android device

> The devices use *dynamic* allocation, meaning they will try to find an available device that matches a regular
expression.
> NOTE: Make sure you are in the folder `appium-app-examples` when you execute this command

0 comments on commit 1032038

Please sign in to comment.