I met with the opinion that time.sleep in autotest is a bad practice, tell me why and what alternative methods can be used

214 Views Asked by At

It looks like this and it doesn't work any other way.

Which other functions for waiting for an element on the page and waiting for the page to load could I use?

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")))
1

There are 1 best solutions below

0
Kumar Rishabh On

Using time.sleep in test automation is generally considered a bad practice for several reasons:

1. Unreliable timing: Using a fixed delay with time.sleep assumes that a certain action will always take the same amount of time. However, the execution speed of tests can vary depending on factors such as system load, network latency, or the performance of the application under test. This can lead to tests failing due to incorrect timing assumptions.

2. Increased test execution time: time.sleep adds a fixed delay to your test script, even if the element or condition you are waiting for becomes available earlier. This unnecessarily increases the overall execution time of your tests.

3. Poor maintainability: If the application under test undergoes changes, such as UI updates or improvements in performance, using fixed sleep delays may cause your tests to become unreliable. You would need to manually update the sleep durations, which can be time-consuming and error-prone.

To overcome these issues, it's recommended to use explicit waits in test automation. Explicit waits allow you to wait for specific conditions to be met before proceeding with the test. They are more reliable and provide better control over the timing of your tests. Selenium WebDriver provides the WebDriverWait class for implementing explicit waits.

Here's an example of using explicit waits instead of time.sleep in Python with Selenium WebDriver:

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()

In this code, we use WebDriverWait to wait for the target element to be present before proceeding with further actions. The EC.presence_of_element_located expected condition checks if the element is present in the DOM. The WebDriverWait instance will keep polling the DOM until the element is found or a timeout occurs.

Using explicit waits in this way provides more reliable and efficient synchronization between your test script and the application under test, without relying on fixed sleep durations.

Here's a list of commonly used explicit waits:

presence_of_element_located: Waits for an element to be present in the 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: Waits for an element to be visible.

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: Waits for an element to be invisible.

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: Waits for an element to be clickable (visible and enabled).

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: Waits for specific text to be present in an 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: Waits for the page title to contain specific text.

from selenium.webdriver.support import expected_conditions as EC

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

alert_is_present: Waits for an alert to be present.

from selenium.webdriver.support import expected_conditions as EC

wait.until(EC.alert_is_present())

These are just a few examples of the explicit waits available in Selenium WebDriver. You can find more explicit wait conditions in the expected_conditions module documentation:

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