How to use switch_to_latest_window method in Airtest

Best Python code snippet using Airtest

report.py

Source:report.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2import os3import airtest.report.report as report4LOGDIR = "log"5old_trans_screen = report.LogToHtml._translate_screen6old_trans_desc = report.LogToHtml._translate_desc7old_trans_code = report.LogToHtml._translate_code8screen_func = ["find_element_by_xpath", "find_element_by_id", "find_element_by_name", "assert_exist",9 "back", "forward", "switch_to_new_tab", "switch_to_previous_tab", "get",10 "click", "send_keys"]11second_screen_func = ["click", "send_keys"]12other_func = []13def new_trans_screen(self, step, code):14 trans = old_trans_screen(self, step, code)15 if "name" in step['data'] and step['data']["name"] in screen_func:16 screen = {17 "src": None,18 "rect": [],19 "pos": [],20 "vector": [],21 "confidence": None,22 }23 src = ""24 if step["data"]["name"] in second_screen_func:25 res = step["data"]['ret']26 src = res["screen"]27 if "pos" in res:28 screen["pos"] = res["pos"]29 for item in step["__children__"]:30 if item["data"]["name"] == "_gen_screen_log":31 res = item["data"]['ret']32 src = res["screen"]33 if "pos" in res:34 screen["pos"] = res["pos"]35 break36 if self.export_dir and src:37 src = os.path.join(LOGDIR, src)38 screen["src"] = src39 return screen40 else:41 return trans42def new_translate_desc(self, step, code):43 trans = old_trans_desc(self, step, code)44 if "name" in step['data'] and (step['data']["name"] in screen_func or step["data"]["name"] in other_func):45 name = step["data"]["name"]46 args = {i["key"]: i["value"] for i in code["args"]}47 desc = {48 "find_element_by_xpath": lambda: u"Find element by xpath: %s" % args.get("xpath"),49 "find_element_by_id": lambda: u"Find element by id: %s" % args.get("id"),50 "find_element_by_name": lambda: u"Find element by name: %s" % args.get("name"),51 "assert_exist": u"Assert element exists.",52 "click": u"Click the element that been found.",53 "send_keys": u"Send some text and keyboard event to the element that been found.",54 "get": lambda: u"Get the web address: %s" % args.get("address"),55 "switch_to_last_window": u"Switch to the last tab.",56 "switch_to_latest_window": u"Switch to the new tab.",57 "back": u"Back to the last page.",58 "forward": u"Forward to the next page.",59 "snapshot": u"Snopshot current page."60 }61 desc_zh = {62 "find_element_by_xpath": lambda: u"寻找指定页面元素: \"%s\"" % args.get("xpath"),63 "find_element_by_id": lambda: u"寻找指定页面元素: \"%s\"" % args.get("id"),64 "find_element_by_name": lambda: u"寻找指定页面元素: \"%s\"" % args.get("name"),65 "assert_exist": u"断言页面元素存在.",66 "click": u"点击找到的页面元素.",67 "send_keys": u"传送文本\"%s\"到选中文本框." % (args.get("text", "")),68 "get": lambda: u"访问网络地址: %s" % args.get("address"),69 "switch_to_last_window": u"切换到上一个标签页.",70 "switch_to_latest_window": u"切换到最新标签页.",71 "back": u"后退到上一个页面.",72 "forward": u"前进到下一个页面.",73 "snapshot": u"截取当前页面."74 }75 if self.lang == 'zh':76 desc = desc_zh77 ret = desc.get(name)78 if callable(ret):79 ret = ret()80 return ret81 else:82 return trans83def new_translate_code(self, step):84 trans = old_trans_code(self, step)85 if trans:86 for idx, item in enumerate(trans["args"]):87 if item["key"] == "self":88 trans["args"].pop(idx)89 return trans90report.LogToHtml._translate_screen = new_trans_screen91report.LogToHtml._translate_desc = new_translate_desc...

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 Airtest 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