Simplify the test code, use the Edge WebDriver

Bug553_LinShan
Lan Hui 2024-04-18 20:06:02 +08:00
parent 230e8e92dc
commit 9aa718b236
1 changed files with 14 additions and 23 deletions

View File

@ -1,35 +1,26 @@
# -*- coding: utf-8 -*-
# Run the docker image using the following command:
# docker run -d -p 4444:4444 selenium/standalone-chrome
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
import logging import logging
import time import time
import pytest import pytest
# driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX) @pytest.mark.parametrize("test_input,expected",
# HOME_PAGE = 'http://121.4.94.30:91/' [("test1", "test1"),
("'test2'", "test2"),
("“test3”", "test3"),
# 我使用的是Chrome浏览器所以我又通过安装webdriver-manager插件来配置浏览器的驱动 ("it's", "it's"),
# 我通过 'pip install webdriver-manager==4.00' 命令安装webdriver-manager并且设置其版本为4.00 ("hello,I'm linshan", ["hello","i'm","linshan"]),
driver = webdriver.Chrome(ChromeDriverManager().install()) ("Happy New Year", ["happy","new","year"]),
HOME_PAGE = 'http://127.0.0.1:5000/' ("My favorite book is 《Harry Potter》。", ["potter","harry","my","favorite","book","is"])])
driver.implicitly_wait(10) def test_bug553_LinShan(test_input,expected, driver, URL):
@pytest.mark.parametrize("test_input",("test1","'test2'","“test3”","it's","hello,I'm linshan","Happy New Year","My favorite book is 《Harry Potter》。"))
@pytest.mark.parametrize("expected",(["test1"],["test2"],["test3"],["it's"],["hello","i'm","linshan"],["happy","new","year"],["potter","harry","my","favorite","book","is"]))
def test_bug553_LinShan(test_input,expected):
try: try:
# 打开对应地址的网页 # 打开对应地址的网页
driver.get(HOME_PAGE) driver.get(URL)
# 浏览器最大窗口化 # 浏览器最大窗口化
driver.maximize_window() driver.maximize_window()
@ -40,11 +31,11 @@ def test_bug553_LinShan(test_input,expected):
# 将测试的数据输入到主页的textarea里 # 将测试的数据输入到主页的textarea里
driver.find_element_by_xpath("//textarea[@name='content']").send_keys(Keys.CONTROL, "a") driver.find_element_by_xpath("//textarea[@name='content']").send_keys(Keys.CONTROL, "a")
driver.find_element_by_xpath("//textarea[@name='content']").send_keys(test_input) driver.find_element_by_xpath("//textarea[@name='content']").send_keys(test_input)
time.sleep(2) time.sleep(1)
# 点击按钮获取单词 # 点击按钮获取单词
driver.find_element_by_xpath("//input[@value='get文章中的词频']").click() driver.find_element_by_xpath("//input[@value='get文章中的词频']").click()
time.sleep(2) time.sleep(1)
# 获取筛选后的单词 # 获取筛选后的单词
words = driver.find_elements_by_xpath("//p/a") words = driver.find_elements_by_xpath("//p/a")
@ -56,11 +47,11 @@ def test_bug553_LinShan(test_input,expected):
# 返回上一页网页 # 返回上一页网页
driver.find_element_by_xpath("//input[@value='确定并返回']").click() driver.find_element_by_xpath("//input[@value='确定并返回']").click()
time.sleep(2) time.sleep(0.1)
except Exception as e: except Exception as e:
# 输出异常信息 # 输出异常信息
logging.error(e) logging.error(e)
# 关闭浏览器 # 关闭浏览器
finally: finally:
driver.quit() driver.quit()