Java + Selenium + appium mobile phone automation test

Source: Internet
Author: User
Tags appium testng

First, the launch of the Android Simulator (Genymotion commonly known as the world's fastest simulator, can be installed by Baidu itself)


Second, start Appium (appium environment installation can self-Baidu)


Third, installation application to genymotion, such as I installed a computer small application, package named calctest.apk

Installation steps: (based on the Android SDK has been configured environment variables, can self-Baidu)
1, Win + R
2, CMD
3, ADB devices--check operation, list the existing settings name
4, adb install F:\Appium\CalcTest.apk--official installation of the app

such as: 192.168.229.101:5555 is the genymotion virtual machine I just opened


Iv. after successful installation, go back to Genymotiong to see that the installation is successful.

Open the app and you'll see that it's actually a simple calculator.


V. Open eclipse to create a MAVEN project and use the Uiautomatorviewer tool (the Android SDK Toolkit comes with it) for basic element positioning operations
1. Open the Android SDK to find the path: Android-sdks\tools as follows (get the app package name to decompile: AAPT dump badging apk path)

2. Open Uiautomatorviewr.bat


3, write the basic code as follows for reference only:

Package Appium_demo;import Java.net.malformedurlexception;import Java.net.url;import java.util.concurrent.TimeUnit ; Import Org.openqa.selenium.by;import Org.openqa.selenium.remote.desiredcapabilities;import Io.appium.java_ client.android.androiddriver;/** * @author Li Xiaowei e-mail:[email protected] @date creation Time February 11, 2018 morning 10:10:02 */public Class Calc_demo {public static void main (string[] args) throws Malformedurlexception {Androiddriver driver;d  Esiredcapabilities des = new desiredcapabilities (); Des.setcapability ("Automationname", "Appium");//selendroid//Automation mode selection//des.setcapability ("app", "c:\\software\\  calctest.apk ");//Configure the path of the APK to be tested//des.setcapability (" Browsername "," Chrome "); H5des.setcapability ("PlatformName", "Android");//Platform name Des.setcapability ("Platformversion", "4.4");// Mobile OS version des.setcapability ("Udid", "192.168.229.101:5555");//Unique Device identification des.setcapability for connected physical devices ("DeviceName", "S4") Udiddes.setcapability ("Apppackage", "Com.sky.jisuanji"),//app installed package name, note the original calctes with the phone type or simulator type.t.apk not the same des.setcapability ("Appactivity", ". Jisuanjizixieactivity ");//app testers often have to get activity, conduct relevant tests, and follow des.setcapability (" Unicodekeyboard "," True ");// Support Chinese input des.setcapability ("Resetkeyboard", "True");//Support Chinese input des.setcapability ("Newcommandtimeout", "10");// Timeout when no new command is set des.setcapability ("Nosign", "True");//skip check and debug the application for the steps driver = new Androiddriver (The New URL ("http:// 127.0.0.1:4723/wd/hub "), des);//virtual machine default address Driver.manage (). Timeouts (). implicitlywait (timeunit.seconds);// Set timeout wait time, default 250msdriver.findelement (By.id ("Com.android.calculator2:id/digit1")). Click ();//position ' 1 ' Driver.findelement (By.id ("Com.android.calculator2:id/plus")). Click ();//position ' + ' driver.findelement (By.id (" Com.android.calculator2:id/digit6 ")). Click ();//Position ' 6 ' driver.findelement (by.id (" com.android.calculator2:id/equal    "). Click ();//Position ' = '}}


VI. Use TESTNG to write formal test cases and begin to execute tests

Package Appium_operate;import Java.net.malformedurlexception;import Java.net.url;import Java.util.concurrent.timeunit;import Org.openqa.selenium.by;import Org.openqa.selenium.remote.desiredcapabilities;import Org.testng.assert;import Org.testng.annotations.BeforeTest ; Import Org.testng.annotations.dataprovider;import Org.testng.annotations.test;import Io.appium.java_ client.android.androiddriver;/** * @author Li Xiaowei e-mail:[email protected] @date creation Time February 11, 2018 morning 10:30:02 */public Class Calctest {androiddriver driver; @BeforeTestpublic void SetUp () throws Malformedurlexception{desiredcapabilities des = new desiredcapabilities ();//des.setcapability ("App", "c:\\");d es.setcapability ("PlatformName", "Android"); Des.setcapability ("Platformversion", "4.4");d es.setcapability ("Udid", "192.168.43.101:5555");d es.setcapability (" DeviceName "," S4 ");d es.setcapability (" Apppackage "," Com.android.calculator2 ");// Com.android.contactsdes.setCapability ("Appactivity", ". Calculator ");//.activities. PeopleactiviTydes.setcapability ("Unicodekeyboard", "true");d es.setcapability ("Resetkeyboard", "true");d es.setcapability (" Newcommandtimeout "," "(") ");d es.setcapability (" Nosign "," True ");d river = new Androiddriver (The New URL (" HTTP/    127.0.0.1:4723/wd/hub "), des);d river.manage (). Timeouts (). implicitlywait (timeunit.seconds);} @Test (Enabled = false) public void Add () {driver.findelement (By.xpath ("//android.widget.button[@text = ' 5 ']"). Click (    );    Driver.findelement (By.xpath ("//android.widget.button[@text = ' + ')"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' 8 ')"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' = ']"). Click (); String value = driver.findelement (By.xpath ("//android.widget.edittext[@class = ' android.widget.EditText ']").    GetAttribute ("text");        Assert.assertequals (Value, "13"); } @Test (Enabled = false) public void sub () {Driver.findelement (By.xpath ("//android.widget.button[@text = ' 1 ']"). C    Lick (); Driver.findelemenT (By.xpath ("//android.widget.button[@text = ' 0 ')"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = '-']"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' 8 ')"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' = ']"). Click (); String value = driver.findelement (By.xpath ("//android.widget.edittext[@class = ' android.widget.EditText ']").    GetAttribute ("text");         Assert.assertequals (Value, "2"); } @Test (Enabled = false) public void Mul () {driver.findelement (By.xpath ("//android.widget.button[@text = ' 5 ']"). C    Lick ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' x ')"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' 8 ')"). Click ();    Driver.findelement (By.xpath ("//android.widget.button[@text = ' = ']"). Click (); String value = driver.findelement (By.xpath ("//android.widget.edittext[@class = ' android.widget.EditText ']").    GetAttribute ("text"); Assert.assertequals (Value, "40"); } @DataProvider (name= "testdata") public object[][] GetData () {return new object[][]{{"20", "80", "100", "+"},{"9    0 "," 3 ","},{"," X "," 6 "," 2 "," 3 "," ÷ "}; } @Test (Dataprovider = "testdata") public void Calctestcase (String num1,string num2,string result,string Calctyp e) {for (char Num:num1.toCharArray ()) {Driver.findelement (By.xpath ("//android.widget.button[@text = '" +    String.valueof (num) + "']"). Click ();    } driver.findelement (By.xpath ("//android.widget.button[@text = '" +calctype+ "')"). Click (); for (char Num:num2.toCharArray ()) {Driver.findelement (By.xpath ("//android.widget.button[@text = '" +string.valueof (    num) + "']"). Click ();    } driver.findelement (By.xpath ("//android.widget.button[@text = ' = ']"). Click (); String value = driver.findelement (By.xpath ("//android.widget.edittext[@class = ' android.widget.EditText ']").    GetAttribute ("text");     Assert.assertequals (value, result); }}

  

Java + Selenium + appium mobile phone automation test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.