Appium+python app automation test scripts start and stop Appium services

Source: Internet
Author: User
Tags appium selenium grid

Studying the automated testing of the Appium Android app for some time, the need to connect multiple mobile terminals to execute test cases simultaneously, I realized by getting the number of device IDs that needed to execute the use case and the number of devices actually connected (obtained through ADB devices). The corresponding number of Appium services is then initiated so that each device executes concurrently and does not affect each other. Of course, can also be achieved through the selenium grid, but currently in the study, or the current launch of a number of appium service implementation of the way to record.

One, start a single appium service under Windows

To start multiple Appium services, you must specify a port for each service.

The relevant parameters for starting the Appium service can be consulted in this blog post: http://www.cnblogs.com/xinleishare/p/4793538.html

127.0. 0.1 4726 4780 " E:/appium "  -

By using this command to start a port for the 4726,bootstrap port for the 4780,appium log storage path for the e-Drive, the session can overwrite and the command times out for the 600s Appium service, the URL address for the access is:/http 127.0.0.1:4726/wd/hub.

Why specify the bootstrap port here? When you do not specify a bootstrap port, the default bootstrap port for the Appium service that is started is 4724. When we start two or more appium services at the same time, do not specify the bootstrap port, then all service bootstrap ports default to 4723, when connecting multiple mobile devices to start driver, some phones do not perform use cases, in order to stabilize, Specify the bootstrap port here separately.

Second, the Python script launches the Appium service

In order to start the corresponding number of Appium services based on the number of connected devices, the start of the Appium service is placed directly in the Python script. The way to do this is to execute the above cmd command line through a Python script.

1 defStart_appium (self, host, port, Bootstrap_port, Appium_log_path):#Device_uid,2         #appium-p 4723-bp 4724-u 22238e79--command-timeout3ErrorMsg =""4Appium_server_url =""5         Try:6             ifSelf.port_is_free (host,port):7cmd ='start/b appium-a'+ Host +'- P'+ STR (port) +'--bootstrap-port'+ STR (bootstrap_port) +'--session-override--log'+'"'+appium_log_path +'"--command-timeout'  #'-u ' + device_uid+8                 Printcmd9                 #p = subprocess. Popen (cmd, shell=true, stdout=subprocess. PIPE, Stderr=subprocess. PIPE) #stdout =pipe, Stderr=pipe)Tenp = subprocess.call (cmd, Shell=true,stdout=open ('E:/logs.log','W'), stderr=subprocess. STDOUT) One                 PrintP AAppium_server_url ='/ http'+ Host +':'+ STR (port) +'/wd/hub' -                 PrintAppium_server_url -             Else: the                 Print "port:%d is used!"%(port) -         exceptException, msg: -ErrorMsg =str (msg) -         returnAppium_server_url, ErrorMsg

Of course, after returning the Appium URL, you need to verify that the Appium service is actually started (you can access the launched URL via requests or view the port based on Netstat).

Note: cmd in the code uses "Start/b", which is used primarily to let the cmd command execute in the background without affecting the execution of the Python script. If you do not add "start/b", start the Appium service and stay in the Appium log state, will not return to execute the subsequent Python script.

Third, stop Appium service

When the use case finishes, close the current Appium service. The implementation is that the Python script calls bat to close the Appium service. The Python script passes the port of the Appium server into the bat, the bat script obtains its process PID based on the port number, and then gets the app name and closes through Taskkill.

The Stopappium.bat script is as follows:

@echo offsetlocal enabledelayedexpansionrem%1 incoming port number for/f "delims=  Tokens=1"%%i in (' Netstat-aon ^| findstr%1 ') (Set A=%%igoto js): jstaskkill/f/pid "!a:~71,5!" REM Pause>nul

The Python script executes as follows:

1 def Stop_appium (Self, appium_url): 2         ' Stopappium.bat%s '%(Self.get_port (appium_url))3         #print  cmd4         p = os.popen (cmd)5         print p.read ()   

Where Appium_url is the URL address of the Appium service that was started before, the Get_port method is to get the port in the URL.

Above is their own implementation of the start-up and stop Appium service methods, if there is wrong to write the hope that we point out, if there is a better way also hope to enlighten.

Appium+python app automation test scripts to start and stop Appium services

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.