Web自动化测试

https://pdf.ceshiren.com/ceba/project/aitc_xian_2025/web_auto/

https://www.selenium.dev/zh-cn/documentation/

https://ck.course.ceba.ceshiren.com/courses/测开班/Python测开班v2/详细大纲/#web_1

https://www.selenium.dev/selenium-ide/

pip install selenium
from selenium import webdriver


driver = webdriver.Chrome()

driver.get("http://selenium.dev")

$(‘[title=“搜索”]’)

$(‘#search-button’)

$(‘button#search-button’)

css selector

python -m pip install pytest
python -m pip install selenium

python -m pytest  /tmp/test_untitled.py

如果遇到不能联网下载driver,可以使用这个下载

https://github.com/SeleniumHQ/selenium_manager_artifacts/releases

./selenium-manager --debug --browser chrome --browser-version 100 --avoid-browser-download --driver-mirror-url=https://registry.npmmirror.com/-/binary/chromedriver/
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

import os

os.environ['SE_CHROME_MIRROR_URL'] = "https://registry.npmmirror.com/-/binary/chromedriver/"


class TestUntitled():
    def setup_method(self, method):
        self.driver = webdriver.Firefox()
        self.vars = {}

    def teardown_method(self, method):
        self.driver.quit()

    def test_untitled(self):
        self.driver.get("https://ceshiren.com/")
        self.driver.set_window_size(1680, 945)
        self.driver.find_element(By.ID, "search-button").click()
        self.driver.find_element(By.CSS_SELECTOR, ".d-icon-sliders-h").click()
        self.driver.find_element(By.CSS_SELECTOR, ".search").click()
        self.driver.find_element(By.CSS_SELECTOR, ".search").send_keys("ai")
        self.driver.find_element(By.CSS_SELECTOR, ".search-cta").click()

python环境

  • python
  • IDE pycharm vscode
  • uv

selenium环境

  • selenium IDE firefox edge chrome
  • record
  • pytest
  • pip install pytest selenium
  • python -m pytest or pytest demo.py
  • driver selenium manager .cache/selenium/manager/0.4.23/selenium-manager

自动化测试

  • selenium 打开网址
  • ide 默认能跑的场景
  • ide默认不能跑的场景 元素定位的基本知识
  • 写代码 pytest script
  • page object
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

import os

os.environ['SE_CHROME_MIRROR_URL'] = "https://registry.npmmirror.com/-/binary/chromedriver/"
os.environ['SE_DEBUG'] = 'true'


class TestUntitled():
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.set_page_load_timeout(5)
        self.driver.implicitly_wait(5)
        self.vars = {}

    def teardown_method(self, method):
        self.driver.quit()

    def test_untitled(self):
        self.driver.get("https://ceshiren.com/")
        self.driver.set_window_size(1680, 945)
        self.driver.find_element(By.ID, "search-button").click()
        self.driver.find_element(By.CSS_SELECTOR, ".d-icon-sliders-h").click()
        self.driver.find_element(By.CSS_SELECTOR, ".search").click()
        self.driver.find_element(By.CSS_SELECTOR, ".search").send_keys("ai")
        self.driver.find_element(By.CSS_SELECTOR, ".search-cta").click()
        assert 'ai' in self.driver.find_element(
            By.CSS_SELECTOR, ".topic-title").text.lower()