Thursday 16 March 2017

Appium - Caliculator Automation - windows os



This is the first programme,


Setup

Install Android  SDK s and Appium ( no need of node.js / npm)

How to install SDK :
download Android studio it will automatically download SDK.
you can find sdk downloaded here C:\Users\PK\AppData\Local\Android\sdk

How to install Appium:
go to official appium site and download the latest appium and just start appium ( nothing required to configure )










Configure like above, when you Launch your appium, it should show console message as above.

Connect your mobile device via USB cable and choose FileTransfer option in USB options.

to check the device properly connected or not.



Path settings:

SET : ANDROID_HOME to C:\Users\PK\AppData\Local\Android\sdk

SET path : %ANDROID_HOME%\tools ;%ANDROID_HOME%\platform-tools ;

Android_home






Add below dependencies


     only one jar required, it internally contains selenium-java libs


<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.3</version>
</dependency>


import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

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;

public class NewTest {

public static void main(String[] ards) throws MalformedURLException {
WebDriver driver;
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "ZY222XRC5L");

// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

// Set android VERSION desired capability. Set your mobile device's OS
// version.
capabilities.setCapability(CapabilityType.VERSION, "6.0.1");

// Set android platformName desired capability. It's Android in our case
// here.
capabilities.setCapability("platformName", "Android");

// Set android appPackage desired capability. It is
// com.android.calculator2 for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.calculator2");

// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appActivity",
"com.android.calculator2.Calculator");

// Created object of RemoteWebDriver will all set capabilities.
// Set appium server address and port number in URL string.
// It will launch calculator app in android device.
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

// Click on DELETE/CLR button to clear result text box before running
// test.
driver.findElements(By.xpath("//android.widget.Button")).get(0).click();

// Click on number 2 button.
driver.findElement(By.name("2")).click();

// Click on + button.
driver.findElement(By.name("+")).click();

// Click on number 5 button.
driver.findElement(By.name("5")).click();

// Click on = button.
driver.findElement(By.name("=")).click();

// Get result from result text box.
String result = driver.findElement(
By.className("android.widget.EditText")).getText();
System.out.println("Number sum result is : " + result);

driver.quit();
}
}



we can locate elements using

C:\Users\PK\AppData\Local\Android\sdk\tools\uiautomatorviewer.bat  tool