selenium+pytest.fixture

import time

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By


@pytest.fixture(scope='session')
def driver():
    dr = webdriver.Chrome()
    dr.maximize_window()
    yield dr
    time.sleep(30)
    dr.quit()


@pytest.fixture()
def login(driver):
    driver.get('http://www.baidu.com')
    login_phone = (By.XPATH, '//form/div[1]/div/div/div/div/input')
    login_pwd = (By.XPATH, '//input[@placeholder="请输入密码"]')
    login_bt = (By.XPATH, '//form/div[4]/div/div[1]/div/button')
    driver.find_element(*login_phone).send_keys('11111111111')
    driver.find_element(*login_pwd).send_keys('654321')
    driver.find_element(*login_bt).click()


@pytest.fixture()
def click_menu(driver, login):
#这里用try的原因是二级菜单默认是隐藏的,批量执行用例的过程中涉及到来回切换菜单,从一级到二级,从二级到二级,封装到这里之后,默认先try二级,不行再从一级到二级
    def func(menu, sub_menu):
        menu2 = "//*[text()=' " + menu + "  ']"
        menufirst = (By.XPATH, menu2)
        menusecond = (By.LINK_TEXT, sub_menu)
         try:
            driver.find_element(*menusecond ).click()
        except Exception as result:
            print(result)
            driver.find_element(*menufirst ).click()
            time.sleep(10)
            driver.find_element(*menusecond ).click()
     return func

你可能感兴趣的:(selenium,selenium,python,测试工具,pytest.fixture)