Web automated testing (advanced learning part 1)

Table of contents

1. Read csv file

2. Data ddt parameterization

2.1 Implementation steps

2.2 Code example (unittest framework)

3. PO mode

3.1 Introduction

3.2 Comparison between non-po mode and po mode

3.3 Po layering and mutual relationship

3.4 Code example (non-PO mode)

3.5 Code example (PO mode)

3.5.1 Base Page

3.5.2 PO layer

3.5.3 Test case layer

1. Read csv file

#通常文件csv
1.找到文件
url_path = os.path.join(os.path.dirname(__file__), "..", "..", "data", file_name)
2.打开这个文件
with open(url_path, "r", encoding="utf8") as file_data
3.读取这个文件中的内容:csv库
file_value = csv.reader(file_data)
4.把读取到的内容转换成python中的列表 
list_file = list(file_value)[line]
5.把读取到的内容返回出去(按行返回,1行就表示1个用例) 
return list_file
#当文件是配置文件时,第四步需要把数据转化成字典
4.把读取到的内容变成字典
dict_file = dict(file_value)
5.返回这个字典信息,所有一起返回
return list_file

2. Data ddt parameterization

2.1 Implementation steps

  1. Create test data
  2. Encapsulate the method of reading data, retain the interface/properties called by the test script (what parameters need to be passed to the script)
  3. Write automated test scripts
  4. The script calls the encapsulated module for processing data files and introduces test data
  5. Execute test scripts and analyze test results

2.2 Code example (unittest framework)

import unittest
import ddt
from openpyxl import load_workbook
from selenium import webdriver

def Excelread():
    driver= load_workbook('sanmu.xlsx')
    sheet = driver['登录']
    rows = sheet.max_row
    usernamelist = []
    #取出表格中有用的数据信息
    for one in range(2, rows+1):
        #取出姓名
        name = sheet.cell(one, 2).value
        password = sheet.cell(one, 3).value
        usernamelist.append([name, password])
    driver.close()
    return usernamelist

@ddt.ddt
class Logincases(unittest.TestCase):
    a = 5
    b = 2
    @classmethod
    def setUpClass(cls) -> None:
    #数据可以是元祖,列表,字典
    value = Excelread()
    #装饰器@ddt.data是用来将value拆成['姓名数据', '密码数据']
    @ddt.data(*value)
    #unpack则是将['姓名数据', '密码数据']两个元素分别赋值传入给uname和passwd
    @ddt.unpack
    def test_login(self, uname, passwd):
        self.driver = webdriver.Chrome()
        self.driver.get('登录网站')
        self.driver.implicitly_wait(15)
        self.driver.find_element_by_name('userName').send_keys(uname)
        self.driver.find_element_by_name('userPass').send_keys(passwd)
        self.driver.find_element_by_css_selector('button[οnclick="login();"]').click()
    @classmethod
    def tearDownClass(cls) -> None:
        self.driver.quit()

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

3. PO mode

3.1 Introduction

    PO mode: Page Object is a page object design mode, which is considered a relatively good design mode. In this design pattern, functional classes (PageObjects) represent the logical relationship between each page. The core idea is layering, loose coupling, script reuse, and easy maintenance of scripts.

3.2 Comparison between non-po mode and po mode

Non-PO mode PO mode
Process Oriented Linear Script POM separates page element positioning from business operation process. Achieve loose coupling.
poor reusability The change of UI elements does not need to modify the business logic code. You only need to find the corresponding PO page to modify the location, and the data code is separated
poor maintenance PO can improve the readability, high reusability and maintainability of our test code.

3.3 Po layering and mutual relationship

  1. Basic layer BasePage: Encapsulate some of the most basic selenium's native api methods, element positioning, frame jumping, etc.
  2. PO layer: element positioning, element object acquisition, page action
  3. Test case layer: business logic, data-driven
  4. The relationship between the three: the PO layer inherits the base layer, and the test case layer calls the PO layer.

3.4 Code example (non-PO mode)

import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By

class PracTesting( unittest.TestCase ):

    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        cls.driver = webdriver.Chrome()
        # 加载百度首页
        cls.driver.get( 'http://www.baidu.com' )
        cls.driver.fullscreen_window()
    def test01(self):
        self.driver.implicitly_wait(5)
        # 在百度搜索栏中输入软件测试
        self.driver.find_element( By.ID, 'kw' ).send_keys( '软件测试' )
        # 点击百度一下按钮
        self.driver.find_element( By.ID, 'su' ).click()
    def test02(self):
        time.sleep(2)
        #返回搜索页面
        self.driver.back()
        time.sleep(2)
        # 在百度搜索栏中输入接口测试
        self.driver.find_element( By.ID, 'kw' ).send_keys( '接口测试' )
        # 点击百度一下按钮
        self.driver.find_element( By.ID, 'su' ).click()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()


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

3.5 Code example (PO mode)

3.5.1 Base Page

from selenium import webdriver

class BasePage:
    #构造方法
    def __init__(self):
        # 打开浏览器
        self.driver = webdriver.Chrome() 
        # 加载百度首页
        self.driver.get('http://www.baidu.com')

    #封装定位元素
    def find_ele(self,*args):
        ele = self.driver.find_element(*args)
        return ele

3.5.2 PO layer

from selenium.webdriver.common.by import By
from base.base_page import BasePage

class BaiduPage(BasePage):
    #元素定位,
    baidu_text_loc = (By.ID, 'kw')
    baidu_submit_loc = (By.ID, 'su')
    #获得元素对象,
    def get_text_obj(self):
        ele = self.find_ele(*BaiduPage.baidu_text_loc)
        return ele
    def get_submit_obj(self):
        ele = self.find_ele(*BaiduPage.baidu_submit_loc)
        return ele
    #页面操作
    def search(self,search_string):
        self.get_text_obj().send_keys(search_string)
        self.get_submit_obj().click()

3.5.3 Test case layer

from ddt import ddt, data
from po.baidu_page import BaiduPage

@ddt
class BaiduTest(unittest.TestCase):

    @data('软件测试','接口测试')
    def test01(self,seaString):
        BaiduPage().search(seaString)
        time.sleep(5)

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

Guess you like

Origin blog.csdn.net/m0_58807719/article/details/130036467