Using Device Cloud to run tests on Real Devices

Appium supports testing on mobile simulators, emulators, and real devices. To set up a mobile-testing lab with real devices requires capital investment as well as the maintenance of devices and infrastructure. Mobile phone manufacturers release new phone models and operating system updates almost every day and your application has to be compatible with the new launches.

To respond to these changes faster and keep the investment to a minimum, we can use Cloud-based mobile-testing labs. There are a number of vendors, such as Amazon Web Services, BrowserStack, and Sauce Labs, that provide cloud-based real-mobile-device labs to execute tests without requiring any upfront investment in real devices. You pay only for the amount of time used for testing. These vendors also allow you to run automated tests using Appium in their Device clouds.

In this section, we will explore BrowserStack to run tests on its real-device Cloud:

  1. You need to have a BrowserStack account with a subscription to the Automate feature. You can sign up for a free trial account at https://www.browserstack.com/.
  2. We need to get the desired capabilities from BrowserStack based on device combination. BrowserStack provides capability-suggestions based on the selected combination of devices and platforms. Go to https://www.browserstack.com/automate/java and select an OS and Device :

  1. Based on your selection, BrowserStack will auto-generate code using your username and access key:

We will not use the suggested code in step 3, and instead change our test as shown in the following code. Remember, you need to use the username and access key shown in the auto-generated code:

public class SearchTest {

private WebDriver driver;

@BeforeTest
public void setUp() throws Exception {

String USERNAME = "username";
String AUTOMATE_KEY = "access_key";
String URL = "https://" + USERNAME + ":"
+ AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

// Set the desired capabilities for iPhone X
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", "iPhone");
caps.setCapability("device", "iPhone X");
caps.setCapability("realMobile", "true");
caps.setCapability("os_version", "11.0");

driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("http://demo-store.seleniumacademy.com/")
;
}

@Test
public void searchProduct() {

WebElement lookingGlassIcon =
driver.findElement(By
.cssSelector("a.skip-search span.icon"));
lookingGlassIcon.click();

// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Phones");

WebElement searchButton =
driver.findElement(By.className("search-button"));

searchButton.click();

List<WebElement> searchItems = new WebDriverWait(driver, 30)
.until(ExpectedConditions
.presenceOfAllElementsLocatedBy(By
.cssSelector("h2.product-name a")));

assertThat(searchItems.size())
.isEqualTo(3);
}

@AfterTest
public void tearDown() throws Exception {
// Close the browser
driver.quit();
}
}

Execute the test from your IDE, and it will run in the BrowserStack cloud. You can monitor the tests in the BrowserStack dashboard where it will show you capabilities used, the status of each step, console logs, network logs, Appium logs, and a video of the execution:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.253.152