python3.5+selenium3.4自动化测试4_随机组合的用户名和密码登陆
来源:互联网 发布:yii2连接oracle数据库 编辑:程序博客网 时间:2024/06/06 16:39
继续讲登陆和登出界面,这次主要讲如何采用随机的字母,数字,特殊符号组合的用户名和密码进行登陆,这边我写了不同验证用户名密码组合的登陆测试用例,所以代码比较长
# _*_ coding: utf-8 _*_import unittest,time,refrom time import strftime, localtimefrom datetime import datetimefrom 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 HTMLTestRunnerimport mod_userpassclass LoginPassword(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_loginpasswordone(self): ''' 用户名填入admin,密码填入随意字母和数字组合的字符串,点击“Login” ''' driver=self.driver driver.get(self.base_url+"/") try: driver.implicitly_wait(10) #智能等待10 driver.find_element_by_id("username").send_keys("admin") mod_userpass.get_userNameAndPassword() driver.find_element_by_id("secret").send_keys(mod_userpass.userPassword) print(mod_userpass.userPassword)#打印输入的随机密码 driver.find_element_by_id("login_button").click() time.sleep(3) #检查提示语 tip=driver.find_element_by_id("statusbar").text if tip=="Invalid User name or Password" : pass else: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) #正确得用户名和密码登陆退出一次 time.sleep(3) driver.find_element_by_id("username").send_keys("admin") driver.find_element_by_id("secret").send_keys("password") driver.find_element_by_id("login_button").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3) driver.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3)#等待3 except: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) def test_loginpasswordtwo(self): ''' 继续连续五次,用户名填入admin,密码填入随意字母和数字组合的字符串,点击“Login” 用户名填入admin,密码填入正确密码,点击“Login” 等待十分钟 用户名填入admin,密码填入正确密码,点击“Login” ''' driver=self.driver driver.get(self.base_url+"/") try: driver.implicitly_wait(10) #智能等待30 #输入五次错误的密码 for i in range(5): driver.find_element_by_id("username").send_keys("admin") mod_userpass.get_userNameAndPassword() driver.find_element_by_id("secret").send_keys(mod_userpass.userPassword) print(mod_userpass.userPassword) driver.find_element_by_id("login_button").click() time.sleep(3) tip=driver.find_element_by_id("statusbar").text if tip=="Sorry, unsuccessful logon for five times, please try again 10 minutes later.": pass else: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) #输入正确的密码,无法正常登陆 time.sleep(3) driver.find_element_by_id("username").send_keys("admin") driver.find_element_by_id("secret").send_keys("password") driver.find_element_by_id("login_button").click() #等待10分钟,使用正确得用户名和密码登陆 time.sleep(600) driver.find_element_by_id("username").send_keys("admin") driver.find_element_by_id("secret").send_keys("password") driver.find_element_by_id("login_button").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3) driver.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3)#等待3 except: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) def test_loginpasswordthr(self): ''' 用户名填入随机字母组合,密码填入随意字母和数字组合的字符串,点击“Login” ''' driver=self.driver driver.get(self.base_url+"/") try: driver.implicitly_wait(10) #智能等待10 mod_userpass.get_userNameAndPassword() driver.find_element_by_id("username").send_keys(mod_userpass.userName) driver.find_element_by_id("secret").send_keys(mod_userpass.userPassword) print(mod_userpass.userPassword) driver.find_element_by_id("login_button").click() time.sleep(3) #检查提示语 tip=driver.find_element_by_id("statusbar").text if tip=="Invalid User name or Password" : pass else: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) time.sleep(3) driver.find_element_by_id("username").send_keys("admin") driver.find_element_by_id("secret").send_keys("password") driver.find_element_by_id("login_button").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3) driver.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3)#等待3 except: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) def test_loginpasswordfor(self): ''' 用户名/密码分别填入:admin/空白,点击登录 ''' driver=self.driver driver.get(self.base_url+"/") try: driver.implicitly_wait(10) #智能等待30 driver.find_element_by_id("username").send_keys("admin") driver.find_element_by_id("secret").send_keys("") driver.find_element_by_id("login_button").click() time.sleep(3) #检查提示语 tip=driver.switch_to_alert().text if tip=="Invalid User name or Password" : pass else: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]) time.sleep(3) driver.switch_to_alert().accept() driver.find_element_by_id("username").clear() driver.find_element_by_id("username").send_keys("admin") driver.find_element_by_id("secret").send_keys("password") driver.find_element_by_id("login_button").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3) driver.find_element_by_xpath(".//*[@id='nav']/ul/li[1]/a").click() time.sleep(3) driver.switch_to_alert().accept() time.sleep(3)#等待3 except: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%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(LoginPassword("test_loginpasswordone")) suite.addTest(LoginPassword("test_loginpasswordtwo")) suite.addTest(LoginPassword("test_loginpasswordthr")) suite.addTest(LoginPassword("test_loginpasswordfor")) unittest.TextTestRunner().run(suite)
下面是个引用函数,创建一个mod_userpass.py文件,这个函数主要是创建了一个随机字母,数字和特殊符号组成的用户名和密码
# _*_ coding: utf-8 _*_import randomglobal userName,userPassword #为了便于后面使用,定义为全局变量userName = ''userPassword = ''def get_userNameAndPassword(): global userName, userPassword #8位用户名及8位密码 userName = ''.join(random.sample("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><:}{?/",8)) userPassword = ''.join(random.sample("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.1234567890",8))
1.用例中主要涉及到随机用户名登陆和随机密码登陆会不会提示错误,并最终锁住不能登陆,这个主要是用例的覆盖性,这边暂时不讲,我们主要针对讲一下,经常遇到的,用户名正确,不断尝试不同的密码登陆,在函数的开头已经有import mod_userpass
#输入五次错误的密码 for i in range(5): driver.find_element_by_id("username").send_keys("admin") mod_userpass.get_userNameAndPassword() driver.find_element_by_id("secret").send_keys(mod_userpass.userPassword) print(mod_userpass.userPassword) driver.find_element_by_id("login_button").click() time.sleep(3) tip=driver.find_element_by_id("statusbar").text if tip=="Sorry, unsuccessful logon for five times, please try again 10 minutes later.": pass else: driver.get_screenshot_as_file(u"D:/python/selenium/error_png/1.login/1.5/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])
2.随机的用户名和密码登陆
mod_userpass.get_userNameAndPassword() driver.find_element_by_id("username").send_keys(mod_userpass.userName) driver.find_element_by_id("secret").send_keys(mod_userpass.userPassword)
阅读全文
3 0
- python3.5+selenium3.4自动化测试4_随机组合的用户名和密码登陆
- python3.5+selenium3.4自动化测试2_参数化
- 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自动化测试6_selenium的Webdriver-API整理(上)
- python3.5+selenium3.4自动化测试9_selenium框架2.0-unittest(上)
- python3.5+selenium3.4自动化测试10_selenium框架2.0-unittest(下)
- python3 + selenium3 web自动化测试环境搭建
- 基于selenium3+python3.6.x+chrome57.x的自动化测试环境搭建
- 设置tomcat的登陆用户名和密码
- Selenium3.0 自动化测试
- Selenium3.0 自动化测试
- 测试的局限性和自动化随机测试
- selenium+python针对登录功能的用户名,密码和断言组合进行的测试
- java 表现层:jsp、freemarker、velocity
- 矩形覆盖(java版本)
- git 合并分支出现 Please enter a commit message to explain why this merge is necessary.
- Bootstrap扩展Modal弹窗
- DateEdit和TimeEdit用法
- python3.5+selenium3.4自动化测试4_随机组合的用户名和密码登陆
- 前端组件化
- windows 的文件在linux下格式不对的问题。
- c#中数组,ArrayList和List 三者的区别
- Question Answer(web-QA + KB-QA + social-QA) research in the world
- hadoop学习之一:mac下hadoop安装
- Android 实现浏览器打开app
- 【详细内容】LVDS转TTL芯片GM8284DD规格书
- Ext中apply及applyIf