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'])
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库