python27+selenium3自动化登录测试

#encoding:utf-8
'''
方法:通过selenium的webdriver模块结合unittest单元测试框架自动化登录系统,下文枚举了几个测试用例登录情况,其中包括用户名和密码的验证以及滑动模块的滑动验证。
作者:范先生
时间:2018年4月
'''
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import time

class LoginCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="D:\\4software\\Python\\python27\\driver\\chrome\\chromedriver")
self.driver.maximize_window()

#定义登录系统方法
def Login(self,username,password,offset):
self.driver.get("http://192.168.20.21")
time.sleep(2)
self.driver.find_element_by_id("username").clear()
self.driver.find_element_by_id("username").send_keys(username)
self.driver.find_element_by_id("password").clear()
self.driver.find_element_by_id("password").send_keys(password)
source =self.driver.find_element_by_xpath("//*[@id='form1']/div/label[3]/div/div/div[3]/div")
ActionChains(self.driver).drag_and_drop_by_offset(source, offset, 0).perform()
time.sleep(1)
self.driver.find_element_by_xpath("//*[@id='form1']/div/button").click()
time.sleep(5)

def test_login_success(self):
'''用户名和密码正确的情况'''
self.Login('superadmin','superxxxxx123',300)
link = self.driver.find_element_by_xpath("//*[@id='button-1252-btnInnerEl']")
# 用assertTrue断言“上网用户管理”在"//*[@id='button-1252-btnInnerEl']"里面
self.assertTrue(u'上网用户管理' in link.text)
self.driver.get_screenshot_as_file("D:\\4software\\Python\\script\\untitled\\login_success.png")
print "Login is ok !"

def test_paswd_error(self):
'''用户名正确,密码错误的情况'''
self.Login('superadmin','1234567',300)
error_message = self.driver.find_element_by_xpath('//*[@id="dvMsgCT"]').text
self.assertIn(u'帐号或密码错误',error_message)
self.driver.get_screenshot_as_file("D:\\4software\\Python\\script\\untitled\\login_paswd_error.png")
print "assert is right !"

def test_paswd_tabsoms(self):
'''用户名正确,密码非法字符的情况'''
self.Login('superadmin','######',300)
error_message = self.driver.find_element_by_xpath('//*[@id="dvMsgCT"]').text
self.assertIn(u'帐号或密码错误',error_message)
self.driver.get_screenshot_as_file("D:\\4software\\Python\\script\\untitled\\login_paswd_tabsomes.png")
print "assert is right !"

def test_paswd_null(self):
'''用户名正确,密码为空'''
self.Login('superadmin','',300)
error_message = self.driver.find_element_by_xpath('//*[@id="dvMsgCT"]').text
self.assertIn(u'账号或密码不能为空',error_message)
self.driver.get_screenshot_as_file("D:\\4software\\Python\\script\\untitled\\login_paswd_null.png")
print "assert is right !"

def test_paswd_offset_Low(self):
'''用户名正确,密码正确,滑块没有移动到指定位置!'''
self.Login('superadmin','supermanager123',100)
error_message = self.driver.find_element_by_xpath('//*[@id="dvMsgCT"]').text
self.assertIn(u'请拖动滑动解锁验证!',error_message)
self.driver.get_screenshot_as_file("D:\\4software\\Python\\script\\untitled\\login_paswd_offset_Low.png")
print "assert is right !"

def tearDown(self):
time.sleep(2)
# print '/n'
# print u"测试通过!"
self.driver.quit()

if __name__=="__main__":
unittest.main()
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

执行结果:

D:\4software\Python\python27\install\python.exe D:/4software/Python/script/untitled/login.py
Login is ok !
.assert is right !
.assert is right !
.assert is right !
.assert is right !
.
----------------------------------------------------------------------
Ran 5 tests in 126.282s

OK

posted on 2018-04-19 14:45  蜗牛前进的方向  阅读(705)  评论(0编辑  收藏  举报

导航