01. Intro
In the Selenium WebDriver, there are two ways to wait.
One is implicit wait (waiting for the entire webpage to come over) and extreme wait (waiting for a portion of the webpage to appear).
02. Time.sleep()
Before learning two ways, there is a command to wait for physical time.
That is time.Sleep().
import time
time.sleep(1) # wait 1 sec
time.sleep(3) # wait 3 sec
time.sleep(5) # wait 5 sec
03. Implicit wait
Once set, the WebDriver applies this implicit wait for all the steps that find the elements on the page.
In the example, the meaning of the numbers means that you will wait for the webpage to load for 10 seconds. This means that you will wait up to 10 seconds for the page to come over, not unconditionally 10 seconds after requesting. When it reaches the timeout, it will throw a NoSuchElementException.
def setUp(self):
baseUrl = "http://www.google.com"
s = Service("E:\selenium\drivers\chromedriver.exe")
self.driver = webdriver.Chrome(service=s)
self.driver.implicitly_wait(30)
self.driver.maximize_window()
self.driver.get(baseUrl)
04. Explicit wait
The explicit wait is another wait mechanism available in WebDriver to synchronize tests.
An explicit wait can only be implemented in specific cases where script synchronization is needed. WebDriver provides the WebDriverWait and expected_conditions classes to implement an explicit wait.
driver.back()
self.assertTrue(WebDriverWait(self.driver, 10).until(expected_conditions.title_is('selenium webdriver - Google 검색')))
driver.forward()
self.assertTrue(WebDriverWait(self.driver, 10).until(expected_conditions.title_is('Index')))
In this example, the explicit wait is used to wait until the title is expected, using the title_is condition.
The script will wait for a maximum of 10 seconds looking for the element to be visible.
05. Example
The visibility_of_element_located condition is used to wait until the something user wants is visible in the DOM.
The title_contains condition is that checks to make sure that the substring matches with the title of the page.
The alert_is_present condition is used to check if the alert is displayed to the user and returned back to the script for the upcoming actions. The script will wait for 10 seconds checking for the presence of the alert, otherwise, it will raise an exception.
☞ expected_conditions.title_contains
☞ expected_conditions.visibility_of_element_located
☞ expected_conditions.element_to_be_clickable
def test_a_create_new_customer(self):
# click on Log In link to open login page
self.driver.find_element(By.LINK_TEXT, 'ACCOUNT').click()
# wait for My Account link in Menu
my_account = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, 'My Account')))
my_account.click()
# get the create button
create_account_button = WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, 'CREATE AN ACCOUNT')))
create_account_button.click()
WebDriverWait(self.driver, 10).until(expected_conditions.title_contains("Create New Customer"))
☞ expected_conditions.visibility_of_element_located
☞ expected_conditions.alert_is_present
def test_b_compare_product(self):
# get the search field and submit the keyword
search_field = self.driver.find_element(By.NAME, 'q')
search_field.clear()
search_field.send_keys('phone')
search_field.submit()
# click the compare link
self.driver.find_element(By.LINK_TEXT, 'Add to Compare').click()
# wait for Clear All link to be visible
clear_all_link = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, 'Clear All')))
# click on Clear All link
clear_all_link.click()
# wait for the alert to present
alert = WebDriverWait(self.driver, 10).until(expected_conditions.alert_is_present())
# get the text from alert
alert_text = alert.text
self.assertEqual('Are you sure you would like to remove all products from your comparison?', alert_text)
alert.accept()
06. Result
'Testing Automation > 01. Selenium' 카테고리의 다른 글
08. Selenium | External data sources for DDT (0) | 2022.03.09 |
---|---|
07. Selenium | Data-driven testing (0) | 2022.03.09 |
05. Selenium | Alert (0) | 2022.03.04 |
04. Selenium | Browser navigation (0) | 2022.03.04 |
03. Selenium | Browser control (0) | 2022.01.14 |