We shall now discuss one functional automation scenario in the application with the link: https://rahulshettyacademy.com/angularpractice/shop

Selecting a Product from the list of Products with Product Name parameter

Let us obtain all the list of product names along with their prices.

Let us spy on one of the products and analyze the html code.

Each of the product names like iphone X, Samsung Note 8, Nokia Edge and Blackberry can be identified with xpath locator having value as //div[@class='card-body']. Selenium shall identify the list of products by the method find_elements_by_xpath. The xpath value for the list of products shall be passed as an argument to the method. We can validate the xpath for the list of products in the Console, which shows an array of length 4.

We shall iterate through the list of products and obtain its name and price by using parent child traversal concept of xpath and by deriving the child xpath expression from the parent element xpath. Thus the xpath of the product names shall be //div[@class='card-body']/h4/a and their prices shall be //div[@class='card-body']/h5.

Selenium shall identify the product names and their prices by the method find_element_by_xpath. The xpath value of product name and prices shall be passed as an argument to the method. We can validate the xpath for the product names and their prices in the Console, which shows an array of length 4.

Code Implementation:

from selenium import webdriver

driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")

# launch a URL in browser

driver.get("https://rahulshettyacademy.com/angularpractice/shop")

           # to identify list of products with xpath

            products =driver.find_elements_by_xpath("//div[@class='card-body']")

            # to iterate over list of products

            for product in products:

            # to get product name from products by parent child traversal in xpath

                    productName = product.find_element_by_xpath("h4/a").text

                    print(productName)

           # to get product prices from products by parent child traversal in xpath

                    productPrice = product.find_element_by_xpath("h5").text

                    print(productPrice)

Output:

The output shows iphone X, $24.99, Samsung Note 8, $24.99, Nokia Edge, $24.99, Blackberry and $24.99. Thus all the product names along with their prices got retrieved.

Conclusion: Thus we have discussed one end to end practice project with complete methods in Selenium WebDriver. For more details, you can refer to the links:

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

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

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

In the next section we shall discuss how to capture screenshots in Selenium WebDriver.

Taking screenshots using Selenium Python with assertions

In Selenium, we can capture screenshots with the help of the method get_screenshot_as_file. The screenshot of the current webpage state is captured in the form of a file. The file name along with its extension is passed as an argument to the method.

Syntax:

driver.get_screenshot_as_file("screen.png") -  a new file with name screen.png gets created within the project folder.

Code Implementation:

from selenium import webdriver

driver = webdriver.Chrome(executable_path = "C:\\chromedriver.exe")

# launch a URL in browser

driver.get("https://rahulshettyacademy.com/angularpractice/shop")

# to identify list of products with xpath

 products =driver.find_elements_by_xpath("//div[@class='card-body']")

            # to iterate over list of products

           for product in products:

 #to get product name from products by parent child traversal in xpath

productName = product.find_element_by_xpath("h4/a").text

      print(productName)

 #to get product prices from products by parent child traversal in xpath

           productPrice = product.find_element_by_xpath("h5").text

                    print(productPrice)

 # perform scroll from top to bottom with scrollTo Javascript method 

          driver.execute_script("window.scrollTo

                                            (0,document.body.scrollHeight);")

# to capture screenshot with get_screenshot_as_file method

          driver.get_screenshot_as_file("screen.png")

Output:

The output shows iphone X, $24.99, Samsung Note 8, $24.99, Nokia Edge, $24.99, Blackberry and $24.99. Thus all the product names along with their prices got retrieved.

Also, a new file named screen.png gets created in the project folder.

Conclusion: 

Thus we have discussed how to capture screenshots in Selenium WebDriver. For more details, you can refer to the link:

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

In the next post, we shall discuss how to generate logs in a log file and its importance in Selenium WebDriver.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
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"}