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. In the second chapter we talked about running the test case on the simulator, and I spent two more days studying the real machine running the test case. Only if you are running on a real machine can you really see the problem. 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.
4.1 How to run test cases on a real machine
One, turn on the phone's 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 4.1:
Figure 4.1 View phone information
If there is an output, the connection is successful.
Three, start the 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.
Figure 4.2 starting the Appium service
when the program outputs such as information, indicating that the Appium started successfully, you can run the test script at this time.
Four, Install the App you want to test
Of course we need to install the tested app on the test machine, go online to download the latest crowdfunding app, and then install it on the test machine. Note Be sure to install the version of the app, the version of the phone system, and the API version of the Android SDK is the same or compatible.
Five. 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 (Ten)
#点击 "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 (Ten)
#此处加上检测登录是否成功的代码
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. After you open the app, enter your username and password, then stop the app for 10 seconds after you enter the app.
Note: To install Appium Settings on your phone while the test case is running, follow the prompts to allow installation.
4.2problems encountered when the real machine is 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 the input will open the soft keyboard, and the layout above the soft keyboard is nine Gongge, click has been wrong.
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!
4.3Summary of this chapter
In this chapter we explain how to run test cases on a real phone, and of course we still use the login for crowdfunding app as an example. By stepping through, we can run the test cases on the real machine step-by-point, which is what we are prepared to do before we actually start our automated test Case authoring path. Environment, Appium API and real machine running methods after we have all explained, we start with the next chapter and explain how the specific test cases should begin.
Fourth Chapter Appium Real machine running test case explanation