#-*- coding: utf-8 -*-
importjsonimportunittestimporttimefrom appium importwebdriver#from HtmlTestRunner import HTMLTestRunner
from selenium.webdriver.support.ui importWebDriverWaitfrom selenium.webdriver.common.action_chains importActionChainsimportsys
reload(sys)
sys.setdefaultencoding('utf8')
startime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
argv=sys.argvprint(argv)if len(argv) >1:
waittime= argv[1] #批处理输入参数1
if len(argv) >2:
login=argv[2]print(type(login))
login=json.loads(login)if len(argv) >3:
noReset=argv[3]else:
noReset=Trueelse:
login={"username": "xxxxxx","userpwd": "xxxx","newsever": "http://IP:port/agent_client"}else:
waittime=5
classLotteryTests(unittest.TestCase):defsetUp(self):#初始化测试平台
desired_caps ={'platformName': 'Android', #移动操作平台
'platformVersion': '4.4.2', #移动操作系统版本
'newCommandTimeout': '3600', #自定义超时时间
'deviceName': '127.0.0.1:52001', #设备名称
'appPackage': 'com.dgcy.pasx', #测试APP的包名
'app': 'E://example_android.apk','appActivity': 'com.dgcy.ui.activity.StartTicketsActivity', #要从包中启动的Android活动的活动名称
'automationName': 'Appium', #自动化引擎
#'unicodeKeyboard':True,
#'resetKeyboard':True,
'noReset': noReset,'chromeOptions': {'androidProcess': 'com.dgcy.paysx'}
}print(startime+': 打开APP...'.decode("utf-8"))
self.driver= webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)defisElementExist(self, locator):"""locator: locator输入规则:元素属性名=元素属性值,如果是id则以"id=xxx",如果是css则以“css=xxx”等,例如:id=com.dgcy.paysx:id/monitor_t;css=div.popup-content;
xpath=//div[text()='删除成功'];name=myna;class=mmv;link=登录;
:return: 布尔值"""list=locator.split('=')
flag=True
driver=self.drivertry:if list[0] == "id":
driver.find_element_by_id(list[1])elif list[0] == "xpath":
driver.find_element_by_xpath(list[1])elif list[0] == "name":
driver.find_element_by_namne(list[1])elif list[0] == "css":
driver.find_element_by_css_selector(list[1])elif list[0] == "class":
driver.find_element_by_class_name(list[1])elif list[0] == "tag":
driver.find_element_by_tag_name(list[1])elif list[0] == "link":
driver.find_element_by_link_text(u"%s" % list[1])returnflagexcept:
flag=Falsereturnflagdef login(self): #打开app并判断是否自动登录
print("Start test1...open application")
time.sleep(2)#判断是否需要登录
driver =self.driver
assertelement=LotteryTests.isElementExist(self,"id=com.dgcy.paysx:id/monitor_t")if notassertelement:
loging= driver.find_element_by_id("com.dgcy.paysx:id/login_submit").texttry:assert '登录'.decode("utf-8") in loging #检测到登录按钮
print("检测到登录按钮,重新登录....".decode("utf-8"))#输入用户名
driver.find_element_by_id("com.dgcy.paysx:id/loginUserName").send_keys(login["username"])#输入密码
driver.find_element_by_id("com.dgcy.paysx:id/loginPwd").send_keys(login["userpwd"])#输入服务器设置
driver.find_element_by_id("com.dgcy.paysx:id/fwurl").send_keys(login["newsever"])
time.sleep(1)#点击登录
driver.find_element_by_id("com.dgcy.paysx:id/login_submit").click()exceptAssertionError as e:print("未检测到登录按钮....".decode("utf-8"))else:print("已登录在APP首页".decode("utf-8"))
time.sleep(2)def test_03_allTicket(self,timewait=waittime):#检测‘运营监控’
timewait=int(timewait)
driver=self.driver
assertelement= LotteryTests.isElementExist(self, "id=com.dgcy.paysx:id/monitor_t")ifassertelement:
firstPage= driver.find_element_by_id("com.dgcy.paysx:id/monitor_t").text#添加断言是否显示“运营监控”
try:assert '运营监控'.decode("utf-8") infirstPageprint("Login User is Right!")exceptAssertionError as e:print('loginUser is Error')
self.login()#非‘运营监控’则重新登录
else:
self.login()
driver.find_element_by_id("com.dgcy.paysx:id/monitor_t").click() #点击运营监控
WebDriverWait(driver, timewait).until(lambda x: x.find_element_by_id("com.dgcy.paysx:id/rb_all")) #等待显示所有数据
print("正在每隔".decode("utf-8")+str(timewait)+"s循环获取到所有设备的状态.....".decode("utf-8"))
driver.find_element_by_id('com.dgcy.paysx:id/rb_all').click() #点击‘全部’
whileTrue:
driver.find_element_by_id('com.dgcy.paysx:id/rb_all').click() #点击‘全部’
time.sleep(timewait)
assertelement= LotteryTests.isElementExist(self, "id=com.dgcy.paysx:id/monitor_t")if assertelement ==False:raise ValueError("elm:(com.dgcy.paysx:id/monitor_t) is not exist!")#print("等待了"+str(timewait)+"s,获取到所有设备的状态.....")
deftearDown(self):
self.driver.quit()if __name__ == '__main__':#构造测试套件
unittest.main()