William126

Python实战之Selenium自动化测试web登录

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
class LoginTest(object):

    def __init__(self):
        self.url = ''
        self.uname = ''
        self.pwd = ''
        self.userEle = ''
        self.pwdEle = ''
        self.loginEle = ''
        self.status = ''
        self.statusCode = ''

    def openUrl(self):
        self.driver = webdriver.Firefox()
        self.driver.get(self.url)
        time.sleep(10)

    def findLoginEle(self):
        # id = "user"
        self.userbTn = self.driver.find_element_by_id(self.userEle)
        self.pwdbTn = self.driver.find_element_by_id(self.pwdEle)
        self.loginbTn = self.driver.find_element_by_id(self.loginEle)

        # print(self.userId,self.pwdId,self.loginId)

    def logIn(self):
        self.userbTn.clear()
        self.userbTn.send_keys(self.uname)
        self.pwdbTn.clear()
        self.pwdbTn.send_keys(self.pwd)
        self.loginbTn.click()
        time.sleep(30)

    def checkStatus(self):
        try:
            self.statusbTn = self.driver.find_element_by_id(self.status)
            if self.statusbTn.text == self.statusCode:
                print(self.statusCode)
                return False
        except:
            print("Pass")
            return True

    def quitBrowse(self):
        self.driver.quit()

if __name__ == '__main__':
    test1 = LoginTest()
    userInfo = {
       'uname':['Administrator','USERID'],
        'pwd':['Aa1','PASSW0RD']
    }
    test1.url = 'https://10.245.53.64/designs/imm/index.php'
    for uname,pwd in zip(userInfo['uname'],userInfo['pwd']):
        print(uname,pwd)
        test1.uname = uname
        test1.pwd = pwd
        test1.userEle = 'user'
        test1.pwdEle = 'password'
        test1.loginEle = 'btnLogin_label'
        test1.status = 'gblMessageDlgID_title'
        test1.statusCode = 'Login failed.'

    test1.openUrl()
    test1.findLoginEle()
    test1.logIn()
    if test1.checkStatus() == False:
        test1.quitBrowse()

 

posted on 2017-07-25 20:01  William126  阅读(2494)  评论(1编辑  收藏  举报

导航