概念:
1、线性测试:单纯模拟用户完整的操作场景,每个脚本相对独立,且不产生依赖与调用。
2、模块化与类库:把重复的操作单独封装成公共模块,需要时进行调用。
3、数据驱动测试:参数化测试数据。
4、关键字驱动测试:把自动化操作封装成为“关键字”
模块化与参数化:
163邮箱登录与退出:save as: test_mail.py
from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get("https://mail.163.com/") driver.implicitly_wait(10) #登录 iframe = driver.find_element_by_css_selector("iframe") driver.switch_to.frame(iframe) driver.find_element_by_css_selector("[name = ‘email‘]").send_keys("****") driver.find_element_by_css_selector("[name = ‘password‘]").send_keys("***") driver.find_element_by_id("dologin").click() driver.switch_to.default_content() #退出 driver.find_element_by_id("_mail_icon_0_0").click() driver.find_element_by_id("_mail_component_72_72").click()
模块化:save as :module.py
class Mail: def __init__(self,driver): self.driver = driver def login(self): """登录""" iframe = self.driver.find_element_by_css_selector("iframe") self.driver.switch_to.frame(iframe) self.driver.find_element_by_css_selector("[name = ‘email‘]").send_keys("***") self.driver.find_element_by_css_selector("[name = ‘password‘]").send_keys("***") self.driver.find_element_by_id("dologin").click() self.driver.switch_to.default_content() def logout(self): """退出""" self.driver.find_element_by_id("_mail_icon_0_0").click() self.driver.find_element_by_id("_mail_component_72_72").click()
修改test_mail.py文件调用Mail中的login() 、logout()
from selenium import webdriver from time import sleep from module import Mail driver = webdriver.Chrome() driver.get("https://mail.163.com/") driver.implicitly_wait(10) mail = Mail(driver) mail.login() mail.logout()
参数化(用户名密码)登录:修改module代码如下:
class Mail: def __init__(self,driver): self.driver = driver def login(self,username,password): """登录""" iframe = self.driver.find_element_by_css_selector("iframe") self.driver.switch_to.frame(iframe) self.driver.find_element_by_css_selector("[name = ‘email‘]").clear() self.driver.find_element_by_css_selector("[name = ‘email‘]").send_keys(username) self.driver.find_element_by_css_selector("[name = ‘password‘]").clear() self.driver.find_element_by_css_selector("[name = ‘password‘]").send_keys(password) self.driver.find_element_by_id("dologin").click() self.driver.switch_to.default_content()
不同数据调用Login():
from selenium import webdriver from time import sleep from module import Mail driver = webdriver.Chrome() driver.get("https://mail.163.com/") driver.implicitly_wait(10) mail = Mail(driver) mail.login("error","error") sleep(2) mail.login("123","123")
原文:https://www.cnblogs.com/yijierui/p/13449577.html