Book Excerpt - Science of Selenium - Ch-6 WebUI Automation Using Selenium
Kalilur Rahman
Director @ Novartis | Technology Transformation Leader| Author | Ex-Accenture/Cognizant/TCS | Life Long Learner | Quizzer | Mentor | Speaker | Influencer | Operations | Consulting | Quality Engineering
Dear All
Using this forum to share some excerpts to raise the interest levels of the book I wrote and published through BPB Publications. I will share some excerpts every week to get your inputs and suggestions for future work.
This week, I am focusing on the Chapter 6 WebUI Automation Using Selenium. This covers the following aspects
Table of Contents
Chapter 6 covers one of the most important features of Selenium, that is, how to automate Web UI using Selenium in Java and Python. It also covers automation at browser, page and element level locator usage using various types of locators such as XPath, CSS, Name, ID etc., and action automation.
Chapter 6 Web UI Automation with Selenium
“Good programmers write code for humans first and computers next.”
– Anonymous
In Chapter 6 we shall leverage the concepts learned so far and try to run some programs using Python to evaluate some of the core features of Selenium. While not all concepts in Selenium can be covered in a single chapter, we will focus on Python coding with reference to a few examples in this chapteras against Java coding used in Chapter5 to give a distinctive flavour of Selenium ecosystem.
In this chapter, we will cover the core aspects of WebUI automation to a certain degree, including what is a HypertextMarkup Language (HTML), usage of Cascading Style Sheets (CSS) and Document Object Model (DOM), that largely makeup how content is rendered and interacted within a web application, in addition to actions performed by the user. We shall also look at various approaches to locate various web elements, choose the selectors in the Web UI, customize various IDs and elements in a page, have a mechanism to handle events and take care of asynchronous interactions. The objective of this chapter is to address all these points. We shall also look at some nice features of Selenium such as asynchronous interactions, setting up of a proxy configuration, accessing web application through headless browsers, and handling cookies.
Python example to understand the components of Web UI
Let us take a simple example first, which would invoke a web browser using a Python program. Mentioned in Figure 6.1, Ch_6_Prog_1_Inaugurate_Browsers is a simple program, which can be used to open a browser, open a search site and run a search on “Selenium” as a search string. This program is made modular using “testBrowser” as a function to be called. We can instantiate any type of browser (chrome, firefox etc.) through invoking WebDriver interface and pass it as a handle to the calling function. Depending on the instantiation of the browser through the WebDriver, the program will execute the action in the specified browser and share the results. For example, we are using the Chrome browser. However, this can be either Firefox, Safari, Opera, Internet Explorer bowsers or an Edge browser.
Correlation between various locators using DOM, CSS in an HTML Page
Now, let us try a simple locator program by invoking the Internet Sample app available at Heroku App. Thesite https://the-internet.herokuapp.com has some brilliant examples for a web automation project to test the capabilities. For the example, we will take the simple login example provided on the site. The code mentioned in Figure 6.3, as Ch_6_Prog_2_Heroku_Katz_Login_SIDE.py, uses simple locators, such as find_element_by_name, find_element_by_id and find_element_by_xpath, as a means to navigate the web application. This program is modularized and the code is built to run against a unit test framework for Python likeunittest or pytest. A couple of functions have been added to it to check if apop-up alert is present on the page. In addition, to deal with the program, another function is also added to check the availability of a web element. Such a level of modularization and use of a POM (we willcover POM in a future chapter) are good programming practices to follow.
Let us take another example where we will use the CSS selectors and explore how to navigate the deep-DOM web pages. In the example, we have taken the-internet. herokuapp as a sample.We will navigate the application using the CSS selectors. Mentioned in Figure 6.3, the Ch_6_Prog_3_HerokuApp_Navigation.py uses the features that we discussed in Chapter 5 and utilizes find_element_by_css_selector and find_element_by_link_text functions.
Python event handling for Selenium
Now, let us take an example of how to handle events in Python for a Selenium automation program. We had an example in Java while we were discussing this topic in Chapter 5. Let us see how it wouldlook like in a Python program. For example, we shall be performing a search in the Bing search engine.The program mentioned in Figure 6.4 as Ch_6_Prog_5_Python_Event_Listener.py gives a modular view of how the CustomPythonListener class can be implemented. To implement a custom listener, we would extend the Abstract Event Listener and Event Firing WebDriver classes and execute the interface functions with our custom implementations. In the example, we have used a simple print statement to check the log of events and function calls based on the action performed by themain web driver.
This program would produce the following output:
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....
WebDriver.Proxy
Most of the corporate applications are behind a firewall or a demilitarized zone, and to communicate with applications or servers in a different zone or on the Internet, a proxy server may be required to protect the infrastructure. Hence,a use of proxy to make applications talk to each other becomes important. WebDriver.Proxy class gives a way to the programmers to establish the necessary connections to perform the actions.
Let us see the program to set a Webdriver proxy for a Chrome browser.
The first method is to use DesiredCapabilities option to set the proxy setting.
from selenium import webdriver PROXY_SERVER_IP = "YOUR IP ADDRESS OF the PROXY" //https://AA.BB.CC.DD:XXXXX
where XXXXX is port and AA.BB.CC.DD is the Proxy server IP webdriver.DesiredCapabilities.CHROME['proxy']={ "httpProxy": PROXY_SERVER_IP, "ftpProxy": PROXY_SERVER_IP, "sslProxy": PROXY_SERVER_IP, "noProxy":None, "proxyType":"MANUAL", "autodetect":False } driver = webdriver.Chrome() // or webdriver.Firefox() driver.get('https://www.whatsmyip.net/') // Use a website to check your public IP through the proxy
A second method is to use the Proxy() class and add the same to the DesiredCapabilities of the browser. Thus, the program above would be modified as:
from selenium.a.common.proxy import Proxy, ProxyType # Configure Proxy Option Proxy_class = Proxy() Proxy_class.proxy_type = ProxyType.MANUAL # Proxy IP & Port Proxy_class.http_proxy = “AA.BB.CC.DD:XXXXX” Proxy_class.socks_proxy = “AA.BB.CC.DD:XXXXX” Proxy_class.ssl_proxy = “AA.BB.CC.DD:XXXXX” # Configure capabilities desiredcapabilities = webdriver.DesiredCapabilities.CHROME Proxy_class.add_to_capabilities(desiredcapabilities) driver = webdriver.Chrome() // or webdriver.Firefox() driver.get('https://www.whatsmyip.net/') // Use a website to check your public IP through the proxy
Both the code snippets perform more or less the same work.
Conclusion
In this chapter, we explored the Web UI automation in detail and used Python for programming examples to explain the WebUI automation concept., So far, we have been able to explore how to perform Selenium automation, how to leverage various features available in Selenium, how to set up a Selenium Grid, how to leverage WebDriver to perform various automation scripts. We also looked at some of the advanced concepts of Selenium. In the upcoming chapters, to expand our horizon of understanding, we’ll check out moreadvanced concepts such as how to create a Page Object Model, how to modularize the scripts better, how to perform smooth exception handling etc.
Questions
- How do you set up a proxy in Selenium using Python for a Firefox browser?
- How do you set up a FirefoxProfile in Selenium? What are the benefits of it?
- What are the benefits of headless testing in Selenium? How will you setup headless testing using Chrome()?
- What is the use of DesiredCapabilities class? How is it useful?
- What are the functions available in TouchActions class? Where will you use TouchActions class?
- How does ActionChains class work in Selenium? How can ActionChains class be used? When will you use it?
- How will you go about handling cookies in Selenium? What is the role of function add and delete cookies in Selenium?
- How do you manage event handling in Python for a Selenium program? What are the different event handling functions available?
- What are the asynchronous operations handling mechanisms in Python for usage in a Selenium script?
- What is Async-Await functionality? How will you use it?
- How will you set screen sizes in Selenium using Python?
Hope you enjoyed the excerpts from Chapter-6. What are your comments?
Book Availability
The book is available at the following sites
- Australia ==> Amazon ==> https://www.amazon.com.au/dp/B082KFY17B
- Austria ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- Brasil ==> Amazon ==> https://www.amazon.com.br/dp/B082KFY17B
- Canada ==> Amazon ==> https://www.amazon.ca/dp/B082KFY17B
- Czech Republic ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- France ==> Amazon ==> https://www.amazon.fr/dp/B082KFY17B
- Germany ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- India ==> Amazon ==> https://www.amazon.in/dp/B082KFY17B
- India ==> Amazon ==> https://www.amazon.in/dp/B082KFY17B/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1
- India ==> Amazon ==> https://www.amazon.in/Science-Selenium-Automation-Framework-English-ebook/dp/B082KFY17B
- Italy ==> Amazon ==> https://www.amazon.fr/dp/B082KFY17B
- Japan ==> Amazon ==> https://www.amazon.co.jp/dp/B082KFY17B
- Japan ==> Amazon ==> https://www.amazon.co.jp/Science-Selenium-Automation-Framework-English-ebook/dp/B082KFY17B
- Mexico ==> Amazon ==> https://www.amazon.com.mx/dp/B082KFY17B
- Spain ==> Amazon ==> https://www.amazon.es/dp/B082KFY17B
- Switzerland ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- UK ==> Amazon ==> https://www.amazon.co.uk/dp/B082KFY17B
- USA ==> Amazon ==> https://www.amazon.com/dp/B082KFY17B
- USA ==> Amazon ==> https://www.amazon.com/Science-Selenium-Automation-Framework-English-ebook/dp/B082KFY17B
- USA ==> Barnes & Noble ==> https://www.barnesandnoble.com/w/science-of-selenium-kalilur-rahman/1135502525?ean=2940163963881
- Brasil ==> Livrariacultura ==> https://www3.livrariacultura.com.br/science-of-selenium-2012821784/p
- Canada ==> Indigo ==> https://www.chapters.indigo.ca/en-ca/books/science-of-selenium/9789389423242-item.html
- Canada ==> Indigo ==> https://www.chapters.indigo.ca/fr-ca/livres/science-of-selenium/9789389423242-article.html
- Germany ==> WetBild ==> https://www.weltbild.de/artikel/ebook/science-of-selenium_26964037-1
- Germany ==> WetBild ==> https://www.weltbild.de/artikel/ebook/science-of-selenium_26964037-1
- Global ==> Google Play ==> https://play.google.com/store/books/details/Kalilur_Rahman_Science_of_Selenium?id=-ofCDwAAQBAJ
- Global ==> Review Site ==> https://www.goodreads.com/book/show/49289577-science-of-selenium
- Global ==> Scribd ==> https://www.scribd.com/book/439434766/Science-of-Selenium
- Global ==> Textbookx ==> https://www.textbookx.com/book/Science-of-Selenium-Master-Skills-Needed-to-Become-a-Top-Test-Automation-Guru-with-Easytofollow-Selenium-Examples/9789389423242/
- Holland ==> Bol ==> https://www.bol.com/nl/p/science-of-selenium/9200000126647725/
- India ==> BPB Publications ==> https://bpbonline.com/products/science-of-selenium-master-web-ui-automation-and-create-your-own-test-automation-framework
- India ==> PayTM Mall ==> https://paytmmall.com/science-of-selenium-RKC0003512361_882733-pdp
- India ==> SapnaOnline ==> https://www.sapnaonline.com/books/science-selenium-master-web-ui-kalilur-rahman-9389423244-9789389423242?position=2&searchString=science%20of%20selenium
- India ==> Snapdeal ==> https://www.snapdeal.com/product/science-of-selenium/626675025728
- Japan ==> Rakuten ==> https://books.rakuten.co.jp/rk/7a977206400b37fb90e0057fc90225a7/
- Portugal ==> FNAC ==> https://www.fnac.pt/livre-numerique/a7376163/Science-of-Selenium
- Saudi Arabia ==> Google Play ==> https://play.google.com/store/books/details/Science_of_Selenium_Master_Web_UI_Automation_and_C?id=-ofCDwAAQBAJ&hl=ar
- Saudi Arabia ==> VitalSource ==> https://www.vitalsource.com/sa/en-us/products/science-of-selenium-master-skills-needed-to-kalilur-rahman-v9789389423242
- Serbia ==> Kombib ==> https://knjige.kombib.rs/science-of-selenium-master-web-ui-automation-and-create-your-own-test-automation-framework
- Spain ==> FNAC ==> https://www.fnac.es/livre-numerique/a7147532/Science-of-Selenium
- Sweden ==> BOKUS ==> https://www.bokus.com/bok/9789389423242/science-of-selenium/
- USA ==> Tattered Cover ==> https://www.tatteredcover.com/book/9789389423242
- USA ==> Walmart ==> https://www.walmart.com/ip/Science-of-Selenium-eBook/466432367
Note:-
Title Images are created using CANVA tools. Authors of the quote referred where known. Most of the information shared is generic and available in various forms in the Internet. Respective trademarks are owned by corresponding firms. Opinions about tools highlighted are from a personal experience standpoint and in no way reflect the views of my current or past employers or clients.
#WhatInspiresMe #Automation #Selenium #KRPoints #TestAutomation #DevOps #AITesting #NewAgeTesting #MobileTestAutomation
Ghost of Unifor4268_228; ISO fuel audits, failed collective bargaining, disbanded not by choice.
4 年Per molecule basis, crystal silver is the standards.
Ghost of Unifor4268_228; ISO fuel audits, failed collective bargaining, disbanded not by choice.
4 年??
Student ved Vestre viken helseforetak
4 年Good
Brand Marketing Consultant | Always here to help you build awareness for your startups, uplift the visibility of your brand, and make an impact.
4 年Very nice!
Team building Expert | LinkedIn Top Voice | Forbes featured | I help executives manage change, foster innovation, & boost their bottom line ???? ???? Actress ?? Writer ?? ???
4 年Clearly you have put a lot of work into your book.?