概述
有时候业务中的一些固定流程的测试环境需要重复执行很多次;这种场景其实可以用python
的selenium
库模拟用户手动点击输入,实现自动化测试;
我这边的python
版本是
Python 3.6.7rc2
demo
# coding=utf-8
import time
import logging
import re
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChainsdef login(driver):url = "http://192.168.xxx.xxx/xxx/xxx/add"# 打开网页driver.get(url) # 打开url网页 比如 driver.get("http://www.baidu.com")login_user_name_xpath = '//*[@id="userName"]'login_user_passwd_xpath = '//*[@id="password"]'# 等待登录账户输入框dom加载wait_xpath_dom_loading(driver, login_user_name_xpath)wait_xpath_dom_loading(driver, login_user_passwd_xpath)# 登录自动获得tokendriver.find_element_by_xpath(login_user_name_xpath).send_keys('xxx')driver.find_element_by_xpath(login_user_passwd_xpath).send_keys('xxx')driver.find_element_by_xpath('//*[@id="root"]/div/div/div/div[2]/form/button').click()# 等待登录后的网页跳转time.sleep(2)return driverdef memberFillDataAndSubmit_01(driver):url = "http://xxx.xxx.xxx.xxx/xxx/xxx/add"# 打开网页driver.get(url)# 等待页面加载time.sleep(2)try:driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/ul')except:print('未找到分页元素,当前页面已无数据')return driver# 填充数据driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[2]/div[2]/div/div[2]/input').send_keys("2")driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[3]/div[2]/div/div[2]/input').send_keys("3.5")for count in range(0,170):# 页面一件填充按钮driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/button').click()# 数据全选按钮driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div/label/span/input').click()# 数据提交driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[2]/div[2]/div[3]/button').click()# 等待页面加载time.sleep(2)return driverdef memberFillDataAndSubmit_02(driver):url = "http://xxx.xxx.xxx.xxx/xxx/xxx/add"# 打开网页driver.get(url)# 等待页面加载time.sleep(2)try:driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/ul')except:print('未找到分页元素,当前页面已无数据')return driver# CA填报driver.find_element_by_xpath('//*[@id="advanced_search"]/div/div[1]/div[2]').click()# 等待页面加载time.sleep(2)# 填充数据driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[2]/div[2]/div/div[2]/input').send_keys("2")driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/div[3]/div[2]/div/div[2]/input').send_keys("3.5")for c in range(0,24):# 页面一件填充按钮driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[3]/button').click()# 数据全选按钮driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[4]/div/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div').click()# 数据提交driver.find_element_by_xpath('//*[@id="root"]/div/section/div[2]/main/div/div[1]/div[2]/div[2]/div[3]/button').click()# 等待页面加载time.sleep(2)return driverdef groupDataCheckAndSubmitWrapper(driver):url = "http://xxx.xxx.xxx.xxx/xxx/xxx/manager"# 状态下拉框xpathstatus_optional_xpath = '//*[@id="advanced_search"]/div/div[5]/div/div/div[2]/div/div/div'# 当前页面数据全选current_page_total_choice_xpath = '//*[@id="rc-tabs-0-panel-2"]/div[2]/div[3]/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div/label'# 状态下拉框中option xpathstatus_optional_wait_check_xpath = '/html/body/div[2]/div/div/div/div[2]/div/div/div/div[3]'# 查询按钮xpathquery_btn_xpath = '//*[@id="advanced_search"]/div/div[19]/div/div[2]/button'# 打开网页driver.get(url)time.sleep(5)# 等待数据全选渲染完毕(数据请求完毕)wait_xpath_dom_loading(driver, current_page_total_choice_xpath)# 创建ActionChains对象actions = ActionChains(driver)# 点击唤出状态下拉框status_optional = driver.find_element_by_xpath(status_optional_xpath)actions.move_to_element(status_optional).perform()status_optional.click()time.sleep(1)# 点击待审核状态click_delay_time(driver, status_optional_wait_check_xpath)# 重新查询click_delay_time(driver, query_btn_xpath)time.sleep(3)submit_type_xpath_list = [# 标签页2'//*[@id="rc-tabs-0-panel-2"]/div[1]/div/div[2]/label[2]',# 标签页3'//*[@id="rc-tabs-0-panel-2"]/div[1]/div/div[3]/label[2]',# 标签页4'//*[@id="rc-tabs-0-panel-2"]/div[1]/div/div[4]/label[2]']# 首次进入页面,查询标签页1数据groupDataCheckAndSubmit(driver, actions)for submit_type_xpath in submit_type_xpath_list:driver.find_element_by_xpath(submit_type_xpath).click()time.sleep(10)# 数据审核提交groupDataCheckAndSubmit(driver, actions)def groupDataCheckAndSubmit(driver, actions):# 状态筛选下拉浮窗hover位置check_submit_hover_position_xpath = '//*[@id="rc-tabs-0-panel-2"]/div[2]/div[2]/div[2]/div[1]/div/div[2]/button'# 浮窗中 通过并提交 按钮xpathpass_and_submit_xpath = '/html/body/div[3]/div/div/ul/li[2]/span/a'# 当前页面数据全选current_page_total_choice_xpath = '//*[@id="rc-tabs-0-panel-2"]/div[2]/div[3]/div/div/div/div/div/div[1]/table/thead/tr/th[1]/div/label'# 审核按钮hover位置dom加载wait_xpath_dom_loading(driver, check_submit_hover_position_xpath)# 审核按钮hover位置check_submit_hover_position = driver.find_element_by_xpath(check_submit_hover_position_xpath)try:# 通过class获取分页litotal_page_li_list = driver.find_element_by_class_name('ant-pagination').find_elements_by_tag_name('li')# 过滤获取页码对应xpath对象page_li_list = list(filter(get_max_page_num, total_page_li_list))total_page_num = int(page_li_list[len(page_li_list) - 1].get_attribute('title'))# 没有数据if 0 == total_page_num: return driverexcept Exception as e:# 没有数据时当前页面没有渲染分页下标logging.exception(e)return driverfor count in range(0,total_page_num):# 提交后页面重新加载time.sleep(5)# 数据全选wait_xpath_dom_loading(driver, current_page_total_choice_xpath)click_delay_time(driver, current_page_total_choice_xpath)# 进行重置坐标actions.reset_actions()# 鼠标悬停,唤出按钮actions.move_to_element(check_submit_hover_position).perform()# js加载time.sleep(2)# 鼠标hover到审核通过按钮上.ran后点击按钮check_and_submit_btn = driver.find_element_by_xpath(pass_and_submit_xpath)# actions.move_to_element(check_and_submit_btn).perform()# js加载#time.sleep(1)# 通过并提交check_and_submit_btn.click()return driver# WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
# driver: 浏览器驱动
# timeout: 最长超时时间
# poll_frequency: 检测间隔时间,默认0.5s
# ignored_exceptions: 超时后的异常信息,默认情况抛出NoSuchElementException异常
# 一般由until()或until_not方法配合使用,下面是这两种方法的说明:
# until(method, message=''): 调用该方法提供的驱动程序作为一个参数,直到返回值为True;
# until_not(method, message=''): 调用该方法提供的驱动程序作为一个参数,直到返回值为Flase;
def wait_xpath_dom_loading(driver, xpath):locator = (By.XPATH, xpath)WebDriverWait(driver, 10, 1).until(EC.presence_of_element_located(locator))def click_delay_time(driver, xpath):driver.find_element_by_xpath(xpath).click()time.sleep(2)def get_max_page_num(xpath_obj):return re.match('\d', xpath_obj.get_attribute('title'))if __name__ == '__main__':driver_path = 'D:\chromeDownLoad\chromedriver-win64\chromedriver.exe'driver = webdriver.Chrome(driver_path) # Chrome浏览器# 设置全局等待超时时间5sdriver.implicitly_wait(5)try:# 登录driver = login(driver)# 数据填报提交memberFillDataAndSubmit_01(driver)# 数据填报提交memberFillDataAndSubmit_02(driver)# 组长数据审核提交groupDataCheckAndSubmitWrapper(driver)except Exception as e:logging.exception(e)# 关闭浏览器driver.quit()