我遇到了这样的观点:自动测试中的 time.sleep 是一种不好的做法,请告诉我为什么以及可以使用哪些替代方法

问题描述 投票:0回答:1

它看起来像这样,并且没有任何其他方式工作。

请介绍一下等待页面上的元素和等待页面加载的函数

buttonEnter = driver.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/div/div/form/div[3]/button') 
buttonEnter.click()

time.sleep(10)

buttonLeftMenu = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[1]/div/div/button")
buttonLeftMenu.click()

buttonItemSelection = driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/div[1]/div[2]/div/div/div/ul/li[6]")
buttonItemSelection.click()

time.sleep(10)

buttonItemLoyaltyPrograms = driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/div[1]/div[2]/div/div/div/ul/li[6]/div/ul/li[4]/a")
buttonItemLoyaltyPrograms.click()

time.sleep(10)

searchLoyaltyPrograms = driver.find_element(By.XPATH,"/html/body/div/div[2]/div[1]/div[2]/div[2]/form/div[2]/div/div/div/div[1]/div[1]/div/input")
searchLoyaltyPrograms.send_keys("Шаблонная программа")

buttonSearh = driver.find_element(By.XPATH, "/html/body/div/div[2]/div[1]/div[2]/div[2]/form/div[2]/div/div/div/div[1]/div[1]/div/input")
buttonSearh.click()

I tried it like this, swears that it cannot find such an element and crashes

wait.until(EC.presence_of_element_located((By.CLASS_NAME, "loaded")))
python python-3.x selenium-webdriver automated-tests autotest
1个回答
1
投票

在测试自动化中使用 time.sleep 通常被认为是一种不好的做法,原因如下:

1。不可靠的计时: 使用 time.sleep 的固定延迟假设某个操作总是花费相同的时间。然而,测试的执行速度可能会因系统负载、网络延迟或被测应用程序的性能等因素而有所不同。这可能会导致测试由于不正确的时序假设而失败。

2。增加测试执行时间: time.sleep 会为您的测试脚本添加固定延迟,即使您正在等待的元素或条件较早变得可用。这不必要地增加了测试的总体执行时间。

3.可维护性差: 如果被测应用程序发生变化,例如 UI 更新或性能改进,使用固定睡眠延迟可能会导致您的测试变得不可靠。您需要手动更新睡眠持续时间,这可能非常耗时且容易出错。

为了克服这些问题,建议在测试自动化中使用显式等待。显式等待允许您在继续测试之前等待满足特定条件。它们更可靠,可以更好地控制测试时间。 Selenium WebDriver 提供了 WebDriverWait 类来实现显式等待。

这是在 Python 中使用 Selenium WebDriver 使用显式等待而不是 time.sleep 的示例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Configure the webdriver (change the path to your specific driver)
driver = webdriver.Chrome('/path/to/chromedriver')

# Navigate to the web page
driver.get('https://example.com')

# Define the target element you want to find (change the locator as needed)
target_element_locator = (By.CSS_SELECTOR, '#target-element')

# Wait until the target element is present
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located(target_element_locator))

# Perform further actions with the element if needed
# ...

# Close the browser
driver.quit()

在此代码中,我们使用 WebDriverWait 等待目标元素出现,然后再继续执行进一步操作。 EC.presence_of_element_ located 预期条件检查元素是否存在于 DOM 中。 WebDriverWait 实例将持续轮询 DOM,直到找到元素或发生超时。

以这种方式使用显式等待可以在测试脚本和被测应用程序之间提供更可靠、更高效的同步,而无需依赖固定的睡眠持续时间。

以下是常用的显式等待列表:

presence_of_element_ located:等待元素出现在 DOM 中。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.presence_of_element_located((By.ID, 'element-id')))

visibility_of_element_ located:等待元素可见。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'element-selector')))

invisibility_of_element_ located: 等待元素不可见。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, 'element-class')))

element_to_be_clickable:等待元素可点击(可见并启用)。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.element_to_be_clickable((By.XPATH, 'element-xpath')))

text_to_be_present_in_element: 等待元素中出现特定文本。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.text_to_be_present_in_element((By.ID, 'element-id'), 'expected-text')))

title_contains:等待页面标题包含特定文本。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.title_contains('expected-text'))

alert_is_present: 等待警报出现。

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.alert_is_present())

这些只是 Selenium WebDriver 中可用的显式等待的几个示例。您可以在 Expected_conditions 模块文档中找到更明确的等待条件:

https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

© www.soinside.com 2019 - 2024. All rights reserved.