selenium测试实践

1、登录测试用例和其他测试用例的驱动分开考虑

2、除登录测试用例外,其他用例实现浏览器复用技术

3、使用return返回一个新的页面,参数返回driver

4、 def init(self, driver: WebDriver): 参数这么写方便代码书写

5、测试文件中使用setup实例化主页面

6、创建基类,用于页面继承,初始化页面和公有方法

7、显示等待相对复杂的元素,那么简单的元素必然可以加载完成,可被点击

8、核心代码:

# 基类
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:

    _driver = None
    _base_url = ""

    def __init__(self, driver: WebDriver = None):
        if driver is None:
            # options = Options()
            # options.debugger_address = "127.0.0.1:9888"
            # self._driver = webdriver.Chrome(options=options)
            self._driver = webdriver.Chrome()
            self._driver.implicitly_wait(3)
        else:
            self._driver = driver
        if self._base_url != "":
            self._driver.get(self._base_url)

    def find(self, by, locator):
        return self._driver.find_element(by, locator)

    def finds(self, by, locator):
        return self._driver.find_elements(by, locator)

    def wait_for_click(self, locator, time = 10):
        WebDriverWait(self._driver, time).until(expected_conditions.element_to_be_clickable(locator))

    def wait_for_elem(self, condtions, time=10):
        WebDriverWait(self._driver, time).until(condtions)
# 用于测试登录的首页

from selenium.webdriver.common.by import By

from selenium_xd.page.base_page import BasePage
from selenium_xd.page.login.login import Login


class Index(BasePage):
    _base_url = 'https://xdclass.net/#/index'

    def go_login(self):
        self._driver.find(By.CSS_SELECTOR, 'div.login > span:nth-child(2)').click()
        return Login(self._driver)
# 登录页面类

from selenium.webdriver.common.by import By

from selenium_xd.page.base_page import BasePage
from selenium_xd.page.login.register import Register


class Login(BasePage):
    def go_register(self):
        self._driver.find(By.CSS_SELECTOR, '.f_r').click()
        return Register(self._driver)
# 主页类:可以使用浏览器复用技术

from selenium.webdriver.common.by import By

from selenium_xd.page.base_page import BasePage
from selenium_xd.page.main.class_center import ClassCenter


class Main(BasePage):
    _base_url = 'https://xdclass.net/#/index'

    def go_classCenter(self):
        locator = (By.CSS_SELECTOR, 'div.m_navitems.f_l > ul > li:nth-child(2)')
        self.wait_for_click(locator, 9)
        self.find(*locator).click()
        # 分页操作机制
        # content: str = self.find(*locator).text
        # cul_page, total_page = [int(x) for x in content.split('/', 1)]
        # cul_page = [int(x) for x in content.split('/', 1)][0]
        return ClassCenter(self._driver)
# 测试代码类

class TestRegister:
    def setup(self):
        self.index = Index()

    def test_register_from_index(self):
        self.index.go_login().go_register().register("moli", 'xsjhxsjkh')

    def test_register_from_login(self):
        self.index.go_register().register("moli", 'xsjhxsjkh')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值