Working with web apps

Web apps can be run on any device or platform. The only requirement for running them is a web browser and an Internet connection; the best part is that you don't need to install the app on the device. Web apps are generally designed with cross-browser compatibility.

Web apps on Android

We will take the example of the Gmail login page. In this section, we are going to take a look at how to load the Chrome browser on a real device and then type in invalid credentials and click on the Sign in button that is shown in the following screenshot:

Web apps on Android

Let's start with the following steps to automate the web apps:

  1. Set the desired capabilities to launch the Chrome browser:
    caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
  2. Now, we need to navigate to https://www.gmail.com using the driver.get("https://www.gmail.com"); command.
  3. We need to find the username box element (we can take a reference from Chapter 4, Finding Elements with Different Locators, to find an element); in this section, we are going to find an element by name:
    WebElement username=driver.findElement(By.name("Email"));
  4. Now, we need type into the username box:
    username.sendKeys("test");
  5. We need to find the password box element; in this section, we are going to find an element by name:
    WebElement password=driver.findElement(By.name("Passwd"));
  6. Now, we need to type into the password box element:
    password.sendKeys("test");
  7. We need to find the Sign in button; in this section, we are going to find an element by name:
    WebElement signIn=driver.findElement(By.name("signIn"));
  8. Now, we need to perform a click on it:
    signIn.click();
  9. Run your script using TestNG; this is how it should look:
      public class TestAppIication {
      AndroidDriver driver;
     @BeforeClass
      public void setUp() throws MalformedURLException {
      DesiredCapabilities caps = new DesiredCapabilities();
      caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
      caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
      caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
      caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");
      driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
     @Test
      public void testExample(){
      driver.get("https://www.gmail.com");
      WebElement username=driver.findElement(By.name("Email"));
      username.sendKeys("test");
      WebElement password=driver.findElement(By.name("Passwd"));
      password.sendKeys("test");
      WebElement signIn=driver.findElement(By.name("signIn"));
      signIn.click(); }
     @AfterClass
      public void tearDown(){
      driver.quit();
    }
    }

Web apps on iOS

We are going to take an example of the Google search page. In this section, we are going to take a look at how to load the browser on a real iOS device and then type data in the Google search box shown in the following screenshot:

Web apps on iOS

Let's start with the following steps to automate web apps:

  1. Update the desired capabilities in the setup() method to launch the Safari browser:
    caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
  2. Now, we need to navigate to https://www.google.com using the driver.get("https://www.google.com"); command.
  3. We need to find the searchBox element; in this section, we are going to find an element by name:
    WebElement searchBox=driver.findElement(By.name("q"));
  4. Now, we need to type into the search box:
    searchBox.sendKeys("Appium for mobile automation");
  5. Before running the test script, we need to start the proxy using the following command:
    ios_webkit_debug_proxy -c 2e5n6f615z66e98c1d07d22ee09658130d345443:27753 –d

    Replace 2e5n6f615z66e98c1d07d22ee09658130d345443 with the attached device's UDID and make sure that the port is set to 27753.

  6. Make sure that the Web Inspector is turned ON on a real device (Settings | Safari | Advanced) and the SafariLauncher app is installed:
  7. Run your script using TestNG; this is how it should look:
     public class TestAppIication {
     IOSDriver driver;
     @BeforeClass
      public void setUp() throws MalformedURLException{
      DesiredCapabilities caps = new DesiredCapabilities();
      caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
      caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");
      caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
      caps.setCapability(MobileCapabilityType.DEVICE_NAME, "iPad");
      caps.setCapability("udid","Real Device Identifier");
      driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
     @Test
      public void testExample(){
      driver.get("https://www.google.com");
      WebElement searchBox=driver.findElement(By.name("q"));
      searchBox.sendKeys("Appium for mobile automation");
    }
     @AfterClass
      public void tearDown(){
      driver.quit();
    }
    }
..................Content has been hidden....................

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