Two. Android Phone automation test real machine Run

Source: Internet
Author: User

Although mobile phone automation test cases can be run on the simulator, but after all, the simulator and the real machine still have a difference. After running the test case on the simulator, it took me two more days to study the real-world running test cases. During the period also encountered a lot of problems, but finally finished, and now the test case to run the method of the real machine to say, and then list the problems I encountered.

How to run a test case on a real machine

One, turn on your phone USB Debug Mode

Different phones have different ways to turn on the USB debugging mode, but go online to check your phone debug mode open method (Http://wenku.baidu.com/view/3077f06c25c52cc58bd6be15.html?re=view). There is also an application that can download a "USB debug shortcut switch" On the internet and turn on the USB debug mode of the phone.

Two, Connect your phone to your computer

Connect your phone with a data cable to your computer and authorize USB debug mode. To view the effect of the connection, run the command under cmd: adb devices-l view Udid as shown in:



If there is an output, the connection is successful.

Three, Start Appium Service

Start the Appium service according to the Udid that is found, run the command:

#>appium-a 127.0.0.1-p 4723–u 4ca1558c--no-reset

A string of characters followed by the-u parameter is the udid of the phone, which is checked by the second step.

When the program outputs such as information, indicating that the Appium started successfully, you can run the test script at this time.

Four, writing and running test scripts

The test scripts run by the real machine are almost identical to those of the simulator, but some are not quite the same. The detailed script is detailed below:

#-*-Coding:utf-8-*-

Import OS

Import time

Import UnitTest

From selenium import Webdriver

From lib2to3.pgen2.driver import driver

From Lib2to3.tests.support import driver

Path=lambda P:os.path.abspath (

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

)

Global driver

Class loginandroidtests(unittest. TestCase):

def setUp(self):

desired_caps={}

desired_caps[' device '= ' Android '

desired_caps['platformname ']=' Android '

desired_caps['browsername']= '

desired_caps['version ']=' 4.4.2 '

desired_caps[' devicename ']=' miui-kxccnbf22.0 '# This is the model of the test machine, you can view the phone's about native options to get

desired_caps['app'= PATH (' d:\\androidautomation\\androidautotest\\app\ \ zhongchou.apk ') #被测试的App在电脑上的位置

#如果知道被测试对象的apppage, appactivity can be added to the following two parameters, if you do not know, can be commented out, does not affect the use case execution

#desired_caps [' apppackage ']= ' Com.subject.zhongchou '

#desired_caps [' appactivity ']= '. Zhongchou '

self. Driver=webdriver. Remote (' Http://localhost:4723/wd/hub ', Desired_caps)

def TearDown(self):

self. driver.quit ()

def test_login(self):

Time.sleep (10)

#点击 "Register Login" button button=self.driver.find_element_by_id ("Com.subject.zhongchou:id/register_button")

Button.Click ()

Time.sleep (5)

#登录

Name =self. driver.find_element_by_id (' Com.subject.zhongchou:id/loginnumber_phone ')

Name.click ()

Name.send_keys (' 183xxxxxx05 ')

PSD =self. driver.find_element_by_id (' Com.subject.zhongchou:id/loginnumber_password ')

Psd.click ()

Psd.send_keys (' XXXXXXXX ')

blogin=Self driver.find_element_by_id (' Com.subject.zhongchou:id/go_numberlogin ')

Blogin.click ()

Time.sleep (10)

#此处加上检测登录是否成功的代码

if __name__ = = ' __main__ ':

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

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

Open a separate CMD window and run the test script (Python xxxx.py). At this point the server will have output, the program is running on the phone.

Note: To install Appium Settings on your phone while the test case is running, follow the prompts to allow installation.

Study the problem of real machine running

1, after the computer is connected to the mobile phone, run adb devices–l no information output?

After the computer is connected to the phone, you will normally be prompted to install the driver, if the phone newer words may not prompt, but run adb devices–l after no output. At this point only need to install 91 mobile phone assistant or other phone management tools, install the appropriate driver, connect again.

2, after running the program prompt: Webdriverexception:message:u "A new session could not be created. (Original Error:activity used to start app doesn ' t exist or cannot be launched! Make sure it exists and is a launchable activity) "?

When I learned how to write scripts on the Web, I simulated writing my own test case, but after connecting to the real machine, when I ran the script, the error occurred during the execution, but the program couldn't run.

Viewed online, found the problem: is the program's ' Apppackage ' and ' appactivity ' is not correct, find the solution, there is said to decompile the test app, and then find the inside of the appactivity. But the anti-compilation only found that the inside of the things a lot, can not find the beginning of the time appactivity is? Had to find another way.

Finally, a solution was found in a forum reply: Two lines of code in the Setup function are commented out.

desired_caps[' apppackage ']= ' Com.subject.zhongchou '

desired_caps[' appactivity ']= '. Zhongchou '

3, in the program execution of this process, the input box with the Send_keys () function to enter the value of the time, cannot be entered?

When I execute the test case, I find that the user name and password cannot be entered into the input box with the Send_keys () function. Because one input will open the soft keyboard, and the top of the soft keyboard layout is nine Gongge, click Always error.

On the internet to check a lot of ways, people are a use of this function ah, why I do not have it? I think of the first switch to digital or English input mode, and then input, toss it up very troublesome, has not succeeded. Finally found that it is my own good silly, installed the reason for Sogou input method, the input method is removed after the success!

Two. Android Phone automation test real machine Run

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.