selenium + python automated testing - read excel data selenium + python automated testing - Login

1, excel data (note: when pure digital data, to set it to text)

2, to read the package file functions excel

File name: read_excel.py

Import to xlrd 

class ReadExcel ():
     DEF  the __init__ (Self, excelPath, sheetName = " Sheet1 " ): 
        self.data = xlrd.open_workbook (excelPath) 
        self.table = self.data.sheet_by_name (sheetName)
         # Get the first row as the key value 
        self.keys = self.table.row_values (0)
         # Get the number of rows 
        self.rowNum = self.table.nrows
         # acquires the total number of columns 
        self.colNum = self.table.ncols 

    DEF dict_data (Self):
         IF self.rowNum <= 1:
             Print ( " total number of lines is less than. 1 " )
         the else : 
            R & lt = [] 
            J =. 1
             for I in Range (-self.rowNum. 1 ): 
                S = {}
                 # from the values corresponding to the values of the second row takes 
                values = self.table. row_values (J)
                 for X in (self.colNum) Range: 
                    S [self.keys [X]] = values [X] 
                r.append (S) 
                J + =. 1
        return r

if __name__ == "__main__":
    filepath = "D://gmy//seleniumTest//sel_test//cases//ddt_data.xlsx"
    data = ReadExcel(filepath)
    print(data.dict_data())

3, test case

File name: test_readexcel.py

import ddt
import unittest
from common.read_excel import ReadExcel
import os
from selenium import webdriver
from common.base import Base

curpath = os.path.dirname(os.path.realpath(__file__))
excelpath = os.path.join(curpath,"login.xlsx")
print(excelpath)
data = ReadExcel(excelpath)
testdata = data.dict_data()

@ddt.ddt
class Test(unittest.TestCase):
    # 定位手机号
    username = ("name", "loginName")
    # 定位密码
    psw = ("name", "loginPassWord")
    # 定位登录按钮
    loginbutton = ("id", "loginBtn")
    # 定位提示信息
    message = ("className", "toast-message")

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        cls.login = Base(cls.driver)
    def setUp(self):
        self.driver.get("url地址")
    def tearDown(self):
        #清空cookies
        self.driver.delete_all_cookies()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    @ddt.data(*testdata)
    def test_login(self,data):
        '''测试登录{0}'''
        username = data['user']
        password = data['psw']
        exp = data['exp']
        # 输入手机号
        self.login.send(self.username, username)
        # 输入密码
        self.login.send(self.psw, password)
        # 点击登录按钮
        self.login.click(self.loginbutton)
        # 获取结果
        result = self.login.find(self.message).text
        print("我是结果:", result)
        self.assertEqual(result, exp)

if __name__ == '__main__':
    unittest.main()

base.by 文件查看selenium+python自动化测试--登录

4、运行结果

 

 最后,附带读取excel函数封装参考地址:https://www.cnblogs.com/yoyoketang/p/6701950.html

Guess you like

Origin www.cnblogs.com/yudx/p/11277921.html