Wednesday, 26 March 2014

Appium on anroid with windows.

Easy way to learn appium for selenium user.

Here I am going to give the android automation example with Appium on windows machine. Please ensure that before going to use this , The below software is intalled.

1- Node.js Download
2- Appium Download
3- Android SDK. Download
4- JDK Download
5- Selenium Server and Client Jar (3 jars) Download
6- Eclipse Download
7- install apk info s/w on android mobile Download
8- Selendroid jar Download

About Appium :

Appium is tool where we can automate hybrid and native application. There is a lot of mobile automation tool is in market but the beuty of appium is very much interesting for selenium user. And as well as it gives the cross functionalty solution for Android and IOS.

Now when we come to practicle few things we must know.

i- We can develope our code by two way. One is gving the automation code to UIautomator (defalut provided by android and which is named by "android" ) and one more Selendroid (which suport by Appium server and named by "selendroid"). But code writing style is different on both UIautomator and Selendroid to handle the java script and mobile guestures.

ii - UIautomator does not support below API level 17. Means It suports only greater then 4.2 Android versions mobile.

iii- To support below API level 17 of Android the Selendroid is only way to atuomate by help of appium. For that we dont need to do exatra download. But we should make sure the id is proper. Becuase when we inspect element for any field in UIautomator that time the id is not showing with full path where as selendroid can not able to take the partial id. In selendorid we should provide full path of id. For example:

if username field is having id with - com.andoid.id/userName

when you inspect element with UIautomator it may show userName only or may not show the id as well. That case we can use selendroid inspecotor or Appium inspector to see full id of that particular element.

iv- If you want to inspect the element through selendroid that case we need to download the selendoid jar and also we need this jar to handle few mobile gesture licke filck,swipe e.t.c. But we can inspect element through appium inspector as well. So it is optional to download the selendroid jar.



Here I am giving two example one is appium with UIautomator and another one is Appium with selendroid.

To view app-package and app-activity please use apk-info software in mobile to see the details.

1- Appium with UIautomation as android.




import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Testa {
       WebDriver driver;
       @BeforeClass
       public void setUp() throws MalformedURLException{
              DesiredCapabilities capabilities = new DesiredCapabilities();
              capabilities.setCapability("device","android");  // Here we have chosen Android UIautomator to automate our code.
              capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
              capabilities.setCapability(CapabilityType.VERSION, "4.4");
              capabilities.setCapability(CapabilityType.PLATFORM, "Windows");
              capabilities.setCapability("app-package", "com.companyname.project"); // This is package name of your app (you can get it from apk info app)
              capabilities.setCapability("app-activity", "com.companyname.project.ActivityName"); // This is Launcher activity of your app (you can get it from apk info app)
              driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
       }
       @Test
       public void test() throws InterruptedException{
              driver.findElement(By.id("com.companyname.project:id/edit_text_username")).sendKeys("some name");;
              driver.findElement(By.id("com.companyname.project:id/edit_text_password")).sendKeys("some pwd");;
              driver.findElement(By.id("com.companyname.project:id/button_sign_in")).click();
              waitForElement(driver, "Any text of Welcome page", 300);
       }
       public boolean waitForElement(WebDriver driver, String xpath, int seconds) throws InterruptedException{
              int t=0;
              while(t<seconds*10){
                     if(driver.findElements(By.name(xpath)).size()>0)
                           return true;
                     else{
                           Thread.sleep(100);
                           t++;
                           continue;
                     }
              }      
              System.out.println("waited "+seconds+"seconds. But couldn't find "+xpath+ " in the element specified");
              return false;
       }
}





2- Appium with Selendoid as selendoid.





import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
//import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class testa2 {
       WebDriver driver;
       @BeforeClass
       public void setUp() throws MalformedURLException{

              //Set up desired capabilities and pass the Android app-activity and app-package to Appium      
              File app = new File("DriverName://Directory//Yourapplication.apk");
              DesiredCapabilities capabilities = new DesiredCapabilities();
              capabilities.setCapability("device","selendroid"); // Here we have chosen Selendroid to automate our code.
              capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
              capabilities.setCapability("app", app.getAbsolutePath());
              capabilities.setCapability(CapabilityType.VERSION, "4.0");
              capabilities.setCapability(CapabilityType.PLATFORM, "Windows");
              capabilities.setCapability("app-package", "com.companyname.project"); // This is package name of your app (you can get it from apk info app)
              capabilities.setCapability("app-activity", "com.companyname.project.ActivityName"); // This is Launcher activity of your app (you can get it from apk info app)
              driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
       }
       @Test
       public void test() throws InterruptedException{
              driver.findElement(By.id("com.companyname.project:id/edit_text_username")).sendKeys("some name");;
              driver.findElement(By.id("com.companyname.project:id/edit_text_password")).sendKeys("some pwd");;
              driver.findElement(By.id("com.companyname.project:id/button_sign_in")).click();
              waitForElement(driver, "Any text of Welcome page", 300);
       }
       public boolean waitForElement(WebDriver driver, String xpath, int seconds) throws InterruptedException{
              int t=0;
              while(t<seconds*10){
                     if(driver.findElements(By.name(xpath)).size()>0)
                           return true;
                     else{
                           Thread.sleep(100);
                           t++;
                           continue;
                     }
              }      
              System.out.println("waited "+seconds+"seconds. But couldn't find "+xpath+ " in the element specified");
              return false;
       }
       public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
              private RemoteTouchScreen touch;

              public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
                     super(remoteAddress, desiredCapabilities);
                     touch = new RemoteTouchScreen(getExecuteMethod());
              }
              public TouchScreen getTouch() {
                     return touch;
              }
       }
}






8 comments:

  1. Hi,

    The post was indeed helpful, but still I could not automate my selendroid test cases using Appium for API level 16 (4.1.2). The issue I am facing is that Selendroid is not able to receive any commands. It seems it is getting hanged.

    Please have a look at my google-groups discussion. You input can help many people as well.

    https://groups.google.com/forum/#!topic/appium-discuss/dFBb3MAB5vc

    Regards,
    Krish

    ReplyDelete
  2. Try to give small latter s incase of S. Here is the example

    ReplyDelete
  3. Can you please copy your direct script here ? so that I can have try to help you out.

    ReplyDelete
  4. Best Place to learn Appium Selenium Testing Training in Chennai http://thecreatingexperts.com/appium-training-in-chennai/

    ReplyDelete
  5. Appium training in Chennai
    We provide best appium training in Chennai with real time scenarios. For real time training reach us 8122241286 and become experts in appium.
    Appium training in chennai

    ReplyDelete
  6. If you are developing an app for a specific platform (and have no intention of supporting others in future), Appium is not really required and this is basically the answer you are looking for. Appium becomes meaningful only when you have apps targeting more than one platform (Windows, Android or iOS to cite some). Appium becomes essential if you have a webview-based app (necessarily) targeting many platforms out there.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Thanks for sharing this Information.It was nice to read this
    Appium Training in Irving

    ReplyDelete