2. Selenium Webdriver

Ali Pala
5 min readOct 26, 2019

--

Hello all! We were talking about building a Hybrid Test Automation Framework before. You can find details in the first blog here.

What is Selenium Webdriver and Advantages?

Selenium WebDriver is a tool for doing interactive tests on web applications in different browsers and using different languages like Java, C#, Python, PHP, Javascript, etc. Allowing you to use different programming languages makes Selenium powerful to test web applications. It has support most of the browsers to execute tests over. Chrome, IE Explorer, Firefox… Even can support also the headless browser, HtmlUnit. It is an invisible browser, that is, it is GUI less. It is so fast since it is not required to spend time waiting for page elements to load.

Another benefit of using Selenium Webdriver is that it is open-source and it has huge community. If you stuck about something, you can find the right solution whatever your problem is. It is easy to implement and user-friendly to start to write automation code. Although I haven’t used to write a piece of code for the framework, it allows developing scripts so that extending the functionality of Webdriver.

These are some good features of Selenium Webdriver to be used, but not all of them. So, let’s start to discuss more about downloading and installing.

How to Download Selenium Webdriver?

Before jumping up to work with Selenium, you need to download it. The only consideration you should have is that, deciding programming language. I used Python language bindings but you can pick whatever you love. However, I will explain the Python-related steps.

In order to download, first, go to the Selenium Webdriver download link. And click the python binding link as it is shown below.

After clicking the download link, you will be forwarded to the pypi.org to get selenium package to automate web browser interaction from Python. Copy pip command to the clipboard.

Open a command prompt and paste copied command. “pip install selenium”. There will be a message first “Collecting selenium”, and then “Successfully installed selenium-x.xxx.x”

After Python and Selenium Webdriver installation finished, we can try to write our first selenium test. However, we need to download “driver.exe” which related to a browser. I will show you how to do that with Chrome driver. For this reason, we need to download Chrome driver in Selenium download page. And start to download “Google Chrome Driver” by clicking the link below. Once the download is finished, put the “chromedriver.exe” a proper location to call from your test automation code.

First Selenium Python Test Code

Here is our very first code.

Save this script into a file by giving a name as you wish and try to run with the following command in the command window.

python base.py

Let’s have a look together to make more clear for you. The selenium.webdriver module contains WebDriver implementations like Chrome, Firefox, IE Explorer, etc. The Keys class provides keys in the keyboard like RETURN.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

Next, the instance of Chrome Driver is created and goes to https://www.google.com. Then verify the title by using an Assertion in __init__ the method. You must be seeing the “self” keyword as well. Let’s deep dive into what do they mean and why they are so important for Python.

def __init__(self):
print("Initializing Chrome driver.")
self.driver = webdriver.Chrome()
self.driver.get("https://www.google.com")
assert "Google" in self.driver.title

__init__ is a reserved method in python classes. __init__ is what is called as a constructor in other OOP languages. This method called when an object is created from the class and it allows the class to initialize the attributes of a class.

“self” represents the instance(object) of the class. By using the “self” keyword we can access the attributes and methods of the class in python. If you don’t get the idea, don’t worry. There is an amazing explanation in this link.

def my_func(self):
search_box = self.driver.find_element_by_name("q")
search_box.clear()
search_box.send_keys("python is amazing")
search_box.send_keys(Keys.RETURN)
assert "python is amazing" in self.driver.title
print("Test is finished")

“search_box” refers to the input text element that can be located by the method called find_element_by_name() passing “q”. That means to find the element by name is “q” and clear the element to ensure it is empty. After that, send “python is amazing” text by send_keys() the method. Special keys can be sent using Keys class imported from “selenium.webdriver.common.keys”. After submitting the page by Keys.RETURNKeys.RETURN, we should get the result if there is any. To ensure that some results are found, we use the assertion.

self.driver.close()
self.driver.quit()

Finally, the browser tab should be closed via self.driver.close()and exit the entire browser by using self.driver.close().

These are a short summary for installing and using Selenium Webdriver with Python. Let’s continue with the next blog!

--

--

Ali Pala

Entrepreneur, Quality Coach, AI Enthusiast, Public Speaker, Trainer, Dad