基于selenium的portal自动化认证

#!/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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值