软测05/20记录一下学习过程|web自动化实战po模式

2022/05/20学习内容
整理时间:2022/05/21
参考资料:https://www.bilibili.com/video/BV1NM4y1K73T?p=1&spm_id_from=333.851.header_right.history_list.click

web自动化实战

po模式:去哪网购票

1.po模式:page object页面对象模型
1)将页面当成对象
2)将页面中业务当到一起
3)web和app测试中使用

2.优点
1)页面分层,将页面元素与业务逻辑进行区分
2)方便复用对象
3)每个页面都是一个独立测试用例
4)自动化更容易

3.po模式
1)base层
2)common层
3)data层
4)logs层
5)po层
6)testcase层
7)reports层
8)config层

# base.py
from selenium.webdriver.chrome.webdriver import WebDriver


class Base():
    def __init__(self, driver):
        self.driver = driver  # type: WebDriver

    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.quit()
# functions.py
from datetime import date, datetime, timedelta
import xlrd


# ishead 是否有标题 有为True 无默认 False
def read_excel(filename, index, ishead=False):
    e = xlrd.open_workbook(filename)
    sheet = e.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_ticket_page.py
from quna_po.base.base import Base
from quna_po.common import *
import time
from selenium.webdriver.common.action_chains import ActionChains
from quna_po.common.functions import *
from selenium.webdriver.common.keys import Keys


class BookTicket(Base):

    def book_start(self):
        self.xpath("//*[@name='fromStation']").send_keys(Keys.CONTROL, 'a')
        self.xpath("//*[@name='fromStation']").send_keys(Keys.BACKSPACE)
        return self.xpath("//*[@name='fromStation']")

    def book_end(self):
        self.xpath("//*[@name='toStation']").send_keys(Keys.CONTROL, 'a')
        self.xpath("//*[@name='toStation']").send_keys(Keys.BACKSPACE)
        return self.xpath("//*[@name='toStation']")

    def book_date(self, date):
        self.xpath("//*[@name='date']").send_keys(Keys.CONTROL, 'a')
        self.xpath("//*[@name='date']").send_keys(Keys.BACKSPACE)
        self.xpath("//*[@name='date']").send_keys(date)

    def book_button(self):
        return self.xpath("//*[@name='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 = "https://train.qunar.com/"
        # self.open_url(url)
        # time.sleep(2)

        # 进入查询页面-出发、到达、日期、立即搜索
        self.book_start().send_keys(start)
        time.sleep(2)
        # 点击页面空白处
        self.move_click()
        time.sleep(2)

        self.book_end().send_keys(end)
        time.sleep(2)
        # 点击页面空白处
        self.move_click()
        time.sleep(2)

        # 输入需要的时间
        self.book_date(date)
        time.sleep(2)
        # 点击页面空白处
        self.move_click()
        time.sleep(2)

        # 点击搜索
        self.book_button().click()
        time.sleep(5)

# book_list_page.py
from quna_po.base.base import Base
import time


class BookList(Base):

    def book_buy(self):
        return self.xpath('/html/body/div[4]/div/div[1]/ul[2]/li[1]/div/div[7]/a[1]')

    def book_list(self):
        self.book_buy().click()
        time.sleep(2)
# book_order_page.py
from quna_po.base.base import Base
import time


class BookOrder(Base):

    # 马上登录
    def order_log1(self):
        return self.xpath('/html/body/form/div[2]/div[2]/div[4]/div[1]/div[1]/div/div[1]/a')

    # 输入账号
    def order_id(self):
        return self.xpath('/html/body/div[2]/div/div[1]/label')

    # 输入密码
    def order_past_word(self):
        return self.xpath('/html/body/div[2]/div/div[2]/label')

    # 登录
    def order_log2(self):
        return self.xpath('/html/body/div[2]/div/div[6]/a')

    def book_order(self, i_d, past_word):
        self.order_log1().click()
        time.sleep(2)
        self.order_id().send_keys(int(i_d))
        time.sleep(2)
        self.order_past_word().send_keys(past_word)
        time.sleep(2)
        self.order_log2().click()

        time.sleep(3)
        self.close()
# test_book.py
from quna_po.po.book_ticket_page import BookTicket
from quna_po.po.book_list_page import BookList
from quna_po.po.book_order_page import BookOrder
from selenium import webdriver
from quna_po.common.functions import *
import pytest
import time

data = read_excel("../data/open_file_e.xlsx", 0, True)


class Test_Book():
    # 夹具
    def setup(self):
        self.driver = webdriver.Chrome("../chromedriver.exe")
        url = "https://train.qunar.com/"
        self.driver.get(url)
        self.driver.maximize_window()

    @pytest.mark.parametrize(["start", "end", "n", "i_d", "past_word"], data)
    def test_01(self, start, end, n, i_d, past_word):
        ticket = BookTicket(self.driver)
        ticket.book_ticket(start, end, date_n(n))
        time.sleep(2)

        list_b = BookList(self.driver)
        list_b.book_list()
        time.sleep(2)

        order = BookOrder(self.driver)
        order.book_order(i_d, past_word)
        time.sleep(2)
    # 夹具


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


if __name__ == '__main__':
    pytest.main(["-s", "test_book.py"])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值