Wednesday, 23 December 2015

Appium - Differences in executing the web automation script in Mobile and PC.

Below video demo demonstrates differences for web automation in mobile and pc.

Below script can be executed in mobile and pc.
Sample Program:
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import io.appium.java_client.remote.MobileCapabilityType;

public class SampleBrowserTest {
   
 public static void main(String[] args) throws MalformedURLException, InterruptedException {

  String sourceToTest = "mobile";
 
  WebDriver driver = sourceToTest.equals("mobile") ? getAppiumDriverForChrome() : getWebDriverForChrome();     
  driver.get("http://www.google.com");  

  driver.findElement(By.id("lst-ib")).sendKeys("java");
  driver.findElement(By.className("sbico")).click();
  //driver.findElement(By.xpath("//h3/a")).click();

  driver.quit();
 }

 private static WebDriver getWebDriverForChrome(){
     System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers//chromedriver.exe");
     return new ChromeDriver();
 }

 private static WebDriver getAppiumDriverForChrome() throws MalformedURLException{
    
     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"");
     //capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
     capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
     capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    
     return new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
 }

}
 

Above script can be re-written using 'AppiumDriver', but the script can be executed only in mobile web.
Sample Program:
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class SampleBrowserTest {
   
 public static void main(String[] args) throws MalformedURLException, InterruptedException {
      AppiumDriver driver = null;     
 
  try{
      driver =  getAppiumDriverForChrome();
      driver.get("http://www.google.com");  
      Thread.sleep(2000);
      driver.findElement(By.id("lst-ib")).sendKeys("java");
      driver.findElement(By.className("sbico")).click();
      //driver.findElement(By.xpath("//h3/a")).click();
     
  }finally{
      if(driver!=null){
          driver.quit(); 
      }
     
  }
 
 }

 private static AppiumDriver  getAppiumDriverForChrome() throws MalformedURLException{
   
     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"");
     //capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
     capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
     capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    
     return new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);   
 }

}

1 comment: