Appium Automated Test Cases

Source: Internet
Author: User
Tags appium testng

Original Address http://www.cnblogs.com/tobecrazy/p/4579631.html

Original Address http://www.cnblogs.com/tobecrazy/

The blogger has a lot of dry goods, you can study more

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:

Package Com.dbyl.core; Import Org.openqa.selenium.by;import Org.openqa.selenium.webelement;import Org.openqa.selenium.remote.capabilitytype;import org.openqa.selenium.remote.DesiredCapabilities; Import Org.testng.assert;import Org.testng.annotations.aftermethod;import Org.testng.annotations.BeforeMethod; Import Org.testng.annotations.test;import Io.appium.java_client.android.androiddriver;import Java.io.File;import Java.net.url;import Java.util.list;import Java.util.concurrent.TimeUnit;     public class Zhihu {private Androiddriver driver; @BeforeMethod (alwaysrun=true) public void SetUp () throws Exception {//Set up Appium File classpathroot        = New File (System.getproperty ("User.dir"));        File Appdir = new file (Classpathroot, "apps");        File app = new file (appdir, "zhihu.apk");        Desiredcapabilities capabilities = new Desiredcapabilities ();        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 this capabilities.setcapability ("app", App.getabsolutepath ());        Capabilities.setcapability ("Apppackage", "com.zhihu.android");        Support Chinese capabilities.setcapability ("Unicodekeyboard", "True");        Capabilities.setcapability ("Resetkeyboard", "True");        No need sign capabilities.setcapability ("NoSign", "True");        Capabilities.setcapability ("Appactivity", ". ui.activity.GuideActivity");    Driver = new Androiddriver (New URL ("Http://127.0.0.1:4723/wd/hub"), capabilities);    } @AfterMethod (alwaysrun=true) public void TearDown () throws Exception {driver.quit (); } @Test (groups={"Zhtest"}) public void login () {//find login button webelement Loginbutton = Driver.f   Indelement (By.id ("Com.zhihu.android:id/login"));     Loginbutton.click ();                Wait for 20s driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Find login userName and password editText list<webelement> textfieldslist = driver.findelementsbyclassname        ("Android.widget.EditText");        Textfieldslist.get (0). SendKeys ("[email protected]");        Textfieldslist.get (1). SendKeys ("cookies123");                Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);        Find OK button byname driver.findelementbyname ("OK"). Click ();                Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);                    Find keyword Home and verify it is display Assert.asserttrue (driver.findelement ("Home")). By.name ()); }}

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 Test Cases

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.