Blog 0.6: Selenium WebDriver for Automation Tests
Selenium
Selenium is one of the most renowned open-source test automation frameworks. Selenium allows to test automation of web-apps or websites across different browsers and operating systems.
In recent years, Selenium has also been one of the important WEB-SCRAPPING tool for data scientists. Selenium works similar to Beautiful Soup Framework but, In my understanding Selenium is more powerful tool than Beautiful Soup. Selenium allows the user to INPUT values and lookup the information without any API Keys.
Some of the benifits of using Selenium are as follows:
- Faster Cross Browser Testing with Selenium
- Selenium Offers Supports Different Programming Languages
- Selenium supports Multiple OS
- Selenium can be Integrated with CI/CD Pipelines
In this post, I will talk about the steps and required tools in order to run Selenium Webdriver. The following code will run the Selenium Web-Driver locally in my jupyter notebook.
Importing the Required Libraries for Selenium:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# html contents
from IPython.display import display, clear_output
from IPython.core.display import HTML
# FONT style
import fontstyle
Here, in this example, I will show how to input KEYS in the WIKIPEDIA and get the information from this webpage. This WebDriver will open up Chrome browser and looks up the URL which is ‘Wikipedia’.
Automation STEPS:
- opens up wikipedia browser
- Loccates the SEARCH box
- Pass Strings on the SEARCH box and clicks the search button
- Selects the First ITEM from the searched section and traverse through the link
- Gets the paragraph from the BIO and IMAGE
browser= webdriver.Chrome()
browser.get('https://en.wikipedia.org/wiki/Main_Page')
# Browser waiting time before retrieving the keys
browser.implicitly_wait(5)
# locate the placeholder, here looking for Search box
srch_box= browser.find_element_by_xpath('//*[@id="searchInput"]')
# searched strings
srch_name= "tupac"
srch_box.send_keys(srch_name)
# Search Box on WIKIPEDIA homepagename of the ARTIST from searhced Section
login= browser.find_element_by_xpath('//*[@id="searchButton"]')
login.click()
# CLICKS the first Content on Searched Page
name_artist= browser.find_element_by_xpath('//*[@id="firstHeading"]')
# GET bio from wikipedia
artist_info= browser.find_element_by_xpath('//*[@id="mw-content-text"]/div[1]/p[3]')
# GET image from wikipedia
image_artist= browser.find_element_by_xpath('//*[@id="mw-content-text"]/div[1]/table[1]/tbody/tr[2]/td/a/img')
img_src= image_artist.get_attribute('src')
print('Name: ',fontstyle.apply(name_artist.text, 'bold'))
print('Bio: \n', fontstyle.apply(artist_info.text, 'blue/blink'))
html_pic= '<img src=\"'+img_src+'\" width=\"200\" height=\"150\">'
# Displays the IMAGE
display(HTML(html_pic))
output:
Name: Tupac Shakur
Bio:
Tupac Amaru Shakur (/ˈtuːpɑːk ʃəˈkʊər/ TOO-pahk shə-KOOR; born Lesane Parish Crooks, June 16, 1971 – September 13, 1996),
better known by his stage name 2Pac and by his alias Makaveli, was an American rapper and actor. He is considered by many
to be one of the most influential rappers of all time. Much of Shakur's work has been noted for addressing contemporary
social issues that plagued inner cities, and he is considered a symbol of activism against inequality.
output (Image from wiki):
Selenium WebDriver
Selenium WebDriver is the core component of the Selenium Framework as it enables the interfacing of the Test code with the elements of the web page under test. It communicates directly with the WEB browser and has a browser-specific applicatoin.
The example above runs Selenium WebDriver to run locally. We can also run Selenium WebDriver Remotely. However, we need to host a webserver that allows to run the Selenium WebDriver remotley by assigned ip address and port number.
Selenium Grid allows to use WebDriver remotely from anywhere. I will talk more about Selenium Grid on my next Blog.