Appium+python3 Getting Started

Source: Internet
Author: User

Objective:

Appium is an automated testing open source tool that supports native applications, Web applications, and hybrid applications on IOS and Android platforms.

First, the Environment configuration

1. Install node. js

https://nodejs.org/

2, Installation Appium

http://appium.io/

3. Install the Android SDK

Http://tools.android-studio.org/index.php/sdk

4, Installation Python-client

PIP3 Install Appium-python-client

5, Installation Appium-client

NPM Install WD

Finally, open the command line, enter the "appium-doctor" command, and if you get the following prompt, the environment you appium is ready to complete.

Second, the service key words

Desired capabilities must be provided at the start of the session.

Desired capabilities is essentially stored in the form of the key value dictionary, which the client sends to the server and tells the server how we want to test it.

Desired_caps = {}

desired_caps[' platformname ' = ' Android '

desired_caps[' platformversion '] = ' 6.0.1 '

desired_caps[' devicename '] = ' e0bbc8b7 '

desired_caps[' apppackage '] = ' com.ximalaya.ting.android '

desired_caps[' appactivity '] = ' com.ximalaya.ting.android.host.activity.WelComeActivity '

desired_caps["Unicodekeyboard"] = "True"

desired_caps["Resetkeyboard"] = "True"

Self.driver = Webdriver. Remote (' Http://127.0.0.1:4723/wd/hub ', desired_caps)

It tells Appium server something like:

DeviceName: Which device to start, is it a real machine or an emulator? IPhone simulator,ipad simulator,iphone Retina 4-inch,android emulator,galaxy S4 ...

Automationname: What kind of automation engine is used? Appium (default) or Selendroid.

PlatformName: What kind of mobile platform is used? IOS, Android, Orfirefoxos.

? platformversion: Specifies the system version of the platform. For example, the Android platform, version 5.1.

Appactivity: The activity name of the app under test. such as Mainactivity,. Settings. Note that the native app should add a "." Before the activity.

Apppackage: The Java package for the app to be tested. Like Com.example.android.myApp, Com.android.settings.

Third, positioning control

1. ID Location

How to use: driver.find_element_by_id (' Com.android.calculator2:id/formula ')

2. Name positioning

How to use: Driver.find_element_by_name ("9"))

3. Class name positioning

How to use: Driver.find_element_by_class_name ("Android.widget.Button"))

4. XPath positioning

How to use: Replace the name of the label with the attribute of class.

Driver.find_element_by.xpath ("//android.view.viewgroup/android.widget.button")

When the result is the same as class, you can use the control's property values to differentiate.

Driver.find_element_by_xpath ("//android.widget.button[contains (@text, ' 7 ')]"). Click ();

Driver.find_element_by_xpath ("//android.widget.button[contains (@content-desc, ' Times ')"). Click ();

Driver.find_element_by_xpath ("//android.widget.button[contains (@text, ' 7 ')]"). Click ();

Driver.ffind_element_by_xpath ("//android.widget.button[contains (@content-desc, ' equals ')]"). Click ();

The use of XPath on the appium is still very strong, sometimes need to write more smelly longer positioning syntax, because the app on the elements of the class command is already long, coupled with multi-level, the results can be imagined.

5, accessibility ID location

How to use: In fact, our core is to find the contentdescription attribute of the element. It is the content-desc of the elements.

DRIVER.FIND_ELEMENT_BY_ACCESSIBILITY_ID ("Plus"). Click ();

6, Android Uiautomator positioning

How to use:

Any attribute of an element can be positioned using the Android Uiautomator method, but the uniqueness of this positioning method is ensured.

Driver.find_element_by_android_uiautomator ("New Uiselector (). Text (\" 8\ ")"). Click ();

Driver.find_element_by_android_uiautomator ("New Uiselector (). Description (\" Plus\ ")"). Click ();

IV. Application operation

1. Installation Application

Installapp ()

Install the application to the device. The path to the APK package is required.

2. Uninstall the application

Removeapp ()

Remove an app from your device.

3. Close the application

Closeapp ()

Close the open app and close the currently open app by default, so you don't need to enter the parameter. This method does not really close the app, it is equivalent to pressing the home key to place the app in the background and can be started again via Launchapp ().

4. Start the application

Launchapp ()

Launch the app. You must be confused, not in the initialization of the configuration information has been specified by the application, the script will need to launch the application when running, why do you have this method to launch the application? Restarting the app is also a test point, which needs to be used with Closeapp ().

5. Check if the application is installed

Isappinstalled ()

Check if the app is already installed. The name of the application package needs to be passed. The returned result is ture or false.

6. Put the app in the background

Runappinbackground ()

Sends the currently active application to the background. This method requires an entry and specifies the length of time the app is placed in the background.

7. Application Reset

Resetapp ()

Resets the currently tested program to the out-of-start state. The method does not require an entry parameter.

Five, keyboard operation

1. SendKeys () method

Driver.find_element_by_name ("name"). Send_keys ("Jack");

2. Presskeycode () method

In addition, the Appium extension provides the Presskeycode () method. The method is unique to Android.

An operation that sends a key code. (Key code Comparison table please Baidu, here does not show.) )

Driver.press_keycode (3)//Click on the Android Home button

Driver.press_keycode (27)//Click the photo button

3, Input Method problem:

You must use Appium to bring your own keyboard and add:

desired_caps["Unicodekeyboard"] = "True"

desired_caps["Resetkeyboard"] = "True"

VI. touchaction operation

Appium auxiliary classes, mainly for gesture manipulation, such as sliding, long-press, drag and so on.

1. Press Control Presses ()

Start pressing an element or sitting punctuation (x, y). Press a position on your phone's screen with your finger.

Press (webelement el, int x, int y)

2, long press the control longpress ()

Start pressing an element or sitting punctuation (x, y). Compared to the press () method, longpress () more than one entry, since the long press, you have to have the time bar. The duration is measured in milliseconds. 1000 means press one second. The usage is the same as the press () method.

Longpress (webelement el, int x, int y, Duration Duration)

3. Tap the control tap ()

Performs a click on an element or control. Usage Reference Press ().

Tap (webelement el, int x, int y)

4, Mobile MoveTo ()

Points the pointer (cursor) from the past to the specified element or point.

Movto (webelement el, int x, int y)

5. Pause Wait ()

Pauses the execution of the script in milliseconds.

Action.wait (1000);

Vii. Other operations

Other operations are specific to the mobile device.

1. Extinguish the screen

Method: Lockdevice ()

Click the power button to turn off the screen.

The iOS device can be set to extinguish the screen for a period of time. Android does not have parameters, so the screen will not be lit again after the screen is extinguished.

Driver.lockdevice (+);//IOS

Driver.lockdriice ();//android

2. Close up the keyboard

Method: Hidekeyboard ()

This method is very useful when we put the keyboard back on, and then we need to pick up the keyboard and switch to the input box to enter the input box.

Driver.hidekeyboard ();//Pick up the keyboard

3. Slide

Method: Swipe ()

Simulates user swipe. Drag the control or element from one location (x, y) to another location (x, y).

Swipe (int startx, int starty, int endx, int endy, int duration)

* Start_x: The x-coordinate at which to start sliding. * Start_y: The y-coordinate at which to start sliding.

* End_x: Ends The x-coordinate of the slide. * End_y: The y-coordinate of the end slide.

* Duration: Duration.

Example: Driver.swipe (75, 500, 75, 0, 800);

4. Screen Cutting

Method: Get_screenshot_as_file ()

Usage: Driver.get_screenshot_as_file ('.. /screenshot/foo.png '), parameter is the saved picture path and name

5. Get various properties of the control

Method: Get_attribute ()

Usage: driver.find_element_by_id (). Get_attribute (name),

Name is the flag on the left (class,package,checkable,checked ...),

The type of string that can be obtained:

Name (return Content-desc or text)

Text (return text)

ClassName (return class, only api=>18 can support it)

ResourceId (return to Resource-id, only api=>18 support)

Viii. assertion of UnitTest

In the UnitTest Unit test framework, the TestCase class provides methods to examine and report the failure:

>>assertequal (First, second, msg=none)

Determines whether the values of first and second are equal, if not equal, the test fails, and MSG is used to define the exception information thrown after the failure.

>>assertnotequal (First, second, msg=none)

Test first and second are not equal, and if they are equal, the test fails.

>>assertture (Expr,msg=none)

>>assertfalse (Expr,msg=none)

Test expr as ture (or false)

>>assertis (First, second, msg=none)

>>assertisnot (First, second, msg=none)

The first and second tests are (or are not) the same object.

>>assertisnone (expr, Msg=none)

>>assertisnotnone (expr, Msg=none)

Test expr Yes (or no) to none

>>assertin (First, second, msg=none)

>>assertnotin (First, second, msg=none)

The test first is (or is not) in second. Second contains whether first is included.

Nine, instance code (example app: Himalaya FM)

Import OS

Import UnitTest

From Appium import Webdriver

From time import sleep

# Returns ABS Path relative to this file and not CWD

PATH =lambdap:os.path.abspath (

Os.path.join (Os.path.dirname (__file__), p)

)

Class Contacts Android Tests (unittest. TestCase):

def setUp (self):

Desired_caps = {}

desired_caps[' platformname ' = ' Android '

desired_caps[' platformversion '] = ' 6.0.1 '

desired_caps[' devicename '] = ' e0bbc8b7 '

desired_caps[' apppackage '] = ' com.ximalaya.ting.android '

desired_caps[' appactivity '] = ' com.ximalaya.ting.android.host.activity.WelComeActivity '

desired_caps["Unicodekeyboard"] = "True"

desired_caps["Resetkeyboard"] = "True"

Self.driver = Webdriver. Remote (' Http://127.0.0.1:4723/wd/hub ', desired_caps)

Sleep (6)

def tearDown (self):

Self.driver.close_app ()

Self.driver.quit ()

Deftest_login (self):

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/tab_myspace '). Click ()

Sleep (2)

self.driver.find_element_by_accessibility_id (' Settings '). Click ()

Sleep (2)

Width =self.driver.get_window_size () [' width ']

Height =self.driver.get_window_size () [' Height ']

Self.driver.swipe (WIDTH/2, Height *7/8, width/2, height *4/8,1000)

# Different phone resolutions are different, so a coordinate with a different resolution of the phone may change the location, in order to make Apppium better compatible with different resolutions of the device,

# get the screen resolution before performing the swipe.

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_tv_login '). Click ()

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_username '). Send_keys (' 18500425217 ')

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_password '). Send_keys (' wj1234 ')

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_login '). Click ()

Sleep (4)

Text =self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/tab_myspace '). Text

Self.assertequal (text, ' my ')

Deftest_search (self):

Sleep (3)

Message =self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_item_finding_title '). Text

Self.assertequal (message, ' good books every day ')

self.driver.find_element_by_accessibility_id ("Search"). Click ()

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_search_et '). Send_keys (' Satin ')

self.driver.find_element_by_id (' Com.ximalaya.ting.android:id/main_search_button '). Click ()

Self.driver.get_screenshot_as_file ('/users/wangjuan/gallery/1.jpg ')

Self.driver.press_keycode (4)

Self.assertequal (message, ' good books every day ')

if__name__ = = ' __main__ ':

Suite = UnitTest. Testloader (). Loadtestsfromtestcase (contactsandroidtests)

UnitTest. Texttestrunner (verbosity=2). Run (Suite)

Above, hope to be helpful to you ~ ~

Appium+python3 Getting Started

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.