您的位置:首页 > 编程语言 > Python开发

python3.5+selenium3.4自动化测试4_随机组合的用户名和密码登陆

2017-06-12 10:55 756 查看

继续讲登陆和登出界面,这次主要讲如何采用随机的字母,数字,特殊符号组合的用户名和密码进行登陆,这边我写了不同验证用户名密码组合的登陆测试用例,所以代码比较长

# _*_ coding: utf-8 _*_

import unittest,time,re
from time import strftime, localtime
from datetime import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchAttributeException

import HTMLTestRunner
import mod_userpass

class 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
d547
_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 random
global 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)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息