python3.5+selenium3.4自动化测试2_参数化
来源:互联网 发布:汉王手写板软件 编辑:程序博客网 时间:2024/05/21 10:57
这边继续讲一个简单得登陆窗口,一般登陆窗口保护用户名和密码,那测试登陆的时候必然会用到不同的用户名和密码,这个时候就需要对参数化用户名和密码,先上一下代码
from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchAttributeExceptionimport unittest,time,refrom time import strftime, localtimefrom datetime import datetimeimport HTMLTestRunnerimport mod_usr_passclass LoginUser(unittest.TestCase): def setUp(self): self.driver=webdriver.Chrome() self.driver.implicitly_wait(30) self.base_url="http://192.168.1.131" self.verificationErrors=[] self.accept_next_alert=True def test_loginuser(self): ''' 1.3.用户名和密码分别按以下填入: 1.用户名为admin,密码为正确密码 2.用户名为admin前加空格,密码为正确密码 3.用户名为admin后加空格,密码为正确密码 4.用户名为数字,密码为数字 5.用户名为monitor,密码为正确密码 点击“Login”登录 ''' driver=self.driver driver.get(self.base_url+"/") try: driver.implicitly_wait(10) #智能等待30 for u,p in mod_usr_pass.usrpassdict().items(): driver.find_element_by_id("username").send_keys(u) driver.find_element_by_id("secret").send_keys(p) driver.find_element_by_id("login_button").click() time.sleep(3) if u!="1000": driver.switch_to_alert().accept() driver.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click() driver.switch_to_alert().accept() time.sleep(3)#等待10 else: driver.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click() driver.switch_to_alert().accept() time.sleep(3)#等待10 except: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.3/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) def tearDown(self): time.sleep(3) self.driver.quit() self.assertEqual([],self.verificationErrors)if __name__=="__main__": suite=unittest.TestSuite() suite.addTest(LoginUser("test_loginuser")) unittest.TextTestRunner().run(suite)
1. 下面是用户名和密码的函数,新建一个mod_usr_pass.py文件
def usrpassdict(): d={'admin':'password', ' admin':'password', 'admin ':'password', 'monitor':'password', '1000':'1000', } return d
函数中定义了用户名对应密码的字典
2.主函数中,需要import mod_usr_pass ,然后循环调用字典中的用户名和密码
for u,p in mod_usr_pass.usrpassdict().items(): driver.find_element_by_id("username").send_keys(u) driver.find_element_by_id("secret").send_keys(p)
以后只要在mod_usr_pass.py函数中修改不同的登陆用户名或密码就可以被多个不同的用例函数调用
阅读全文
0 0
- python3.5+selenium3.4自动化测试2_参数化
- python3.5+selenium3.4自动化测试4_随机组合的用户名和密码登陆
- python3.5+selenium3.4自动化测试1_错误图片用时间命名保存
- python3.5+selenium3.4自动化测试3_下拉框循环选择
- python3.5+selenium3.4自动化测试5_frame中的对象
- python3.5+selenium3.4自动化测试8_selenium框架1.0
- python3.5+selenium3.4自动化测试6_selenium的Webdriver-API整理(上)
- python3.5+selenium3.4自动化测试7_selenium的Webdriver_API整理(下)
- python3.5+selenium3.4自动化测试9_selenium框架2.0-unittest(上)
- python3.5+selenium3.4自动化测试6_selenium的Webdriver-API整理(上)
- python3.5+selenium3.4自动化测试10_selenium框架2.0-unittest(下)
- python3 + selenium3 web自动化测试环境搭建
- Selenium3.0 自动化测试
- Selenium3.0 自动化测试
- Selenium3.0 自动化测试-简介
- python3 selenium web自动化测试之excel参数化
- 基于selenium3+python3.6.x+chrome57.x的自动化测试环境搭建
- 测试笔记第一天----python3.6+selenium3.5环境搭建
- 扩展RBAC用户角色权限设计方案
- 简单的三数排序,由小到大
- QT:抽奖软件(可显示图片和姓名)
- ajax 拼url地址,真的是门学问......
- Mina学习笔记(1) mina简介
- python3.5+selenium3.4自动化测试2_参数化
- Java web之一
- Android 高效调试神器 JRebel
- 判断101-200之间有多少个素数,并输出所有素数。
- hololens开发手记——Unity之语音输入
- JAVA中的static关键字
- 线程池
- JQuery 元素位置
- Service流程