基于selenium的portal自动化认证
#!/usr/bin/python#coding:utf-8import randomimport httplibimport timeimport threadingimport unittestfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysthread_n
·
#!/usr/bin/python
#coding:utf-8
import random
import httplib
import time
import threading
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
thread_num = 0
flag = 0
def test_authentication(self):
'''
测试认证界面
'''
ipAddress = '172.27.131.221'
headers={"X-Forwarded-For":ipAddress,"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0"}
#这里主要是验证是否会进行正确的重定向
conn = httplib.HTTPConnection("172.27.131.216:8080")
conn.request("GET","/","",headers)
response = conn.getresponse()
conn.close()
result = response.getheaders()
#打印出重定向的url
#print result[2][1]
#重定向到认证界面的处理过程
try:
if flag == 0:
self.assertEqual(result[2][1], "http://172.27.131.161:8080/nac/register/login?node_ip=172.27.131.153&original_url=172.27.131.216:8080/")
flag = 1
else:
self.assertEqual(1,1)
except Exception, e:
print e
else:
#登陆认证界面
driver = webdriver.Chrome()
loginurl = "http://172.27.131.161:8080/nac/register/login?node_ip=172.27.131.153&original_url=172.27.131.216:8080/"
driver.get(loginurl)
#用户名 密码
elem_user = driver.find_element_by_id("user_name")
elem_user.send_keys("qx")
elem_pwd = driver.find_element_by_id("passwd")
elem_pwd.send_keys("111111")
elem_pwd.send_keys(Keys.RETURN)
time.sleep(6)
finally:
#验证登录成功与否,若currenturl发生变化,则认为登录成功
newpage_url = driver.current_url
#print newpage_url
try:
self.assertNotEqual(newpage_url,loginurl)
except Exception, e:
print e
else:
print "Complete redirection"
if newpage_url == "http://172.27.131.216:8080/login?refer=%2F":
print "auth success"
driver.close()
driver.quit()
class TestAuth(unittest.TestCase):
def setUp(self):
print "test start"
def testAuth(self):
thread_list = list()
thread_num = 2
#thread_num = random.randint(1,100)
for i in range(0, thread_num):
j = random.randint(1,10000)
thread_name = "thread_%s" %j
thread_list.append(threading.Thread(target = test_authentication, name = thread_name, args = (self, )))
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
def tearDown(self):
print "test stop"
if __name__=="__main__":
unittest.main()
更多推荐
已为社区贡献3条内容
所有评论(0)