PO模式

个人见解:Page Object已页面为单位来写测试用例,且元素定位操作和用例分离,便于查看和维护

以登录验证为例:在登录页进行验证,可分为登录成功和登录失败

代码

1.新建a1_login.py ---用于元素的定位和操作

from data.testdata.data_read import Test1
from public.base import Page
data1 = Test1().a1_data(sheet="a1_sta")

class login(Page):
    """
    User login page
    """
    url = "/blog/68"
    #模块1
    login_enter_loc = ("css selector", "a[class='btn btn-primary']")
    login_username_loc = ("css selector", "input[id='id_username']")
    login_password_loc = ("css selector", "input[id='id_password']")
    login_button_loc = ("css selector", "input[class='btn btn-primary pull-right']")
    def login_enter(self):
        self.click(self.login_enter())
    def login_username(self,username):
        self.clear_type(self.login_username_loc, username)
    def login_password(self,password):
        self.clear_type(self.login_password_loc, password)
    def login_button(self):
        self.click(self.login_button_loc)

    #模块2
    def user_login(self, username=data1[0]['username'], password=data1[0]['password']):
        self.open()
        self.login_enter()
        self.login_username(username)
        self.login_password(password)
        self.login_button()

    #模块3
    input_error_loc = ("xpath", "/html/body/div[2]/div/div/div/div[2]/form/span/ul/li']")
    user_login_success_loc = ("xpath", "//*[@id='dd']")
    def user_error_hint(self,actual):
        """用户名密码输入有误"""
        return self.is_text_in_element(self.input_error_loc, actual)
    def user_login_success(self,actual):
        """成功登录获取当前账号"""
        return self.is_text_in_element(self.user_login_success_loc, actual)

代码剖析:模块1写了定位器和点击、账密输入操作;模块2写了打开页面--输入账密--登录的流程;模块3写了定位器和获取当前页面的提示语用于之后用例断言

2.新建a1_login_sta.py ---写测试用例

import unittest
from data.testdata.data_read import Test1
from public.myunit import MyTest
from public import function
from public.base import Page
from test_case.page_obj.a1_login import login
data1 = Test1().a1_data(sheet = "a1_sta")

class loginTest(MyTest,Page):
    """用户密码登录"""

    def test_login1(self,expected=True):
        """
        用户名输入有误
        """
        po = login(self.driver)
        po.user_login(username=data1[1]['username'])
        self.assert_equal(po.user_error_hint(actual=data1[1]['result']),expected)
        function.insert_img(self.driver,data1[1]['screenshot_name'])

    @unittest.skip("跳过此用例")
    def test_login2(self,expected=True):
        """用户名密码正确,登录成功"""
        po = login(self.driver)
        po.user_login(username=data1[2]['username'], password=data1[2]['password'])
        self.assert_equal(po.user_login_success(actual=data1[2]['result']), expected)
        function.insert_img(self.driver, data1[2]['screenshot_name'])

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

代码剖析:这里写了两条测试用例,分别为登录成功和登录失败,断言时实际值取自a1_login.py,期望值取自Excel中的值使用了数据分离也便于管理

详见github: https://github.com/zhangmoumou1/selenium_python

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://zhangyanc.club/subject/article/selenium-po/

许可协议:署名-非商业性使用 4.0 国际许可协议