Thursday, 7 August 2014

Android hybrid application testing using appium

How to inspect element:

step-1 get source code of your mobile application from your development team.

step-2 open the chrome browser from command prompt by type this command 
chrome.exe --disable-web-security

step-3 find the index.html file from the mobile source code which is given by dev team.

step-4 now copy the path for index.html and paste the url in chrome. For example if you copied the 
mobile sournce code in desktop then the url will be C://User/Desktop/app/www/index.html

step-5 now press F12 on chrome. Then click emulate. 

step-6 Refresh/reload your browser.

step-7 Now you will see your application as like your mobile on the chrome browser.

step-8 You can now inspect element by chrome developer tool as like the web applications. 


Test script:

import io.appium.java_client.AndroidKeyCode;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SimpleTest {

private AppiumDriver driver;
public WebDriver driverNew;

@BeforeClass

public void setUp(){
// set up appium

File app = new File("apppath");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
// capabilities.setCapability("automationName", "Android");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","Android");  
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "apppackage");
capabilities.setCapability("appActivity", "appactivity");
try {
//driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@AfterClass

public void tearDown() throws Exception {
//driver.quit();
}

@Test

public void addContact() throws InterruptedException{
 
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")){
driver.context(contextName);
}

}
             driver.findElement(By.id("the id which you inspect by chrome browser")).click();
}
}

Wednesday, 26 March 2014

Appium architecture by Ramshankar.



APPIUM  ARCHITECTURE.
Appium is free tool to automate the native mobile application for Android and IOS.  The major advantage is that it is integrated to selenium where we can test cross platform for all the Andoid and IOS versions. The person who knows selenium they can easily learn Appium.
Appium is an HTTP server which is written in node.js where we can send web driver sessions for platforms like iOS and Android.  I am clearing here about node.js is nothing but it is server like selenium server where Google v8 free JavaScript engine and http server are majorly used to driver the automation.
Appium is kind of hack to automation the app with different platform like Android and IOS.  If the UI layer is same on both Android and IOS then we can develop common automation script for all versions of Android and IOS.

See the above picture where the we have option in web driver script itself whether to go UIAUTOMATOR or SLENDROID.  When Appium sever receive this accordingly set it and send it to the UIautomator or Selendroid respectively. Accordingly it install the bootstrap.jar on the device and set it for automation. Bootstarp.jar nothing but it contains the UIAUTOMATOR/SELENDROID and TCP server by which we can send the corresponding command to perform the job by UIAUTOMATOR/SELENDROID at device level.



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;
              }
       }
}