Appium automated testing for Android clients

Source: Internet
Author: User
Tags appium testng

Appium is an open source framework, relatively not very stable. Reprint please specify the source!!!!

Some days ago, the configuration of the Appium test environment, as for the environment how to build, reference: http://www.cnblogs.com/tobecrazy/p/4562199.html

Next we look at the tests that are known to Android clients. Download by yourself

The configuration directory is as follows: Name the client as zhihu.apk

Write a test case based on Java testng:

Since I am familiar with selenium webdriver, the strategy used in Android object recognition is similar to selenium webdriver

How to identify the elements of the app, this is used in the Android SDK Tools directory tool uiautomatorviewer, execute Uiautomatorviewer.bat can

    • First parse Zhihu app to get its package and activity, here use Appium GUI interface, specific operation

    • Next identify the elements of the app, such as the login button, see ResourceID, can be rainy webdriver find element by.id

As for the login username and password, in Android is the EditText control, you can use the By.class

Complete the object recognition, the final code is as follows:

 PackageCom.dbyl.core;Importorg.openqa.selenium.By;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.remote.CapabilityType;Importorg.openqa.selenium.remote.DesiredCapabilities;ImportOrg.testng.Assert;ImportOrg.testng.annotations.AfterMethod;ImportOrg.testng.annotations.BeforeMethod;Importorg.testng.annotations.Test;ImportIo.appium.java_client.android.AndroidDriver;ImportJava.io.File;ImportJava.net.URL;Importjava.util.List;ImportJava.util.concurrent.TimeUnit; Public classZhihu {PrivateAndroiddriver driver; @BeforeMethod (Alwaysrun=true)     Public voidSetUp ()throwsException {//Set up AppiumFile Classpathroot =NewFile (System.getproperty ("User.dir")); File Appdir=NewFile (Classpathroot, "apps"); File app=NewFile (Appdir, "zhihu.apk"); Desiredcapabilities Capabilities=Newdesiredcapabilities (); Capabilities.setcapability (Capabilitytype.browser_name,""); Capabilities.setcapability ("PlatformName", "Android"); Capabilities.setcapability ("DeviceName", "Android Emulator"); Capabilities.setcapability ("Platformversion", "4.4"); //If no need install don ' t add thisCapabilities.setcapability ("App", App.getabsolutepath ()); Capabilities.setcapability ("Apppackage", "com.zhihu.android"); //Support ChineseCapabilities.setcapability ("Unicodekeyboard", "True"); Capabilities.setcapability ("Resetkeyboard", "True"); //no need signCapabilities.setcapability ("NoSign", "True"); Capabilities.setcapability ("Appactivity", ". Ui.activity.GuideActivity"); Driver=NewAndroiddriver (NewURL ("Http://127.0.0.1:4723/wd/hub"), capabilities); } @AfterMethod (Alwaysrun=true)     Public voidTearDown ()throwsException {driver.quit (); } @Test (Groups={"Zhtest"})     Public voidLogin () {//Find Login ButtonWebelement Loginbutton = driver.findelement (By.id ("Com.zhihu.android:id/login"));                Loginbutton.click (); //Wait for 20sDriver.manage (). Timeouts (). implicitlywait (20, Timeunit.seconds); //Find login userName and password EditTextlist<webelement> textfieldslist = Driver.findelementsbyclassname ("Android.widget.EditText"); Textfieldslist.get (0). SendKeys ("[Email protected]"); Textfieldslist.get (1). SendKeys ("cookies123"); Driver.manage (). Timeouts (). Implicitlywait (20, Timeunit.seconds); //Find OK button bynameDriver.findelementbyname ("OK"). Click (); Driver.manage (). Timeouts (). Implicitlywait (90, Timeunit.seconds); //Find keyword Home and verify it is displayAssert.asserttrue (Driver.findelement (By.name ("Home") . isdisplayed ()); }}

Next, you can debug, start the Android emulator first, after the start is clearly tested, if the results are as follows:

Description Simulator can use

Next, run Apppium on the command line

Wait 1-2 minutes

Run as testng, you can see the automatic installation, landing know

Passed:login

===============================================
Default Test
Tests Run:1, failures:0, skips:0
===============================================


===============================================
Default Suite
Total tests Run:1, failures:0, skips:0
===============================================

[TestNG] Time taken by [email protected]: 1938 MS
[TestNG] Time taken by [email protected]: 121 ms
[TestNG] Time taken by [Failedreporter passed=0 failed=0 Skipped=0]: 0 ms
[TestNG] Time taken by [email protected]: 423 ms
[TestNG] Time taken by [email protected]: 1669 MS
[TestNG] Time taken by [email protected]: ms

Appium automated testing for Android clients

Related Article

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.