Python+Selenium自动化测试之页码,前一页、后一页、翻页

思路:获取当前的总记录数,每页大小,算出总的页数,然后根据总页数实现翻页,在翻页时,可以获取每一页的内容存在列表中,为后面的模糊匹配做准备。

BasePage中封装

#   页面的页码信息,记录总数、分页大小
    def get_page_size(self,totalTtems,pagesize):
        totalitem=self.get_eleText(totalTtems)
        pagesize=self.get_eleText(pagesize)
        totalpage=math.ceil(int(totalitem)/int(pagesize))
        logging.info("返回总页数")
        return totalpage
    # 翻页处理,前一页,后一页
    def operate_page(self,totalpage,nextpage):
        if totalpage==1:
            logging.info("当前只有一页,无需翻页处理")
        else:
            logging.info("当前共有{0}页,可以翻页处理".format(totalpage))
            for i in range(1,totalpage):
                self.get_element(nextpage).click()
                logging.info("开始翻页,第{0}次翻页".format(i))

Locator信息

 # 页码信息
    # 总记录数
    totalTtems=(By.XPATH,"//label[@ng-bind='totalItems']")
    # 每页大小
    pagesize=(By.XPATH,"//label[@class='m-l-sm ng-scope']//following-sibling::label")
    # 前一页
    frontpage=(By.XPATH,"//a[@ng-click='selectPage(page - 1, $event)']")
    # 后一页
    nextpage=(By.XPATH,"//a[@ng-click='selectPage(page + 1, $event)']")
    # 当前的页数
    currentpage=(By.XPATH,"//a[@ng-click='selectPage(page.number, $event)']")
    # 前一页、后一页 首页、尾页

功能Page

  def next_page(self):
        totalpage=self.get_page_size(self.totalTtems,self.pagesize)
        self.operate_page(totalpage,self.nextpage)
    def current_page(self):
        currentpage=self.get_eleText(self.currentpage)
        return currentpage

TestCase:

from PageObeject.UserList_Page import UserlistPage
from TestData.UserListData import *
from Locator.UserList_Locator import UserList
import pytest
from Common import MyLog
import time
@pytest.mark.userfixtures("login_AUMS")
class TestUserlist:
    @pytest.mark.ss
    # 测试翻页功能
    def test_user_next_page(self,login_AUMS):
        UserlistPage(login_AUMS[0]).next_page()
        time.sleep(0.5)
        list=UserlistPage(login_AUMS[0]).userlist_data()
        # assert list==["2","3"]

 

你可能感兴趣的:(Selenium)