Appium_Automators' Cafe Appium android example program for windows using java

Appium android example program for windows using java - native app

To perform the automation using appium for the native android app just .apk file is enough. No need of developers code at all.

When we write the appium script for automation we need to specify the package name and activity name. We can get package and all activity names from the .apk file itself. For this we need to convert .apk file to java class files. See  here to perform that.
I used following environments to accomplish this example program,
  • Windows7
  • Android SDK
  • JDK
  • Eclipse
  • TestNG

Install and start the appium server:
Appium server for windows platform is just a zip folder. It contains all the packages needed for appium such as node, webdriver etc. We no need to install any APIs additionally
  • Download the latest AppiumForWindows.zip from here
  • Unzip the AppiumForWindows.zip
  • Click on the appium.exe present in the unzipped appium folder. It will be opened up as below,


Appium Server For Windows

  • Now click on the 'Launch' button, It will start the server at 127.0.0.1:4723 as below,
Appium server is started
  • Then run the below program once the android emulator is ready.
Appium example program for android
package com.qa.test;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class androidExample {
WebDriver driver = null;
         
        @BeforeMethod
         public void setup() {
                File appDir = new File("E://AndroidApps ");
File app = new File(appDir, "myApp.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2.2");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
                // Here we mention the app's package name, to find the package name we  have to convert .apk file into java class files
capabilities.setCapability("app-package","com.myApp.activity");
                //Here we mention the activity name, which is invoked initially as app's first page.
capabilities.setCapability("app-activity","LoginActivity");
//capabilities.setCapability("app-wait-activity","LoginActivity,NewAccountActivity");
capabilities.setCapability("app", app.getAbsolutePath());

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

         }

       @Test
public void loginTest() throws Exception {
driver.findElement(By.xpath("//EditText[@text='Email Address']")).sendKeys("tester@gmail.com");
driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
driver.findElement(By.xpath("//CheckBox")).click();
driver.findElement(By.xpath("//Button[@text='Login']")).click();

WebDriverWait wait = new WebDriverWait(driver,80);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[@text='Logout']")));
driver.findElement(By.xpath("//Button[@text='Logout']")).click();

        }

       @AfterMethod
        public void tearDown() {
               driver.quit();
        }

}

Running this program will install the app in the emulator if it is not present already. If the app is installed already it will open and perform the automation steps.

If your app is already opened with some other activity(e.g. my profile page) appium will be waiting for the specified activity (com.myApp.activity.LoginActivity) when you run the automation script. In this case automation script will fail, since the app is opened with some other activity.

So it is better to start the appium server with an option full reset. Perform the following steps in appium GUI server,
  • Check the option 'Perform Full Reset'
  • Go to File > Preferences and check the option 'Reset Application State After Each Session'
When you use these options in the GUI server, you might also need to specify the app path, package and Activity name as below,

Start server with full reset

This will uninstall the app once your tests are completed and will install the app into emulator when the test starts initially.

Note:
  • Here I used the uiautomatorviewer to find the element hierarchy and then I constructed the xpath manually. To learn about constructing the xpath, please gohere
  • You should set ANDROID_HOME and path for Android SDK in environment variable
  • Emulator adb device should have API level greater than or equal to 17

修改后的代码如下:

package com.qa.test;

import java.io.File;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class androidExample {
    WebDriver driver = null;
        
    @BeforeMethod
    public void setup() throws Exception {
        File appDir = new File("E://AndroidApps");
        File app = new File(appDir, "myApp.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("device","Android");
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability(CapabilityType.VERSION, "4.2.2");
        capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
                       // Here we mention the app's package name, to find the package name we  have to convert .apk file into java class files
        capabilities.setCapability("app-package","com.myApp.activity");
                       //Here we mention the activity name, which is invoked initially as app's first page.
        capabilities.setCapability("app-activity","LoginActivity");
        //capabilities.setCapability("app-wait-activity","LoginActivity,NewAccountActivity");
        capabilities.setCapability("app", app.getAbsolutePath());
        
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
    
            }

    @Test
    public void loginTest() throws Exception {
    driver.findElement(By.xpath("//EditText[@text='Email Address']")).sendKeys("tester@gmail.com");
    driver.findElement(By.xpath("//LinearLayout/EditText[2]")).sendKeys("Testerpwd");
    driver.findElement(By.xpath("//CheckBox")).click();
    driver.findElement(By.xpath("//Button[@text='Login']")).click();
    
    WebDriverWait wait = new WebDriverWait(driver,80);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//Button[@text='Logout']")));
    driver.findElement(By.xpath("//Button[@text='Logout']")).click();
    
           }

      @AfterMethod
       public void tearDown() {
              driver.quit();
       }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值