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.
PO design pattern
1. The advantages of the PO design pattern
PO mode has the following advantages:
1. It can reduce the repetitive writing of code.
2. The PO mode separates the positioning of page elements from the business operation process, and the change of interface elements does not need to modify the business logic code.
3. PO can improve code readability, high reusability, and maintainability.
2. Non-PO design pattern
For a better comparative analysis, let's first look at the non-PO design pattern:
The test_logintest.py code directly executes all process operations without any encapsulation
from time import sleep
import pytest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class Test_logintest():
def test_1(self):
driver=webdriver.Firefox()
driver.get("http://10.5.1.247/dvwa/login.php")
sleep(1)
driver.find_element_by_name("username").send_keys("admin")
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_name("Login").click()
sleep(2)
driver.find_element_by_link_text("XSS (Reflected)").click()
sleep(2)
driver.find_element_by_name("name").send_keys("nick")
sleep(1)
driver.find_element_by_xpath("//input[@value='Submit']").click()
sleep(1)
if name == ‘main’:
pytest.main(["-sq", "test_logintest.py"])
3. PO design pattern
Next, let's look at the PO design pattern
Basic layer: open browser, encapsulate element positioning
BasePage.py code:
from selenium.webdriver.common.by import By
class BasePage(object):
def __init__(self,driver,url=None):
self.driver=driver
self.url=url
if self.url!=None:
self.driver.get(self.url)
def by_name(self,id):
locator=(By.NAME,id)
ele=self.driver.find_element(*locator)
return ele
def by_linktext(self,linktext):
locator=(By.LINK_TEXT,linktext)
ele=self.driver.find_element(*locator)
return ele
def by_xpath(self,xpath):
locator=(By.XPATH,xpath)
ele=self.driver.find_element(*locator)
return ele
PO layer: page element acquisition, page basic operation
DvwaPage.py code:
from time import sleep
from myPytest.test_case.BasePage import *
class DvwaPage(BasePage):
def usernameText(self):
ele=self.by_name("username")
return ele
def passwordText(self):
ele=self.by_name("password")
return ele
def linkText(self):
ele=self.by_linktext("XSS (Reflected)")
return ele
def Submit(self):
ele =self.by_name("Login")
return ele
def login_dvwa(self,username,password):
self.usernameText().send_keys(username)
self.passwordText().send_keys(password)
self.Submit().click()
sleep(2)
def search(self,str):
#这里是通过调用linkText方法
self.linkText().click()
#这里是直接调用by_name函数
self.by_name("name").send_keys(str)
sleep(1)
# 这里是直接调用by_xpath函数
self.by_xpath("//input[@value='Submit']").click()
sleep(1)
Test case layer: business logic and data-driven execution
test_loginDvwa.py code:
import pytest
from myPytest.test_case.DvwaPage import *
from selenium import webdriver
class Test_loginDvwa():
def test_login(self):
self.driver=webdriver.Firefox()
self.url="http://10.5.1.247/dvwa/login.php"
username="admin"
password="password"
sr=DvwaPage(self.driver,self.url)
sr.login_dvwa(username,password)
sr.search("nick")
if name == ‘main’:
pytest.main(["-sq", "test_loginDvwa.py"])
The execution results are as follows:
4. Troubleshoot the cause of the error
When I execute the code, there will be an error of TypeError: 'module' object is not callable
code:
import pytest
from myPytest.test_case import DvwaPage
from selenium import webdriver
class Test_loginDvwa():
def test_login(self):
self.driver=webdriver.Firefox()
self.url="http://10.5.1.247/dvwa/login.php"
username="admin"
password="password"
sr=DvwaPage(self.driver,self.url)
sr.login_dvwa(username,password)
sr.search("nick")
After executing the code, an error will be reported:
sr=DvwaPage(self.driver,self.url)
E TypeError: ‘module’ object is not callable
Cause Analysis:
There are two ways to import modules in Python: import module and from module import *. After the former is imported, the module name needs to be limited when used, while the latter does not.
Solution:
from myPytest.test_case import DvwaPage
sr=DvwaPage.DvwaPage(self.driver,self.url)
or
from myPytest.test_case.DvwaPage import *
sr=DvwaPage(self.driver,self.url)
Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:
These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive