Thursday 31 May 2018

Web App Automation using Appium

In the Mobile world, we see multiple types of apps namely Native App, Hybrid App & the browser based application, Web App. Also, we see multiple types of operating systems like Android, iOS, Windows, Firefox OS etc.

Automated testing of these many variation of apps, is very tricky & difficult task. Not all of them are supported by single automation tool. Having said that, Appium which is built over Selenium webdriver supports most of the OS & apps with different configurations.

In this article, you would know a way to automate the mobile web app using RemoteWebdriver.

Prerequisites:

  1. Android SDK is downloaded and installed. (ANDROID_HOME env. variable should be set)
  2. Either Emulator or  Real device (USB debugging enabled) is available.
  3. Appium desktop app or non GUI package is installed. 

Steps:

  1. Create a java project in eclipse 'AppiumMobileTesting'.
  2. Create a class 'TestWebApp' to add the automation code.
  3. Open the web application in chrome mobile browser & identify the required locators.
    OR
    One of the easiest way to identify the locators for web app is, opening the website in the desktop chrome browser in emulation mode and capturing the locators by usual process. Emulate the browser for required device / view port as shown below to capture the locators properly.
                          
  4. Add the below code snippet to the 'TestWebApp' class which perform the below steps.
    • Open the chrome browser in the attached device based on the capabilities provided.
    • Open https://www.google.com website.
    • Perform the search the operation by providing search text.

public static void main(String[] args) {

 //Set the Desired Capabilities
 DesiredCapabilities caps = new DesiredCapabilities();
 caps.setCapability("deviceName", "MyDevice");
 caps.setCapability("udid", "G3WGLMA6217835");
 caps.setCapability("platformName", "Android");
 caps.setCapability("platformVersion", "6.0");
 caps.setCapability("browserName", "Chrome");
 caps.setCapability("noReset", true);

 //Set ChromeDriver location
 System.setProperty("webdriver.chrome.driver","C:\\Drv\\chromedriver.exe");

 WebDriver driver = null;
 try {
    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
    //Open URL in Chrome Browser
    driver.get("https://www.google.com");
    driver.findElement(By.name("q")).sendKeys("test");
    driver.findElement(By.xpath("//button[@aria-label='Google Search']")).click();
    Thread.sleep(5000);
    driver.quit();
   } catch (Exception e) {
    System.out.println(e.getMessage());
   }
}

   5. Connect the device through USB or open the emulator.
      6. Start the Appium server using '127.0.0.1' & '4723' as host & port accordingly.
      7. Execute the above script using eclipse.

That's it! You are done. Now you can watch execution on the device.




No comments:

Post a Comment