When to use implicit wait and explicit wait in Tests

There are a lot of debates on whether to use implicit and explicit wait in our test execution. If our application is fast and it has passed the load testing requirements then we can only add explicit wait.

For example, if there is a scenario where the page to page load for an application is two seconds and for a particular requirement load time is set to five seconds then explicit wait should be applied for that element only.

Often the client requests for a page load time threshold for an application. In that situation if we apply an implicit wait, then we might miss load test bugs in the application and the test execution gets passed without indicating the errors. Also, applying a big implicit wait time, slows down the execution.

Now there are applications which are on a slower side and there is no uniform page load time. This may be due a large number of applications running at the same server or the application not being tested with the performance requirements or benchmarks.

In this situation, there is no point applying explicit wait for each and every element. An implicit wait should be applied globally which shall be applicable to all the elements in the test execution.

Often in the industry, there is a combination of implicit and explicit wait. To conclude, there is no perfect rule that tells us where to apply implicit or explicit wait. It all depends on the project, infrastructure and so on.

Conclusion: Thus we have discussed when to use implicit and explicit wait in our tests. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248316

In the next section we shall discuss some of the functional automation scenarios on the GreenKart application.

Functional Automation Example 1

Let us test the scenario where we shall validate that the products selected in the first page are reflected in the second page. We shall be using the GreenKart application with the link: https://rahulshettyacademy.com/seleniumPractise/#/

Let us input a text ber in the search edit box. The vegetable names get populated based on the input text. The ADD TO CART buttons are clicked for each vegetable. Then finally PROCEED TO CHECKOUT button is clicked to move to the next page.

The next page gets navigated where we have our order summary along with the cost details.

The strategy to implement this scenario is to store all the vegetable names which are displayed on entering the search criteria into a list. Once we move to the next page, we shall again store the vegetable names in the order details into another list. Finally, we shall compare the two lists and check if they have the same content with the help of assertion.

Code Implementation:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
# declare lists
l = []
l2 = []
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# launch the application
driver.get("https://rahulshettyacademy.com/seleniumPractise/")
# identify element with css selector and input search criteria
driver.find_element_by_css_selector("input.search-keyword")
.send_keys("ber")
# pause execution for four seconds
time.sleep(4)
# to identify list of ADD TO CART buttons
b=driver.find_elements_by_xpath("//div[@class='product-action']/button")
# to iterate over list of buttons
for button in b:
# get vegetable name, append in list by traversing from child to parent in xpath
l.append(button.find_element_by_xpath("parent::div/parent::div/h4").text)
# to click on each button
button.click()
print(l)
# identify element with css selector and click
driver.find_element_by_css_selector("img[alt='Cart']").click()
# identify element with xpath and click
driver.find_element_by_xpath("//*[text()='PROCEED TO CHECKOUT']").click()
# explicit wait time applied with expected condition
wt = WebDriverWait(driver, 8)
wt.until(expected_conditions.presence_of_element_located(
(By.CLASS_NAME,"promoCode")))
# identify vegetable names with css selector
veggies =driver.find_elements_by_css_selector("p.product-name")
# to iterate over list of vegetable names
for veg in veggies:
# get vegetable name text and append in another list
l2.append(veg.text)
print(l2)
# validate list l and l2 with assertion
assert l == l2
# identify element with class name and input promo code
driver.find_element_by_class_name("promoCode")
.send_keys("rahulshettyacademy")
# identify element with css selector and click
driver.find_element_by_css_selector(".promoBtn").click()
# explicit wait time applied with expected condition
w = WebDriverWait(driver, 8)
w.until(expected_conditions.presence_of_element_located(
(By.CSS_SELECTOR,"span.promoInfo")))
# identify element for successful apply of promo code and print in console
print(driver.find_element_by_css_selector("span.promoInfo").text)

Output:

The output shows the two lists having the values ['Cucumber – 1 Kg', 'Raspberry – 1/4 Kg', 'Strawberry – 1/4 Kg'] and Code applied..!. The output also shows Process finished with exit code 0 statement meaning test execution completed without any failure in assertion.

Conclusion: Thus we have discussed one of the functional automation scenarios on the GreenKart application. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248317

In the next section we shall discuss one more functional automation scenario on the GreenKart application

Functional Automation Example 2

Let us test a scenario where we shall validate that Total After Discount value gets reduced once the coupon code is applied. We shall be using the GreenKart application with the link: https://rahulshettyacademy.com/seleniumPractise/#/. 

After the coupon code is entered and the Apply button is clicked, the Total After Discount value gets reduced.

The strategy to implement this scenario is to grab the value of the Total After Discount field before applying a coupon code. After the coupon code is applied, again we shall grab the value of the Total After Discount field. Then we shall verify that the later value is less than the previous value with the help of assertion.

Code Implementation:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
# declare lists
l = []
l2 = []
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# launch the application
driver.get("https://rahulshettyacademy.com/seleniumPractise/")
# identify element with css selector and input search criteria
driver.find_element_by_css_selector("input.search-keyword").send_keys("ber")
# pause execution for four seconds
time.sleep(4)
# to identify list of ADD TO CART buttons
b=driver.find_elements_by_xpath("//div[@class='product-action']/button")
# to iterate over list of buttons
for button in b:
# get vegetable name, append in list by traversing from child to parent in xpath
l.append(button.find_element_by_xpath("parent::div/parent::div/h4").text)
# to click on each button
button.click()
print(l)
# identify element with css selector and click
driver.find_element_by_css_selector("img[alt='Cart']")
.click()
# identify element with xpath and click
driver.find_element_by_xpath("//*[text()='PROCEED TO CHECKOUT']")
.click()
# explicit wait time applied with expected condition
wt = WebDriverWait(driver, 8)
wt.until(expected_conditions.presence_of_element_located(
(By.CLASS_NAME,"promoCode")))
# identify vegetable names with css selector
veggies =driver.find_elements_by_css_selector("p.product-name")
# to iterate over list of vegetable names
for veg in veggies:
# get vegetable name text and append in another list
l2.append(veg.text)
print(l2)
# validate list l and l2 with assertion
assert l == l2
# identify original amount with css selector and grab text with text method
org= driver.find_element_by_css_selector(".discountAmt").text
print("Original Amount is: " + org)
# identify element with class name and input promo code
driver.find_element_by_class_name("promoCode")
.send_keys("rahulshettyacademy")
# identify element with css selector and click
driver.find_element_by_css_selector(".promoBtn").click()
# explicit wait time applied with expected condition
w = WebDriverWait(driver, 8)
w.until(expected_conditions.presence_of_element_located(
(By.CSS_SELECTOR,"span.promoInfo")))
# identify discount amount with css selector and grab text with text method
disc= driver.find_element_by_css_selector(".discountAmt").text
print("Discounted Amount is: " + disc)
# assertion after converting string value to float and integer
assert float(disc) < int(org)
# identify element for successful apply of promo code and print in console
print(driver.find_element_by_css_selector("span.promoInfo").text)

Output:

The output shows the two lists having the values ['Cucumber – 1 Kg', 'Raspberry – 1/4 Kg', 'Strawberry – 1/4 Kg'], Original Amount is: 388, Discounted Amount is: 349.2 and Code applied ..!. The output also shows Process finished with exit code 0 statement meaning test execution completed without any errors or failures in assertions.

Conclusion: Thus we have discussed another functional automation scenario on the GreenKart application. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248320

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248323

In the next blog post, we shall see how to build logic to automate HTML Web Tables.


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}