# 登录方法的封装 from appium import webdriver from time import sleep from python_selenium.Slide import swipeLeft def login(user,pwd): caps = { "platformName": "Android", "deviceName": "127.0.0.1:21503", "platformVersion": "5.1.1", "appPackage": "com.yw.yzz", "appActivity": "com.yw.yzz.biz.splash.SplashActivity", "unicodeKeyboard": "True", "resetKeyboard": "True", "noReset": "True" } dr = webdriver.Remote("http://127.0.0.1:4723/wd/hub",caps) dr.implicitly_wait(10) sleep(3) try: dr.find_element_by_id("com.yw.yzz:id/iv_img") sleep(2) except: dr.find_element_by_id("com.yw.yzz:id/seting_tab").click() else: swipeLeft(dr,3,1000) sleep(1) dr.find_element_by_id("com.yw.yzz:id/btn_open").click() sleep(4) dr.find_element_by_id("com.yw.yzz:id/seting_tab").click() sleep(1) dr.find_element_by_xpath('//android.widget.TextView[@resource-id="com.yw.yzz:id/tv_name"]').click() el2 = dr.find_element_by_id("com.yw.yzz:id/et_phone") el2.send_keys(user) el3 = dr.find_element_by_id("com.yw.yzz:id/et_password") el3.send_keys(pwd) el4 = dr.find_element_by_id("com.yw.yzz:id/btn_login") sleep(1) el4.click() try: dr.find_element_by_id("com.yw.yzz:id/tv_phone") except: raise AssertionError else: pass return dr
# 滑动方法封装 from time import sleep def swipeLeft(dr,n,t): width = dr.get_window_size()["width"] height = dr.get_window_size()["height"] for i in range(n): dr.swipe(width*0.9,height/2,width*0.1,height/2,t) sleep(1) def swipeUp(dr,n,t): width = dr.get_window_size()["width"] height = dr.get_window_size()["height"] for i in range(n): dr.swipe(width*0.5,height*0.9,width*0.5,height*0.1,t) sleep(1)
# 用例输出 from python_selenium.yyt_login import login from time import sleep import unittest class YYT(unittest.TestCase): def setUp(self): self.driver = login("1777982xxxx","xxxxxxx") sleep(1) el1 = self.driver.find_element_by_id("com.yw.yzz:id/iv_btn_setting") el1.click() def test_ChangeName(self): el2 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout") el2.click() el3 = self.driver.find_element_by_id("com.yw.yzz:id/ll_updateinfo_name") el3.click() el4 = self.driver.find_element_by_id("com.yw.yzz:id/et_my_changemyname") el4.clear() el4.send_keys("xxxx") el5 = self.driver.find_element_by_id("com.yw.yzz:id/top_right_tv") el5.click() def test_ChangeSex(self): self.driver.tap([(0,131),(1080,281)],500) sleep(1) self.driver.find_element_by_id("com.yw.yzz:id/ll_updateinfo_gender").click() sleep(1) self.driver.find_element_by_id("com.yw.yzz:id/ll_choosegender_secret").click() def tearDown(self): self.driver.back() sleep(1) el7 = self.driver.find_element_by_id("com.yw.yzz:id/exit_btn") el7.click() el8 = self.driver.find_element_by_id("com.yw.yzz:id/sure_button") el8.click() sleep(1) self.driver.quit() if __name__ == '__main__': unittest.main()
# 批量运行用例及生成测试报告 import unittest import HTMLTestRunner path = "D:\work_doc\CodeFile\python_selenium" testcase = unittest.defaultTestLoader.discover(path,pattern="case.py",top_level_dir=None) YYTtestreport = "d:\testreport.html" with open(YYTtestreport,"wb") as YYT: runner = HTMLTestRunner.HTMLTestRunner(YYT,title="测试报告",description="详细报告") runner.run(testcase)