From 30e7d4f2ecb948a172fa44f25d18cfa2471c0f58 Mon Sep 17 00:00:00 2001 From: Yu Yi <58987452+yuyi-sl@users.noreply.github.com> Date: Thu, 16 May 2024 23:12:46 +0200 Subject: [PATCH] Rdc network throttling test example (#182) * Add example for Network Throttling * small clean up * add network throttling to list of examples --------- Co-authored-by: Eyal Yovel --- .../appium-app/appium-app-examples/README.md | 1 + .../NetworkThrottlingIosRDCTest.java | 148 ++++++++++++++++++ .../com/examples/network_throttling/README.md | 59 +++++++ 3 files changed, 208 insertions(+) create mode 100644 appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/NetworkThrottlingIosRDCTest.java create mode 100644 appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/README.md diff --git a/appium/appium-app/appium-app-examples/README.md b/appium/appium-app/appium-app-examples/README.md index 5a7b3593..e575cf1a 100644 --- a/appium/appium-app/appium-app-examples/README.md +++ b/appium/appium-app/appium-app-examples/README.md @@ -5,6 +5,7 @@ This folder contains Appium examples * [Simple examples to get you started with your native appium testing](./src/test/java/com/examples/simple_example) * [Using biometric login on Sauce Labs](./src/test/java/com/examples/biometric_login) * [Using image injection on Sauce Labs](./src/test/java/com/examples/image_injection) +* [Using network throttling on Sauce Labs](./src/test/java/com/examples/network_throttling) * [Using gestures](./src/test/java/com/examples/gestures) * [Upload and download file](./src/test/java/com/examples/up_download_file) * [Using deep link](./src/test/java/com/examples/deep_link) diff --git a/appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/NetworkThrottlingIosRDCTest.java b/appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/NetworkThrottlingIosRDCTest.java new file mode 100644 index 00000000..cf6f8843 --- /dev/null +++ b/appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/NetworkThrottlingIosRDCTest.java @@ -0,0 +1,148 @@ +package com.examples.network_throttling; + +import com.google.common.collect.ImmutableMap; +import com.helpers.SauceAppiumTestWatcher; +import io.appium.java_client.ios.IOSDriver; +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.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 NetworkThrottlingIosRDCTest { + + private final static By moreTabButton = By.xpath("//XCUIElementTypeButton[@name=\"More-tab-item\"]"); + private final static By webviewButton = By.xpath("//XCUIElementTypeButton[@name=\"Webview-menu-item\"]"); + private final static By urlInput = By.xpath("//XCUIElementTypeTextField[@value=\"https://www.website.com\"]"); + private final static By goButton = By.xpath("//XCUIElementTypeButton[@name=\"Go To Site\"]"); + private final static By moreInfoLink = By.xpath("//XCUIElementTypeLink[@name=\"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 IOSDriver driver; + private WebDriverWait wait; + + @Before + public void setUp() { + MutableCapabilities capabilities = new MutableCapabilities(); + MutableCapabilities sauceOptions = new MutableCapabilities(); + + capabilities.setCapability("platformName", "ios"); + capabilities.setCapability("deviceName", ".*"); + + // Sauce capabilities + sauceOptions.setCapability("username", System.getenv("SAUCE_USERNAME")); + sauceOptions.setCapability("accessKey", System.getenv("SAUCE_ACCESS_KEY")); + + String appName = "SauceLabs-Demo-App.ipa"; + + 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", "Safari"); + 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 IOSDriver(getAppiumUrl(), capabilities); + } catch (Exception e){ + System.out.println("Error to create the iOS 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(moreTabButton)).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(30)); + wait.until(ExpectedConditions.presenceOfElementLocated(moreInfoLink)); + } + + private URL getAppiumUrl() throws MalformedURLException { + switch (region) { + case "us": + return new URL(SAUCE_US_URL); + case "eu": + default: + return new URL(SAUCE_EU_URL); + } + } +} diff --git a/appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/README.md b/appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/README.md new file mode 100644 index 00000000..5578c836 --- /dev/null +++ b/appium/appium-app/appium-app-examples/src/test/java/com/examples/network_throttling/README.md @@ -0,0 +1,59 @@ +# Using Network Throttling on Sauce Labs Real Devices +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) + +## Important information +### Environment variables for Sauce Labs +The examples in this repository use environment variables, make sure you've added the following + + # For Sauce Labs Emulators/Simulators/Real devices + export SAUCE_USERNAME=******** + export SAUCE_ACCESS_KEY=******* + +### Demo app(s) +The Android demo app that has been used for all these tests can be found [here](https://github.com/saucelabs/my-demo-app-android/releases). +The iOS demo app that has been used for all these tests can be found [here](https://github.com/saucelabs/my-demo-app-ios/releases). + +> The advice is to download the files to an `apps` folder in the root of this folder. + +Make sure that when you downloaded the files from the releases page, that you rename the apps to the following: + +- `mda-{#.#.#}.apk` => `SauceLabs-Demo-App.apk` + +**If you don't do that then the scripts can't find the apps!** + +### Upload apps to Sauce Storage +If you want to use Android emulators, Android real devices, iOS simulators or iOS real devices in the Sauce Labs platform, you need to upload +the apps to the Sauce Storage. + +#### Manual upload +Execute the following steps to manually upload the apps: +- Login to the Sauce Labs platform +- Go to **LIVE** > **Mobile App** +- Click on **App Upload** and OR select the folder, OR drag the apps to the screen to upload them + +#### Automated upload +You can find a script to upload them to, OR the US, OR EU DC in [this](../../helpers/push_apps_to_storage.sh)-file. You can push the files to the +storage by doing the following from the folder `appium-app-examples`: + + cd src/test/java/com/helpers/ + push_apps_to_storage.sh + +## Run tests on Sauce Labs iOS real devices +If you want to run the tests on iOS Sauce Labs Real Devices then you can run the iOS test with + + // If using the US DC + mvn clean test -Dtest=NetworkThrottlingIosRDCTest -Dregion=us + + // If using the EU DC + mvn clean test -Dtest=NetworkThrottlingIosRDCTest -Dregion=eu + +The tests, which can be found [here](NetworkThrottlingIosRDCTest.java), will be executed on: + +- Any available iPhone or iPad + +> 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 +> \ No newline at end of file