测试|UI自动化


UI自动化写脚本【自用】

  • 定位方式
      • 常用操作

定位方式 1、id
2、name
3、class_name
4、tag_name
5、link_text
6、partial_link_text
7、XPath
8、CSS
通过属性定位元素: find_element_by_xpath(“//标签名[@属性=‘属性值’]”)
id属性
find_element_by_xpath("//input[@id=‘’]")

class属性
find_element_by_xpath("//input[@class=‘’]")

name属性
find__element_by_xpath("//input[@name=‘’]")

常用操作
(1)点击操作click():
self.driver.find_element_by_xpath(xpath).click()self.driver.find_element_by_id(xpath).click()self.driver.find_element_by_class_name(xpath).click()self.driver.find_element_by_css_selector(xpath).click()self.driver.find_elements_by_xpath(xpath)[number].click() self.driver.find_elements_by_css_selector(xpath)[number].click()

测试|UI自动化
文章图片

测试|UI自动化
文章图片

(2)输入内容send_keys():
self.driver.find_element_by_xpath(xpath).send_keys(keys)self.driver.find_element_by_id(xpath).send_keys(keys)self.driver.find_element_by_class_name(xpath).send_keys(keys)self.driver.find_element_by_css_selector(xpath).send_keys(keys)self.driver.find_elements_by_xpath(xpath)[number].send_keys(keys) self.driver.find_elements_by_css_selector(xpath)[number].send_keys(keys)

测试|UI自动化
文章图片

(3)清除clears():
self.driver.find_element_by_xpath(xpath).clear()self.driver.find_element_by_id(xpath).clear()self.driver.find_element_by_class_name(xpath).clear()self.driver.find_element_by_css_selector(xpath).clear()self.driver.find_elements_by_xpath(xpath)[number].clear() self.driver.find_elements_by_css_selector(xpath)[number].clear()

【测试|UI自动化】测试|UI自动化
文章图片

测试|UI自动化
文章图片

(4)下拉选择drop():
self.driver.find_element_by_xpath(xpath).click() sleep(1) self.driver.find_element_by_xpath(xpath1).click()self.driver.find_element_by_id(xpath).click() sleep(1) self.driver.find_element_by_id(xpath1).click()self.driver.find_element_by_class_name(xpath).click() sleep(1) self.driver.find_element_by_class_name(xpath1).click()self.driver.find_element_by_css_selector(xpath).click() sleep(1) self.driver.find_element_by_css_selector(xpath1).click()

测试|UI自动化
文章图片

测试|UI自动化
文章图片

1.下拉选择
self.driver.find_element_by_xpath(‘//[@x-placement=“bottom-start”]/div/div/ul/li[1]‘).click()
2.上拉选择
self.driver.find_element_by_xpath(’//
[@x-placement=“top-start”]/div/div/ul/li[1]’).click()
(5)参数对比:
""参数对比check""" text_data = https://www.it610.com/article/self.driver.find_element_by_xpath(xpath).text print(text_data) assert text == text_data""参数对比check_error""" text_data = https://www.it610.com/article/self.driver.find_element_by_xpath(xpath).text print(text_data) assert text != text_data

(6)悬浮按钮操作choice_xf):
ac = ActionChains(self.driver)# 获取实例化ActionChains类 act = self.driver.find_element_by_xpath(xpath) ac.move_to_element(act).perform() sleep(1) self.driver.find_element_by_xpath(xpath1).click() if mold == 'css': ac = ActionChains(self.driver)# 获取实例化ActionChains类 act = self.driver.find_element_by_css_selector(xpath) ac.move_to_element(act).perform() sleep(1) self.driver.find_element_by_css_selector(xpath1).click()choices_xf(): ac = ActionChains(self.driver)# 获取实例化ActionChains类 act = self.driver.find_element_by_xpath(xpath) ac.move_to_element(act).perform() sleep(1) self.driver.find_elements_by_xpath(xpath1)[number].click()

(7)元素按tab键:
self.driver.find_element_by_xpath(xpath).send_keys(Keys.TAB) if mold == 'css': self.driver.find_element_by_css_selector(xpath).send_keys(Keys.TAB)

(8)按住元素进行拖动:
a = self.driver.find_element_by_xpath(xpath) ac = ActionChains(self.driver) ac.drag_and_drop_by_offset(a, x, y).perform()

(9)获取当天日期:
self.driver.find_element_by_xpath("//input[@placeholder='请输入 名称']").send_keys(str(datetime.date.today()))self.driver.find_elements_by_xpath("//input[@placeholder='请输入 名称']")[1].send_keys(str(datetime.date.today()))

(10)获取明天日期:
tomorrow = str(datetime.date.today() + datetime.timedelta(days=1)) self.driver.find_element_by_xpath("//input[@placeholder='请输入 名称']").send_keys(tomorrow)

    推荐阅读