def getScreenShot(self, module): """Screen shot """ time_now = self.getTime() root_dir = os.path.dirname(os.path.dirname(__file__)) image_file = root_dir + '/screenshots/' + '{}_{}.png'.format( module, time_now) logging.info("=====Get {} screen shot=====".format(module)) self.driver.get_screenshot_as_file(image_file) logging.info("======Screen shot end=====")
def check_account_alert(self): """check if there is user logoff info""" logging.info("=====check account logoff alert=====") try: element = self.driver.find_element(*self.commitBtn) except NoSuchElementException: pass else: logging.info("=====click commitBtn=====") element.click()
def check_market_ad(self): """check advertisement""" logging.info("=====Check advertisement=====") try: element = self.driver.find_element(*self.wemedia_cacel) except NoSuchElementException: pass else: logging.info("=====Close market ad====") element.click()
def test_user_register(self): logging.info("=====test_user_register=====") register = RegisterView(self.driver) username = '******' + str(random.randint(5000, 8000)) password = '******' + str(random.randint(5000, 8000)) email = 'george' + str(random.randint(5000, 8000)) + '@qq.com' logging.info(register.register_action(username, password, email)) # self.assertTrue(register.register_action(username, password, email)) print(register.che_register_status()) self.assertEqual(register.che_register_status(), True)
def check_skipBtn(self): """ to skip advertising :return: """ try: skip_btn = self.find_element(*self.skip_loc) except NoSuchElementException: logging.info("=====skip button does not exist in page =====") else: skip_btn.click() logging.info("=====Click skip btn====")
def check_cancelBtn(self): """ if there is a update button cancel it then :return: """ try: cancel_btn = self.find_element(*self.cancel_loc) except NoSuchElementException: logging.info("=====cancel button does not exist in page =====") else: cancel_btn.click() logging.info("=====Click Cancel btn====")
def test_login2_passwdError(self): """username is ok, passwd is error!""" logging.info("=====test login george end=====") print("test_login2_passwdError is start run...") po = LoginPage(self.driver) po.Login_action("george", "123456") sleep(5) # 断言与截图 登陆失败时按照定位器得到的值为"" self.assertEqual(po.type_loginFail_hint(), '用户不存在!') insert_img(self.driver, "login2_fail.png") logging.info("=====test login {name} end=====".format(name="george")) print("test_login2_passwdError test end...")
def test_login1_normal(self): """username password is normal""" logging.info("=====test login pxadmin begin=====") # print("test_login1_normal is start run...") po = LoginPage(self.driver) po.Login_action("pxadmin", "111111") sleep(5) self.driver.get_screenshot_as_file(screenshots_dir + "\\test.png") # 断言与截屏 self.assertEqual(po.type_loginPass_hint(), "Home") # 截图失败 # insert_img(self.driver, "login_normal.png") logging.info("=====test login {name} end=====".format(name="pxadmin"))
def check_login_status(self): """check login status""" logging.info("=====check login status=====") self.check_market_ad() self.check_account_alert() try: self.driver.find_element(*self.button_myself).click() self.driver.find_element(*self.username) except NoSuchElementException: logging.error('login Fail!') self.getScreenShot('login Fail') return False else: logging.info("=====login success=====") # to logoff for next test case every time login then logoff self.logout_action() return True
def logout_action(self): """logout action""" try: element = self.driver.find_element(*self.settingBtn) except NoSuchElementException: pass else: logging.info("=====click setting button=====") element.click() try: element = self.driver.find_element(*self.logoutBtn) except NoSuchElementException: pass else: logging.info("=====click logout=====") element.click() try: element = self.driver.find_element(*self.tip_commit) except NoSuchElementException: pass else: logging.info("=====click commit button=====") element.click()
def get_driver(): """ :return: driver instance """ base_dir = os.path.dirname(os.path.dirname(__file__)) print(base_dir) app_path = '/'.join([base_dir, 'app', 'kaoyan3.1.0.apk']) print(app_path) config_file = '/'.join([base_dir, 'config', 'desired_capability.yaml']) print(config_file) with open(config_file, 'r', encoding='utf-8') as f: config = yaml.load(f) desired_capability = { 'platformName': config['platformName'], 'deviceName': config['deviceName'], 'app': config['app'], 'appPackage': config['appPackage'], 'appActivity': config['appActivity'], # to recover previous settings 'noReset': config['noReset'], # to set unicode if used chinese 'unicodeKeyboard': config['unicodeKeyboard'], # reset keyboard 'resetKeyboard': config['resetKeyboard'] } url = 'http' + '://' + str(config['ip']) + ":" + str( config['port']) + '/wd/hub' logging.info("=====Begin to connect phone=====") driver = webdriver.Remote(url, desired_capability) logging.info("=====starting app,please wait=====") driver.implicitly_wait(3) logging.info("=====wait to page refresh=====") return driver
def login(self, username, password): self.check_cancelBtn() self.check_skipBtn() sleep(1) try: username_input = self.find_element(*self.username_loc) except NoSuchElementException: logging.info("=====account input does not exist in page =====") else: username_input.send_keys(username) logging.info("=====input username:{}====".format(username)) try: password_input = self.find_element(*self.password_loc) except NoSuchElementException: logging.info("=====password input does not exist in page =====") else: password_input.send_keys(password) logging.info("=====input password:{}====".format(password)) try: login_btn = self.find_element(*self.click_loc) except NoSuchElementException: logging.info("=====login btn does not exist in page =====") else: login_btn.click() logging.info("=====click to login====") try: into_btn = self.find_element(*self.into_loc) except NoSuchElementException: logging.info("=====into info does not exist in page =====") else: into_btn.click() logging.info("=====Welcome to kaoyanbang====")
def test_login_error(self): logging.info("=====test login with error account=====") login_action = LoginView(self.driver) row = login_action.get_csv_data(self.csv_file, 3) login_action.login(row[0], row[1]) self.assertEqual(login_action.check_login_status(), False)
def test_login_6421(self): logging.info("=====test login george6421=====") login_action = LoginView(self.driver) row = login_action.get_csv_data(self.csv_file, 2) login_action.login(row[0], row[1]) self.assertEqual(login_action.check_login_status(), True)
def tearDown(self): logging.info("=====tearDown=====") sleep(5) self.driver.close_app()
def setUp(self): logging.info("=====setUp=====") self.driver = get_driver()