Let us test a scenario where we shall validate if the sum of the values under the Total column in the order summary table is equal to the value of Total Amount field. We shall be using the GreenKart application with the below link:

https://rahulshettyacademy.com/seleniumPractise/#/

Let us now spy on the table and analyze its html code.

A table in html is represented by a table tag. The rows of the table are represented by tr tag. The td tag within each tr tag represents the columns of that row in the table. So here we have four rows and each row is having five columns.

For our scenario we have to grab the values under the Total column. The rule to identify all the rows in the table with xpath is //tr and to identify a particular row we have to use the row index number enclosed in []. So the xpath //tr[1] represents the first row. We can validate //tr xpath in the ChroPath, which shows 4 elements matching.

Next if we want to identify all the columns, the rule to construct the xpath should be //tr/td. Now to identify a particular column we have to use the column index number enclosed in []. So the xpath //tr/td[5] represents all the values under the fifth column.

Thus we have identified all the values under the Total column and we have used the concept of traversing from parent to child element [tr being the parent element and td being the child element] in xpath to achieve this. Next we have to eliminate the column header Total from identification.

Let us investigate the html code for the td tag for each of the rows in the fifth column.

It is seen that except for the first row (Total) all the other rows are having td tag with a child tag p. So we can identify the values of all the rows except the first row with the xpath as //tr/td[5]/p. We can validate //tr/td[5]/p xpath in the ChroPath, which now shows 3 elements matching.

Selenium shall identify the list of all the amounts by the method find_elements_by_xpath. The xpath value for the list of amounts shall be passed as an argument to the method. Finally we shall iterate over the list and then shall grab the amount one by one with the help of text method. Also, we have to identify and obtain the Total Amount field value with the help of text method.

Please note: As we have got the amounts in string data type, we have to convert them to integer data type and then perform mathematical operations on them.

Finally we shall verify that the Total Amount field is equal to the sum of amounts in the order table 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")
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)
# identify amounts with xpath from the Total column
amounts = driver.find_elements_by_xpath("//tr/td[5]/p")
# to iterate over list of amounts
sum = 0
for amount in amounts:
sum = sum + int(amount.text)
print("Amount sum is: " + sum)
# identify total amount with class name and grab text
totalAmount = int(driver.find_element_by_class_name("totAmt").text)
# assertion to compare the amount
assert sum == totalAmount

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, Code applied..! and 388. 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 how to build logic to automate HTML Web Tables. For more details, you can refer to the link:

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

In the next blog post, we shall see how to handle advanced user interactions, child windows and frames.


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"}