1.创建一个case包,start_app的python文件
#coding=utf-8
from appium import webdriver
from util.read_init import ReadIni
from util.get_by_local import GetByloacl
import time
def get_driver():
desc={
"platformName":"Android",
"platformVersion":"5.1.1",
"deviceName":"emulator-5554",
"appPackage":"com.tencent.mobileqq",
"appActivity":".activity.SplashActivity",
"noReset":"True"
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desc)
return driver
def get_click():
get_by_local=GetByloacl(driver)
get_by_local.get_element('click').click()
time.sleep(1)
get_by_local.get_element('username').send_keys('11111')
get_by_local.get_element('password').send_keys('22222')
driver=get_driver()
get_click()
----------------------------------------------------------------------------------------------
2.创建一个config目录,text1.ini文件
[login_element]
click=id>com.tencent.mobileqq:id/btn_login
username=classname>android.widget.EditText
password=id>com.tencent.mobileqq:id/password
login_button=loginbutton
[course_element]
course=price
----------------------------------------------------------------------------------------------------------------------------------------
3.创建util包,创建get_by_loacl的python文件 读取配置文件
#coding=utf-8
from util.read_init import ReadIni
class GetByloacl:
def __init__(self,driver):
self.driver=driver
def get_element(self,key):
read_ini = ReadIni()
local=read_ini.get_value(key)
if local!=None:
by=local.split('>')[0]
local_by=local.split('>')[1]
if by=='id':
return self.driver.find_element_by_id(local_by)
elif by=='classname':
return self.driver.find_element_by_class_name(local_by)
else:
return self.driver.find_element_by_xpath(local_by)
else:
return None
(2)封装定位信息的python read_init文件
#coding=utf-8
import configparser
class ReadIni():
def __init__(self,file_path=None):
if file_path==None:
self.file_path=r'E:\Users\00\PycharmProjects\untitled\config\text1.ini'
else:
self.file_path=file_path
self.data=self.read_ini()
def read_ini(self):
read_ini = configparser.ConfigParser()
read_ini.read(r'E:\Users\00\PycharmProjects\untitled\config\text1.ini')
return read_ini
def get_value(self,key,section=None):
if section==None:
section='login_element'
try:
value = self.data.get(section, key)
except:
value=None
return value
if __name__=='__main__':
read_ini=ReadIni()
print(read_ini.get_value("course","course_element"))
--------------------------------------------------------------------------------------------------------------------------------------------
创建一个doc_cmd文件 封装执行命令方法
#coding=utf-8
import os
class DosCmd:
def excute_cmd_result(self,command):
result_list = []
result = os.popen(command).readlines()
for i in result:
if i =='\n':
continue
result_list.append(i.strip('\n'))
return result_list
def excute_cmd(self,command):
os.system(command)
if __name__ == '__main__':
dos = DosCmd()
print (dos.excute_cmd_result('adb devices'))
---------------------------------------------------------------------------------------------------------------------------
#coding=utf-8
from util.doc_cmd import DosCmd
class Port:
def port_is_used(self,port_num):
'''
判断端口是否被占用
'''
flag = None
self.dos = DosCmd()
command = 'netstat -ano | findstr '+str(port_num)
result = self.dos.excute_cmd_result(command)
if len(result)>0:
flag = True
else:
flag = False
return flag
def create_port_list(self,start_port,device_list):
'''start_port 4701
生成可用端口
@parameter start_port
@parameter device_list
'''
port_list = []
if device_list != None:
while len(port_list) != len(device_list):
if self.port_is_used(start_port) != True:
port_list.append(start_port)
start_port = start_port +1
return port_list
else:
print ("生成可用端口失败")
return None
if __name__ == '__main__':
port = Port()
li = [1,2,3,4,5]
print (port.create_port_list(4722,li))
创建一个server文件 多线程启动appium服务端
#coding=utf-8
from util.doc_cmd import DosCmd
from util.port import Port
import threading
class Server:
def get_devices(self):
self.dos =DosCmd()
devices_list = []
result_list = self.dos.excute_cmd_result('adb devices')
if len(result_list)>=2:
for i in result_list:
if 'List' in i:
continue
devices_info = i.split('\t')
if devices_info[1]=='device':
devices_list.append(devices_info[0])
return devices_list
else:
return None
def create_port_list(self,start_port):
port=Port()
port_list=port.create_port_list(start_port,self.get_devices())
return port_list
def create_commed_list(self):
command_list=[]
appium_port_list=self.create_port_list(4700)
bootstrap_port_list=self.create_port_list(4900)
device_list=self.get_devices()
for i in range(len(device_list)):
command='appium -p '+str(appium_port_list[i])+" -bp "+str(bootstrap_port_list[i])+" -U "+device_list[i]+" --no-reset --session-override"
command_list.append(command)
return command_list
def start_server(self,i):
self.start_list=self.create_commed_list()
self.dos.excute_cmd(self.start_list[i])
def main(self):
for i in range(len(self.create_commed_list())):
appium_start=threading.Thread(target=self.start_server,args=(i,))
appium_start.start()
if __name__ == '__main__':
server=Server()
# print(server.create_commed_list())
# print(server.start_server())
print(server.main())
-------------------------------------------------------------------------------------------------------------------
4.创建page包,创建一个 login_page的python文件
#coding=utf-8
from util.get_by_local import GetByloacl
class LoginPage:
def __init__(self,driver):
self.get_by_local = GetByloacl(driver)
def get_username_element(self):
return self.get_by_local.get_element('username')
def get_password_element(self):
return self.get_by_local.get_element('password')
def get_login_elemnt(self):
return self.get_by_local.get_element('click')
----------------------------------------------------------------------------------------------------------------------------
5.创建一个handle包,login_handle的python文件
#coding=utf-8
from page.login_page import LoginPage
class Login_handle():
def __init__(self,driver):
self.login_page=LoginPage(driver)
def send_username(self,user):
self.login_page. get_username_element().send_keys(user)
def send_password(self,password):
self.login_page.get_password_element().send_keys(password)
def send_click(self):
self.login_page.get_login_elemnt().click()
---------------------------------------------------------------------------------------------------------------------------------------
6.创建bussiness包,一个 login_bussiness的python文件
#coding=utf-8
from handle.login_handle import Login_handle
class Login_bussiness:
def __init__(self,driver):
self.login_handle=Login_handle(driver)
def login_pass(self):
self.login_handle.send_username('11111')
self.login_handle.send_password('22222')
self.login_handle.send_click()
def login_user_error(self):
self.login_handle.send_username('12111')
self.login_handle.send_password('25222')
self.login_handle.send_click()