软测05/19记录一下学习过程|web自动化实战

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

web自动化实战

优化项目与项目分层

去哪网购票
1.优化选择出发时间的操作
2.封装查找元素的方法
3.读取excel表格中车次及乘车人账号信息
4.此处重点
:代码的重构
1)基础代码层–base_functions.py
2)业务代码层–quna_book.py
3)测试代码层–test_quna_book.py

# base_functions.py
from datetime import date, datetime, timedelta
from selenium import webdriver
import xlrd

driver = webdriver.Chrome("chromedriver.exe")


def get_driver():
    return driver


# 优化一:选择出发时间
# n就是第几天,n为0即今天,1为明天....以此类推
def date_n(n):
    return str(date.today() + timedelta(days=int(n)))


# 优化二:将寻找元素xpath方法进行封装
def xpath(element):
    return driver.find_element_by_xpath(element)


# 封装打开url操作
def open_url(url):
    driver.get(url)
    driver.maximize_window()


# 关闭浏览器
def close_bower():
    driver.quit()


# 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


if __name__ == '__main__':
    print(read_excel("open_file_e.xlsx", 0, True))
# quna_book.py
from selenium.webdriver.common.action_chains import ActionChains
from quna.base_functions import *
import time
from selenium.webdriver.common.keys import Keys


def book_ticket(start, end, n, i_d, past_word):
    driver = get_driver()
    url = "https://train.qunar.com/"
    open_url(url)
    time.sleep(2)
    action = ActionChains(driver)

    # 进入查询页面-出发、到达、日期、立即搜索
    xpath("//*[@name='fromStation']").send_keys(Keys.CONTROL, 'a')
    xpath("//*[@name='fromStation']").send_keys(Keys.BACKSPACE)
    xpath("//*[@name='fromStation']").send_keys(start)
    time.sleep(2)
    # 点击页面空白处
    action.move_by_offset(0, 0)
    action.click()
    action.perform()
    time.sleep(2)

    xpath("//*[@name='toStation']").send_keys(Keys.CONTROL, 'a')
    xpath("//*[@name='toStation']").send_keys(Keys.BACKSPACE)
    xpath("//*[@name='toStation']").send_keys(end)
    time.sleep(2)
    action.move_by_offset(0, 0)
    action.click()
    action.perform()
    time.sleep(2)

    # 清除原有时间
    xpath("//*[@name='date']").send_keys(Keys.CONTROL, 'a')
    xpath("//*[@name='date']").send_keys(Keys.BACKSPACE)
    time.sleep(2)

    # 输入需要的时间
    date_1 = date_n(n)
    xpath("//*[@name='date']").send_keys(date_1)
    time.sleep(2)

    action.move_by_offset(0, 0)
    action.click()
    action.perform()
    time.sleep(2)

    xpath("//*[@name='stsSearch']").click()
    time.sleep(5)

    # 进入车次列表页面
    xpath('//*[@id="list_listInfo"]/ul[2]/li[1]/div/div[7]/a[1]').click()
    time.sleep(2)

    # 进入订单信息页面--由于需要登陆所以这一步就模拟一下
    xpath('/html/body/form/div[2]/div[2]/div[4]/div[1]/div[1]/div/div[1]/a').click()
    time.sleep(2)
    # driver.switch_to.frame(driver.find_element_by_xpath("/html/body/div[2]"))
    xpath('/html/body/div[2]/div/div[1]/label').send_keys(i_d)
    time.sleep(2)
    xpath('/html/body/div[2]/div/div[2]/label').send_keys(past_word)
    time.sleep(2)
    xpath('/html/body/div[2]/div/div[6]/a').click()

    time.sleep(3)
    close_bower()
# test_quna_book.py
from quna.quna_book import *
from quna.base_functions import *
import pytest

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


@pytest.mark.parametrize(["start", "end", "n", "i_d", "past_word"], data)
def test_book_ticket(start, end, n, i_d, past_word):
    book_ticket(start, end, n, i_d, past_word)


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值