-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RDC Android example for network throttling (#183)
* add RDC Android example for network throttling * rethrow exception * add appium prefix to caps
- Loading branch information
Showing
3 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...amples/src/test/java/com/examples/network_throttling/NetworkThrottlingAndroidRDCTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
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.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() throws MalformedURLException { | ||
MutableCapabilities capabilities = new MutableCapabilities(); | ||
MutableCapabilities sauceOptions = new MutableCapabilities(); | ||
|
||
capabilities.setCapability("platformName", "android"); | ||
capabilities.setCapability("appium:deviceName", ".*"); | ||
capabilities.setCapability("appium: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()); | ||
throw e; | ||
} | ||
|
||
// 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)); | ||
} | ||
|
||
private URL getAppiumUrl() throws MalformedURLException { | ||
switch (region) { | ||
case "us": | ||
return new URL(SAUCE_US_URL); | ||
case "eu": | ||
default: | ||
return new URL(SAUCE_EU_URL); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters