How to use capture_screenshots method in toolium

Best Python code snippet using toolium_python

AndroidDriver.py

Source:AndroidDriver.py Github

copy

Full Screen

...54 """定义一个静态方法,用于输出标准的错误信息"""55 print("Exception in AndroidDriver:")56 print('-' * 30 + '错误信息请看下方' + '-' * 30)57 print(traceback.print_exc(file=sys.stdout))58 def capture_screenshots(self, file_path=None):59 if file_path is None:60 # if not os.path.exists("./screen_shots"):61 if not os.path.exists("./sdk_screen_shots"):62 # 判断截图目录是否存在,不存在则创建63 os.makedirs("./sdk_screen_shots")64 gamename = self.ap.split('.')[-1]65 path = './sdk_screen_shots/'+gamename+'_%s.png' % time.strftime("%Y-%m-%d %H-%M-%S")66 # path = './sdk_screen_shots/sszg_%s.png' % time.strftime("%Y-%m-%d %H-%M-%S")67 self.driver.get_screenshot_as_file(filename=path)68 if os.path.exists(path):69 return path70 else:71 return False72 else:73 self.driver.get_screenshot_as_file(file_path)74 if os.path.exists(file_path):75 return file_path76 else:77 return False78 def is_element_appearance(self, locator):79 """目的:检查元素是否出现80 查找步骤:1、先用id找, 2、然后再用xpath81 """82 try:83 self.driver.find_element_by_id(locator)84 return True85 except NoSuchElementException:86 pass87 # try:88 # self.driver.find_element_by_xpath(locator)89 # return True90 # except NoSuchElementException:91 # pass92 # try:93 # self.driver.find_element_by_accessibility_id(locator)94 # return True95 # except NoSuchElementException:96 # pass97 return False98 def type_by_id(self, locator, text):99 """根据id找到要输入的位置,并向其发送文本"""100 try:101 self.driver.find_element_by_id(locator).clear()102 self.driver.find_element_by_id(locator).send_keys(text)103 return True104 except Exception as e:105 AndroidDriver.print_exception_message()106 return False107 def type_by_xpath(self, locator, text):108 """根据xpath找到要输入的位置,并向其发送文本"""109 try:110 self.driver.find_element_by_xpath(locator).send_keys(text)111 return True112 except Exception as e:113 AndroidDriver.print_exception_message()114 return False115 def click_by_id(self, locator):116 """根据id来点击117 1、先使用 find_element_by_id118 2、再使用 find_element_by_accessibility_id"""119 try:120 self.driver.find_element_by_id(locator).click()121 return True122 except Exception:123 pass124 try:125 self.driver.find_element_by_accessibility_id(locator).click()126 return True127 except Exception as e:128 pass129 return False130 def click_by_xpath(self, locator):131 """点击xpath"""132 try:133 self.driver.find_element_by_xpath(locator).click()134 return True135 except Exception as e:136 AndroidDriver.print_exception_message()137 return False138 def swipe_to_down(self, duration=200):139 """向下滑动的操作"""140 # 注释141 driver = self.driver142 starty = driver.get_window_size()['height'] * 4/5143 # print(starty)144 endy = driver.get_window_size()['height'] * 1/5145 x = driver.get_window_size()['width'] * 1/2146 try:147 self.driver.swipe(x, starty, x, endy, duration)148 return True149 except Exception:150 # 如何出现异常,就转到这里151 AndroidDriver.print_exception_message()152 return False153 #快速游戏方法154 def quick_register(self):155 #点击快速游戏按钮156 try:157 # self.driver.implicitly_wait(20)158 time.sleep(3)159 self.click_by_id('快速游戏')160 return True161 except Exception:162 AndroidDriver.print_exception_message()163 return False164 #手机登录方法165 def phone_login(self):166 try:167 # 点击手机登录按钮168 self.driver.implicitly_wait(20)169 self.click_by_id('手机登录')170 self.capture_screenshots()171 # 输入手机号码172 phone = '18024179019'173 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_phone',phone)174 self.capture_screenshots()175 # 点击下一步176 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_btn_next')177 self.capture_screenshots()178 # 输入验证码179 self.driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.TextView[1]').send_keys('000000')180 # 点击登录181 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_btn_login')182 return True183 except Exception:184 AndroidDriver.print_exception_message()185 return False186 #账号登录187 def account_login(self):188 try:189 self.driver.implicitly_wait(20)190 self.click_by_id('账号登录')191 time.sleep(2)192 username = 'lshbnbn'193 password = '123456'194 # 输入账号195 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_username', username)196 # 输入密码197 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_password', password)198 time.sleep(2)199 # 点击登录按钮200 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/yh_btn_sure')201 self.capture_screenshots()202 return True203 except Exception:204 return False205 #账号注册206 def account_register(self):207 try:208 self.driver.implicitly_wait(20)209 self.click_by_id('账号登录')210 self.capture_screenshots()211 # 点击账号注册按钮212 self.click_by_xpath(213 '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout/android.widget.TextView[2]')214 self.capture_screenshots()215 # 输入账号216 username = 'bnuser' + str(random.randint(1000, 10000000))217 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_username', username)218 self.capture_screenshots()219 # 输入密码220 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_password', 'bn123456')221 # 点击注册按钮222 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/yh_btn_sure')223 self.capture_screenshots()224 return True225 except Exception:226 return False227 #用户协议、隐私协议,忘记密码228 def forget_password(self):229 try:230 self.driver.implicitly_wait(20)231 self.click_by_id('账号登录')232 self.capture_screenshots()233 # # 点击用户隐私协议按钮234 # self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_tv_agreement')235 # self.capture_screenshots()236 # # 用户隐私协议页面点击关闭按钮237 # self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/yh_iv_close')238 # self.capture_screenshots()239 #点击账号注册按钮240 self.click_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout/android.widget.TextView[2]')241 # 点击忘记密码按钮242 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_tv_forget')243 # 输入手机号244 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_phone','18024179019')245 # 点击获取验证码按钮246 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_btn_verify')247 # 输入验证码248 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_verify', '000000')249 self.capture_screenshots()250 # 输入密码251 self.type_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_edt_password', 'bn123456')252 # 点击修改密码按钮253 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_btn_login')254 self.capture_screenshots()255 return True256 except Exception:257 return False258 # #token登录259 def token_login(self):260 # self.driver.implicitly_wait(20)261 time.sleep(5)262 self.driver.switch_to.context(self.driver.contexts[0])263 self.capture_screenshots()264 # while True:265 # if self.is_element_appearance(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_btn_login')==True:266 self.click_by_id(self.bnsdk_app_desired_caps['appPackage'] + ':id/bn_btn_login')267 # break268 # else:269 # break270 # #使用其他账号登录271 def other_login(self):272 try:273 # self.driver.implicitly_wait(20)274 time.sleep(5)275 self.driver.switch_to.context(self.driver.contexts[0])276 time.sleep(5)277 self.click_by_id(self.bnsdk_app_desired_caps["appPackage" ] + ':id/bn_tv_other')278 return True279 except Exception:280 return False281 #下次再说按钮,绑定手机282 def next_time(self):283 self.capture_screenshots()284 while True:285 time.sleep(2)286 if self.is_element_appearance(self.bnsdk_app_desired_caps['appPackage'] + ':id/yh_btn_left') == True:287 self.click_by_id(self.bnsdk_app_desired_caps['appPackage']+':id/yh_btn_left')288 break289 else:290 break291 #悬浮窗按钮,点击悬浮窗,点击退出登录操作292 def xfc(self):293 time.sleep(10)294 y = 1030295 try:296 while True:297 if y in range(1030, 1980):298 # if y in range(1230, 1780):299 y += 80300 TouchAction(self.driver).tap(x=14, y=y).perform()301 print('点击悬浮窗')302 else:303 break304 time.sleep(2)305 self.capture_screenshots()306 return True307 except Exception:308 return False309 #点击个人中心退出登录按钮310 def lagout(self):311 try:312 # self.driver.implicitly_wait(20)313 time.sleep(3)314 self.capture_screenshots()315 self.click_by_xpath('//*[@id="user-center"]/div/a')316 # self.click_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[1]/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[2]/android.view.View[3]')317 return True318 except Exception:319 return False320 #获取权限按钮321 def get_permission(self):322 self.driver.implicitly_wait(20)323 while True:324 # if self.is_element_appearance('android:id/button1') == True:325 if self.is_element_appearance('com.android.packageinstaller:id/permission_allow_button') == True:326 # self.driver.find_element_by_id('android:id/button1').click()327 self.driver.find_element_by_id('com.android.packageinstaller:id/permission_allow_button').click()328 self.capture_screenshots()329 time.sleep(2)330 if self.is_element_appearance('com.android.packageinstaller:id/permission_allow_button') == False:331 break332 #H5个人中心,实名认证功能333 def real_name(self):334 try:335 # self.driver.implicitly_wait(20)336 self.driver.switch_to.context(self.driver.contexts[1])337 time.sleep(2)338 #实名认证339 self.click_by_xpath('//*[@id="user-center"]/a[3]/div[1]/span[2]')#点击实名认证按钮,跳转到实名认证页面340 self.type_by_xpath('//*[@id="layout"]/div[1]/div[1]/input','张三')#输入姓名341 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/input','440514199708087833')#输入身份证342 time.sleep(2)343 self.capture_screenshots()344 self.click_by_xpath('//*[@id="layout"]/div[1]/a')#点击实名认证按钮345 return True346 except Exception:347 AndroidDriver.print_exception_message()348 return False349 #H5个人中心,修改密码350 def change_password(self):351 try:352 # self.driver.implicitly_wait(20)353 # self.driver.switch_to.context(self.driver.contexts[1])354 time.sleep(2)355 self.click_by_xpath('//*[@id="user-center"]/a[1]/div[1]/span[2]')356 time.sleep(2)357 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/input','bn123456')358 time.sleep(2)359 self.type_by_xpath('//*[@id="layout"]/div[1]/div[3]/input','bn123456')360 time.sleep(2)361 # self.capture_screenshots()362 time.sleep(3)363 self.click_by_xpath('//*[@id="layout"]/div[1]/a')364 return True365 except Exception:366 AndroidDriver.print_exception_message()367 return False368 #H5个人中心绑定手机369 def bind_phone(self):370 try:371 # self.driver.implicitly_wait(20)372 # time.sleep(5)373 # self.driver.switch_to.context(self.driver.contexts[1])374 time.sleep(2)375 # self.driver.refresh()376 self.click_by_xpath('//*[@id="user-center"]/a[2]/div[1]/span[2]')377 self.type_by_xpath('//*[@id="layout"]/div[1]/div[1]/input','18024179019')378 self.click_by_xpath('//*[@id="layout"]/div[1]/div[2]/button')379 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/div/input','000000')380 time.sleep(2)381 self.click_by_xpath('//*[@id="layout"]/div[1]/a')382 time.sleep(2)383 self.capture_screenshots()384 return True385 except Exception:386 AndroidDriver.print_exception_message()387 return False388 #H5个人中心解绑手机389 def unbind_phone(self):390 try:391 # self.driver.implicitly_wait(20)392 # self.driver.switch_to.context(self.driver.contexts[1])393 time.sleep(2)394 # 点击账户按钮395 self.click_by_xpath('//*[@id="nav-bar"]/a[1]')396 time.sleep(2)397 self.click_by_xpath('//*[@id="user-center"]/a[2]/div[1]/span[2]')398 self.click_by_xpath('//*[@id="layout"]/div[1]/div[2]/button')399 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/div/input', '000000')400 time.sleep(2)401 self.click_by_xpath('//*[@id="layout"]/div[1]/a')402 # print(self.driver.current_context)403 time.sleep(2)404 self.capture_screenshots()405 return True406 except Exception:407 AndroidDriver.print_exception_message()408 return False409 #客服界面410 def customer_service(self):411 try:412 self.driver.implicitly_wait(20)413 self.driver.switch_to.context(self.driver.contexts[1])414 time.sleep(10)415 # 点击个人中心客服按钮416 self.click_by_xpath('//*[@id="nav-bar"]/a[3]')417 # 点击QQ交谈418 self.click_by_xpath('//*[@id="server"]/div[1]/a')419 time.sleep(2)420 # 点击物理返回键421 self.driver.press_keycode(4)422 time.sleep(2)423 # 点击拨打电话424 self.click_by_xpath('//*[@id="server"]/div[2]/a')425 time.sleep(2)426 # 点击物理返回键427 self.driver.press_keycode(4)428 self.driver.press_keycode(4)429 time.sleep(2)430 # 点击拷贝账号431 self.click_by_xpath('//*[@id="copy-wechat"]')432 return True433 except Exception:434 AndroidDriver.print_exception_message()435 return False436 #论坛437 def forum(self):438 try:439 self.driver.implicitly_wait(20)440 self.driver.switch_to.context(self.driver.contexts[1])441 time.sleep(2)442 self.click_by_xpath('//*[@id="nav-bar"]/a[2]')#点击个人中心论坛按钮443 time.sleep(3)444 # self.capture_screenshots()445 time.sleep(2)446 # self.driver.close_app()447 # time.sleep(3)448 # self.first()449 self.driver.press_keycode(4)450 return True451 except Exception:452 return False453 #客服、解绑、绑定454 def ke_bi_fo(self):455 try:456 self.driver.implicitly_wait(20)457 self.driver.switch_to.context(self.driver.contexts[1])458 time.sleep(2)459 # 点击个人中心客服按钮460 self.click_by_xpath('//*[@id="nav-bar"]/a[3]')461 # 点击QQ交谈462 self.click_by_xpath('//*[@id="server"]/div[1]/a')463 time.sleep(2)464 # 点击物理返回键465 self.driver.press_keycode(4)466 time.sleep(2)467 # 点击拨打电话468 self.click_by_xpath('//*[@id="server"]/div[2]/a')469 time.sleep(2)470 # 点击物理返回键471 self.driver.press_keycode(4)472 self.driver.press_keycode(4)473 time.sleep(2)474 # 点击拷贝账号475 self.click_by_xpath('//*[@id="copy-wechat"]')476 time.sleep(2)477 self.click_by_xpath('//*[@id="nav-bar"]/a[1]')478 #点击账户按钮479 self.click_by_xpath('//*[@id="nav-bar"]/a[3]')480 #解绑、绑定481 time.sleep(2)482 self.click_by_xpath('//*[@id="user-center"]/a[2]/div[1]/span[2]')483 self.click_by_xpath('//*[@id="layout"]/div[1]/div[2]/button')484 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/div/input', '000000')485 self.click_by_xpath('//*[@id="layout"]/div[1]/a')486 time.sleep(2)487 self.capture_screenshots()488 time.sleep(2)489 self.click_by_xpath('//*[@id="user-center"]/a[2]/div[1]/span[2]')490 self.type_by_xpath('//*[@id="layout"]/div[1]/div[1]/input', '18024179019')491 self.click_by_xpath('//*[@id="layout"]/div[1]/div[2]/button')492 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/div/input', '000000')493 self.click_by_xpath('//*[@id="layout"]/div[1]/a')494 time.sleep(2)495 self.capture_screenshots()496 return True497 except Exception:498 AndroidDriver.print_exception_message()499 return False500 #实名认证、改密码501 def re_cha(self):502 try:503 self.driver.implicitly_wait(20)504 time.sleep(5)505 self.driver.switch_to.context(self.driver.contexts[1])506 time.sleep(2)507 #实名认证508 self.click_by_xpath('//*[@id="user-center"]/a[2]/div[1]/span[2]')509 self.type_by_xpath('//*[@id="layout"]/div[1]/div[1]/input','18024179019')510 self.click_by_xpath('//*[@id="layout"]/div[1]/div[2]/button')511 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/div/input','000000')512 self.click_by_xpath('//*[@id="layout"]/div[1]/a')513 time.sleep(2)514 self.capture_screenshots()515 #改密码516 time.sleep(2)517 self.click_by_xpath('//*[@id="user-center"]/a[1]/div[1]/span[2]')518 self.type_by_xpath('//*[@id="layout"]/div[1]/div[2]/input', 'bn123456')519 self.type_by_xpath('//*[@id="layout"]/div[1]/div[3]/input', 'bn123456')520 time.sleep(2)521 self.capture_screenshots()522 self.click_by_xpath('//*[@id="layout"]/div[1]/a')523 return True524 except Exception:525 AndroidDriver.print_exception_message()...

Full Screen

Full Screen

conftest.py

Source:conftest.py Github

copy

Full Screen

...39 if "[" in case_path:40 case_name = case_path.split("-")[0] + "].png"41 else:42 case_name = case_path43 capture_screenshots(case_name)44 img_path = "image/" + case_name.split("/")[-1]45 if img_path:46 html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \47 'onclick="window.open(this.src)" align="right"/></div>' % img_path48 extra.append(pytest_html.extras.html(html))49 report.extra = extra50def description_html(desc):51 """52 将用例中的描述转成HTML对象53 :param desc: 描述54 :return:55 """56 if desc is None:57 return "No case description"58 desc_ = ""59 for i in range(len(desc)):60 if i == 0:61 pass62 elif desc[i] == '\n':63 desc_ = desc_ + ";"64 else:65 desc_ = desc_ + desc[i]66 67 desc_lines = desc_.split(";")68 desc_html = html.html(69 html.head(70 html.meta(name="Content-Type", value="text/html; charset=latin1")),71 html.body(72 [html.p(line) for line in desc_lines]))73 return desc_html74def capture_screenshots(case_name):75 """76 配置用例失败截图路径77 :param case_name: 用例名78 :return:79 """80 global driver81 file_name = case_name.split("/")[-1]82 if RunConfig.NEW_REPORT is None:83 raise NameError('没有初始化测试报告目录')84 else:85 image_dir = os.path.join(RunConfig.NEW_REPORT, "image", file_name)86 RunConfig.driver.save_screenshot(image_dir)87# 启动浏览器88@pytest.fixture(scope='session', autouse=True)89def browser():90 """91 全局定义浏览器驱动92 :return:93 """94 global driver95 if RunConfig.driver_type == "chrome":96 # 本地chrome浏览器97 driver = webdriver.Chrome()98 driver.maximize_window()99 elif RunConfig.driver_type == "firefox":100 # 本地firefox浏览器101 driver = webdriver.Firefox()102 driver.maximize_window()103 elif RunConfig.driver_type == "chrome-headless":104 # chrome headless模式105 chrome_options = CH_Options()106 chrome_options.add_argument("--headless")107 chrome_options.add_argument('--disable-gpu')108 # chrome_options.add_argument("--window-size=1920x1080")109 driver = webdriver.Chrome(options=chrome_options)110 elif RunConfig.driver_type == "firefox-headless":111 # firefox headless模式112 firefox_options = FF_Options()113 firefox_options.headless = True114 driver = webdriver.Firefox(firefox_options=firefox_options)115 elif RunConfig.driver_type == "grid":116 # 通过远程节点运行117 driver = Remote(command_executor='http://localhost:4444/wd/hub',118 desired_capabilities={119 "browserName": "chrome",120 })121 driver.set_window_size(1920, 1080)122 else:123 raise NameError("driver驱动类型定义错误!")124 RunConfig.driver = driver125 return driver126# 关闭浏览器127@pytest.fixture(scope="session", autouse=True)128def browser_close():129 yield driver130 driver.quit()131 print("test end!")132if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run toolium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful