UI自动化框架

问题:用例中的setup_class老是在conftest.py前执行
解决:将setup_class改为setup就可以了

   富文本框输入

  1. 富文本框一般用iframe,先切到iframe
  2. 找到富文本框元素,并输入值
  3. 从iframe跳回
1 self.switch_frame(0)
2 ele = self.get_element_by_xpath(self.richtext_xpath)
3 ele.clear()
4 self.sleep_wait(2)
5 ele.send_keys(text)
6 self.switch_to_default()                

 

只读元素处理
问题:有些下拉框需要点击多次才能选到值,比如地区选择框,需要选择省市县乡;比如时间选择框。这种情况不太好用元素定位的方式。
解决:去掉该元素的readonly属性,直接传入需要输入的数据。代码如下:
self.driver.execute_script('arguments[0].removeAttribute("readonly")', ele)
self.driver.execute_script('arguments[0].setAttribute("value", "'+text+'")', ele)

滚动到某一元素可见
ele = self.get_element_by_xpath(xpath)
js = "arguments[0].scrollIntoView(true)"
self.driver.execute_script(js, ele)

  

鼠标移入操作
鼠标移入,要求element是可见的
ActionChains(self.driver).move_to_element(element).perform()

上传文件
问题:上传文件,若采用window窗口上传方式,运行时,上传窗口需处于打开状态。会存在一个问题:无界面运行时,用例会失败
解决方案一:若上传标签类型为input,直接selenium上传。driver.find_element_by_xpath("").send_keys(r"c:/test.png")
解决方案二:用接口直接上传

pytest用例失败重试
安装插件pytest-rerunfailures
pytest.main(['project_cylinders/testcases','--reruns=3','--alluredir=reports\Allure'])

无界面运行
option = Options()
option.add_argument('--no-sandbox')
option.add_argument('--disable-gpu')
option.add_argument('--hide-scrollbars')
option.add_argument('blink-settings=imagesEnabled=false')
option.add_argument('--headless')
self.driver = webdriver.Chrome(executable_path=self.chrome_driver_path,chrome_options=option)

 



posted @ 2021-04-01 10:18  litian19870924  阅读(133)  评论(0)    收藏  举报