自动化软件测试 -- Po模式

Po模式是指页面对象模型(Page Object),一般用在web测试,app测试中。

优点:

1、页面分层,页面元素和业务的逻辑进行区分

2、方便复用对象

3、每个页面都是一个独立的测试用例

4、自动化变的更容易

分层:

base层主要存放一些基础的内容

common层是存放读取文件、日期处理等公共内容

data层存放数据文件

log层存放日志

po层存放页面的业务代码

testcase层存放测试代码

reports层存放测试报告

config存放配置文件

自动化软件测试 -- Po模式_第1张图片

base.py 

class Base():
    def __init__(self,driver):
        self.driver = driver
    def byname(self,element):
        return self.driver.find_element_by_name(element)
    def xpath(self,element):
        return self.driver.find_element_by_xpath(element)
    def open_url(self,url):
        self.driver.get(url)
        self.driver.maximize_window()
    def close(self):
        self.driver.close()

function.py

from datetime import date, datetime, timedelta
import xlrd

# ishead 是否有标题,有True,没有就False默认没有
def read_excel(filename, index, ishead=False):
    xlsx = xlrd.open_workbook(filename)
    sheet = xlsx.sheet_by_index(index)

    data = []
    for i in range(sheet.nrows):
        if i == 0:
            if ishead:
                continue
        data.append(sheet.row_values(i))

    return data


def date_n(n):
    return str(date.today() + timedelta(days=int(n)))

book_list_page.py

from qunar_po.base.base import Base
import time
class Booklist(Base):
    def book_buy(self):
        return self.xpath('//*[@id="list_listInfo"]/ul[2]/li/div/div[7]/a[1]')
    def book_list(self):
        self.book_buy().click()
        time.sleep(2)

book_order_page.py

from qunar_po.base.base import Base
import time

class BookOrder(Base):
    def book_name(self):
        return self.byname("contact_name")
    def book_phone(self):
        return self.byname("contact_phone")


    def book_order(self,name,phone):
        self.book_name().send_keys(name)
        self.book_phone().send_keys(phone)
        time.sleep(2)

book_ticket_page.py

from qunar_po.base.base import Base
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time

class Bookticket(Base):
    def book_start(self):
        return self.byname("fromStation")
    def book_end(self):
        return self.byname("toStation")
    def book_date(self,date):
        self.byname("date").send_keys(Keys.CONTROL, "a")
        self.byname("date").send_keys(Keys.BACKSPACE)
        self.byname("date").send_keys(date)
    def book_button(self):
        return self.byname("stsSearch")

    def move_click(self):
        action = ActionChains(self.driver)
        action.move_by_offset(0, 0)
        action.click()
        action.perform()

    def book_ticket(self,start, end, date):
        #打开url
        # url = "https://train.qunar.com/"
        # self.open_url(url)

        # 输入出发站、到达站,并且在旁边点一下
        self.book_start().send_keys(start)
        self.move_click()

        self.book_end().send_keys(end)
        self.click()


        #先清除掉之前的日期,然后再输入新的日期
        self.book_date(date)
        self.book_click()

        #点击立即搜索
        self.book_button().click()

        time.sleep(2)

test_book.py

from qunar_po.po.book_ticket_page import Bookticket
from qunar_po.po.book_list_page import Booklist
from qunar_po.po.book_order_page import BookOrder
from selenium import webdriver
from qunar_po.common.function import read_excel,date_n
import pytest

data = read_excel("D:\\桌面文件\\code\\code1\\qunar_po\\data\\date.xlsx",0,True)
class Test_Book():
    def setup(self):
        self.driver = webdriver.Chrome("D:\\桌面文件\\code\\code1\\qunar_po\\chromedriver.exe")
        url = "https://train.qunar.com/"
        self.driver.get(url)
        self.driver.maximize_window()

    @pytest.mark.parametrize(["start", "end", "n", "name", "phone"], data)
    def test_01(self,start,end,n,name,phone):
        ticket = Bookticket(self.driver)
        ticket.book_ticket(start,end,date_n(n))

        list_b = Booklist(self.driver)
        list_b.book_list()
        order = BookOrder(self.driver)
        order.book_order(name,phone)


    def teardown(self):
        self.driver.quit()

if __name__ =='__main__':
    pytest.main(["-s","test_book.py"])

你可能感兴趣的:(自动化软件测试,自动化,运维,selenium,python,测试用例)