Implementation of PageObject+unitetest Framework

Keywords: Selenium PHP Firefox

PageObject+unittest framework

Idea: The change of UI layer automated test front page results in the failure of the whole test code and the difficulty of test script maintenance.

Solution: Page element and operation of page element are encapsulated separately to realize the separation of page element and operation of page element and script.

The general idea of the framework is as follows:
1.BasePage
Basic Page Module, Packaging Basic Method and Page Element Location Method

2.LoginPage
Login module, including all elements to be operated by login, encapsulation of element method of login

3.test_login
Testing login module, introducing unittest module, realizing assertion operation, verifying different scenarios of login.
Including successful login scenarios and failed login scenarios

The code of the BasePage module is as follows:

#coding=utf-8
from  selenium import webdriver

class Page():
    def __init__(self,driver):
        self.driver=driver

    def open(self):
        self.driver.get("http://127.0.0.1:82/bugfree/bugfree3.0.4/index.php/site/login")

    def find_element(self,*loc):
        return self.driver.find_element(*loc)



if __name__ == '__main__':
     pass

The code for the Login_Page module is as follows:

#coding=utf-8
from  BasePage import Page
from  selenium.webdriver.common.by import By

class LoginPage(Page):
    username_loc=(By.ID,'LoginForm_username')
    password_loc=(By.ID,'LoginForm_password')
    submit_loc=(By.ID,'SubmitLoginBTN')
    null_password_loc=(By.ID,'login-error-div')
    pass_loc=(By.CLASS_NAME,'user-info')

    def type_username(self,username):
        self.find_element(*self.username_loc).send_keys(username)

    def type_password(self,password):
        self.find_element(*self.password_loc).send_keys(password)

    def  type_submit(self):
        self.find_element(*self.submit_loc).click()

    def  login_null_password(self):
        return  self.find_element(*self.null_password_loc).text

    def  login_pass(self):
        return  self.find_element(*self.pass_loc).text

    def  login_action(self,username,password):
        po=LoginPage(self.driver)
        po.open()
        po.type_username(username)
        po.type_password(password)
        po.type_submit()

The code for the test_login module is as follows:

#coding=utf-8
from LoginPage import *
from  selenium import webdriver
import unittest

class  T(unittest.TestCase):
    def  setUp(self):
        self.driver = webdriver.Firefox()
    def test_1(self):
        po=LoginPage(self.driver)
        po.login_action('admin',' ')
        self.assertEqual(po.login_null_password(),u'Password cannot be blank.  ')
    def test_2(self):
        po=LoginPage(self.driver)
        po.login_action('admin','123456')
        self.assertIn(u'Welcome',po.login_pass())

    def tearDown(self):
        self.driver.quit()
        
if __name__ == '__main__':
    unittest.main()














Posted by Shovinus on Mon, 30 Sep 2019 02:20:10 -0700