web自动化测试登录功能 python
1. BasePage.py
from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager class BasePage(object): # 初始化浏览器 def __init__(self): service = ChromeService(executable_path=ChromeDriverManager().install()) self.driver = webdriver.Chrome(service=service) # 打开网页 def get_url(self, url): self.driver.get(url) # 关闭浏览器 def quit_browser(self): self.driver.quit() # 输入文本 def send_keys(self, type_, xpath, context): self.driver.find_element(by=type_, value=xpath).send_keys(context) # 点击 def click(self, type_, xpath): self.driver.find_element(by=type_, value=xpath).click()
2. LoginPage.py
from selenium.webdriver.common.by import By from BasePage import BasePage class LoginPage(BasePage): def do_login(self, username, password, yan): type_ = By.XPATH username_xpath = '//*[@id="app"]/div/div/div/div/form/div[1]/div/div/div/input' password_xpath = '//*[@id="app"]/div/div/div/div/form/div[2]/div/div/div/input' yan_xpath = '//*[@id="app"]/div/div/div/div/form/div[3]/div/div/div/div[1]/div/input' login_btn_xpath = '//*[@id="app"]/div/div/div/div/form/div[4]/button' self.get_url("http://ip地址:90/") self.send_keys(type_, username_xpath, username) self.send_keys(type_, password_xpath, password) self.send_keys(type_, yan_xpath, yan) self.click(type_, login_btn_xpath)
# 等待页面加载完成
self.driver.implicitly_wait(20)
# 判断是否跳转到主页
assert self.driver.current_url == "http://120.25.153.197:90/home"
self.quit_browser()
3. test_web.py
from test_web import get_driver_manager_chrome from time import sleep import pytest from LoginPage import LoginPage from selenium.webdriver.common.by import By def test_web_01():# 登录操作 lg = LoginPage() lg.do_login("admin", "123", "11") # driver.find_element(by=By.XPATH, # value='//*[@id="app"]/div/div/div/div/form/div[1]/div/div/div/input').send_keys("admin") # driver.find_element(by=By.XPATH, # value='//*[@id="app"]/div/div/div/div/form/div[2]/div/div/div/input').send_keys("123") # driver.find_element(by=By.XPATH, # value='//*[@id="app"]/div/div/div/div/form/div[3]/div/div/div/div[1]/div/input').send_keys("123") # driver.find_element(by=By.XPATH, # value='//*[@id="app"]/div/div/div/div/form/div[4]/button').click() sleep(5) if __name__ == "__main__": pytest.main(['-s', '-v', '--capture=sys', 'test_web.py', '--clean-alluredir', '--alluredir=allure-results'])