Selenium is based on Python web automation testing framework - PO model

1 Introduction:

About seleniumTesting frameworkThe first thing that comes to mind is the PO model. Let’s briefly talk about the PO model

2. Concept and understanding of PO model:

PO is a design idea. The code is organized in units of pages, and all information and related operations on this page are put into a class, so that specific Test cases become simple call and verify operations.

Advantages: Split and layered

Disadvantages: For complex business page layer changes, the case also needs to be changed.

3. Directory structure of PO model:

Among them, base_page is the basis of login_page and search_page. test_login calls login_page, login_page calls base_page, and similarly test_search.

4. PO code example:

base_page.py

from selenium.webdriver.support.wait import WebDriverWait
 
'''
这个类主要是完成所有页面的一些公共方法的封装
'''
class Action(object):
    #初始化
    def __init__(self,se_driver):
        self.driver = se_driver
 
    #定义open方法
    def open(self,url):
        self.driver.get(url)
        self.driver.maximize_window()
 
    #重写元素定位的方法
    def find_element(self,*loc):
        try:
            WebDriverWait(self.driver,20).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print("未找到%s"%(self,loc))
 
    #定义script方法,用于执行js脚本
    def script(self,src):
        self.driver.execute_script(src)
 
    #重写send_keys方法
    def send_keys(self,loc,value,clear_first=True,clik_first=True):
        try:
            if clik_first:
                self.find_element(*loc).click()
            if clear_first:
                self.find_element(*loc).clear()
                self.find_element(*loc).send_keys(value)
        except AttributeError:
            print("未找到%s"%(self,loc))

  login_page.py

from selenium.webdriver.common.by import By
from seleniumframework.PO import base_page
import time
 
class LoginPage(base_page.Action):
    link_loc = (By.LINK_TEXT,"登录")
    name_loc = (By.ID,"TANGRAM__PSP_8__userName")
    password_loc = (By.ID,"TANGRAM__PSP_8__password")
    submit_loc = (By.ID,"TANGRAM__PSP_8__submit")
 
    username_top = (By.LINK_TEXT,"hanxiaobei")
 
 
    def click_link(self):
        self.find_element(*self.link_loc).click()
        time.sleep(3)           #等待3秒,等待登录弹窗加载完成
 
    def run_case(self,value1,value2):
        self.find_element(*self.name_loc).send_keys(value1)
        self.find_element(*self.password_loc).send_keys(value2)
        time.sleep(20)          #手动输入验证码
        self.find_element(*self.submit_loc).click()
        time.sleep(5)           #等待5秒,登录后的页面加载完成
 
    def get_username(self):
        return self.find_element(*self.username_top).text

test_login.py

import unittest
from selenium import webdriver
from seleniumframework.PO.login_page import LoginPage
import time
 
class TestBaiduLogin(unittest.TestCase):
    """UI自动化登录"""
    def setUp(self):
        self.url = "http://www.baidu.com"
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(20)
        # self.verificationErrors = []
 
    def tearDown(self):
        time.sleep(5)
        self.driver.quit()
        # self.assertEqual([],self.verificationErrors)
 
    def test_login(self):
        """百度登录"""
        sp = LoginPage(self.driver)
        sp.open(self.url)
        sp.click_link()
        sp.run_case("hanxiaobei","xxxxxxx")
        self.assertEqual(sp.get_username(),"hanxiaobei",msg="验证失败!")

main.py is the main entry point for running

import unittest
import HTMLTestRunner
 
#相对路径
testcase_path = ".\\testcase"
report_path = ".\\report\\report.html"
def creat_suite():
    uit = unittest.TestSuite()
    discover = unittest.defaultTestLoader.discover(testcase_path,pattern="test_*.py")
    for test_suite in discover:
        # print(test_suite)
        for test_case in test_suite:
            uit.addTest(test_case)
    return uit
 
suite = creat_suite()
fp = open(report_path,"wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="测试结果",description="测试搜索结果")
runner.run(suite)
fp.close()

To solve the problem of report naming:

1 now = time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime(time.time()))
2 print(now)
3 report_path = ".\\report\\"+now+"report.html"

Test reportScreenshot:

search_page.py

from selenium.webdriver.common.by import By
from seleniumframework.PO import base_page
 
#继承base后既可以调用base的方法也可自己添加新的方法
class SearchPage(base_page.Action):
 
    #通过id进行定位元素
    search_loc = (By.ID,"kw")
 
    def run_case(self,value):
        #第一种利用原生的send_keys方法
        self.find_element(*self.search_loc).send_keys(value)
 
        #第二种利用二次封装的send_keys方法
        # self.send_keys(self.search_loc,value)

test_search.py

import unittest
from selenium import webdriver
from seleniumframework.PO.search_page import SearchPage
import time
 
class TestBaiduSearch(unittest.TestCase):
    """UI自动化搜索"""
    def setUp(self):
        self.url = "http://www.baidu.com"
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(20)
        self.verificationErrors = []
 
    def tearDown(self):
        time.sleep(5)
        self.driver.quit()
        self.assertEqual([],self.verificationErrors)
 
    def test_search(self):
        """搜索测试关键字"""
        sp = SearchPage(self.driver)
        sp.open(self.url)
        sp.run_case("测试")

Thank you to everyone who reads my article carefully. There is always a courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly:

 

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends, and this warehouse also accompanies Thousands of test engineers have gone through the most difficult journey, and I hope it can help you!Friends in need can click on the small card below to receive it 

Guess you like

Origin blog.csdn.net/okcross0/article/details/134863728