The Selenium Library
What is the Selenium Project?
The Selenium Projects, more commonly referred to as the Selenium library, is one of the most popular tools for web browser automation and is entirely platform-independent. It works in virtually all browsers, has versions for every major operating system, and can be written in a wide variety of languages, including the people’s favourite: Python. The tools remain open source and are supported by a large community of developers with an official user group always willing to answer questions and many ways for volunteers to get involved in maintaining and moving the project forward.
Selenium Brief History
Testing is a crucial aspect of all development work and requires increasingly sophisticated tools. As software becomes more complex, developers need ways to automate testing procedures that work seamlessly with multiple programming languages and websites running across every popular internet browser.
Jason Huggins, a developer working at the time for the ThoughtWorks company in Chicago saw this need and decided to do something about it. His solution to the problem of having to repeatedly run the same verification tests on an application he was responsible for was to automate the process with what he called the ‘JavaScriptTestRunner’.
The software became so popular that he decided to release it to the public as an open source product and renamed it the ‘Selenium Core’. His solution has gone from strength to strength and is now the automation tool of choice for those needing to automate tasks or testing protocols on web applications and websites.
What can you do with Selenium?
The short answer is that you can quickly and easily automate browsers without having to learn a new scripting language, but there are many specific advantages to using Selenium that make it the weapon of choice for web developers and testers.Here are some of our favorites.
- It is famously easy to learn and use Selenium. The learning curve for a working developer is insignificant.
- It is Open Source so there are no license fees, forced upgrades, paid subscriptions, or any of the other strings that come attached with proprietary software.
- It is extremely portable. The Selenium tools work with virtually every browser and platform known to the modern developer.
- Reduced testing times for teams that implement Selenium. The lives of software testers have been radically simplified compared to the pre-Jason Huggins era.
- Selenium is both a Domain Specific Language (DSL) and a set of tools that makes it more flexible in carrying out a wide variety of tasks.
- Selenium uses fewer resources than competing software such as RFT or
- It supports parallel test execution increasing overall testing efficiency and saves time.
- It saves money. Businesses that implement Selenium into their software testing protocols can significantly cut costs.
Selenium and Python
Python and Selenium are a great match. What Python does for simplifying coding, Selenium does for automating web browsers. The two work seamlessly together and once you have installed the Selenium tools onto your machine you can begin automating processes extremely easily.
Selenium has a built-in Python module with a simple, developer-friendly API. Programmers and testers can easily automate tests to a high degree of complexity with the Selenium WebDriver. Just like their namesake, the Selenium Python module and WebDriver are easy and intuitive to use. For example, here is what importing the Selenium WebDriver and making a call to a website might look like in Python.
#import the Selenium webdriver from selenium import webdriver #create an instance of the webdriver object driver = webdriver.Brave() #make a call to a website and output it to the screen driver.get(“https://www.selenium.dev/projects/”)
As you can see, the learning curve for a Python developer is minimal. Some of the other functions available include the following:
- Navigating links
- Locating single elements
- Locating multiple elements
- Executing clicks
- Executing click and hold
- Double clicks
- Drag and drop
- Key up and key down
- Move by offset
- Move to element
- Release actions
- Exception handling
- Cookie handling
The level of support and development over the years has led to the Selenium Project being capable of supporting and automating virtually any action on a website that your users in the wild can come up with.
What is the Selenium WebDriver?
This is the granddaddy, or more correctly put, the parent of all of the classes and methods available to Selenium for the Python language. It is this class that enables you to carry out the wide variety of actions across multiple elements on a web page that Selenium is famous for.
Live users will test your web application’s or web page’s functionality to the limits so you need to be able to reproduce those conditions as accurately as you can. This means more than just making a few clicks here and there on a web page but rather interacting deeply with every function in the app or on the page.
Your code must be able to successfully locate multiple HTML elements and Selenium works excellently with Python to enable this. The WebDriver has a multitude of methods to employ within a browser that will mimic real-world use as closely as possible. This includes everything from accepting/denying cookies to navigating multiple open browser tabs and everything in between.
The WebDriver is essentially an interface with which to create a series of steps that will work regardless of the browser your application or website is running on . You can write the code in Python and the instructions will work regardless of whether the end user is running Chrome, Firefox, Safari, Brave, or any other popular browser.
You can choose to do this in Selenese, the language native to the Selenium project, but why do this when you can use the language that you and your team are already familiar with?
In earlier versions, you needed a separate Selenium server to execute commands, but the WebDriver has simplified the process by directly creating an instance of the browser to interact with. Commands can then be issued directly to this browser object. Some of the more useful WebDriver methods include:
- add_cookie
- back
- create_web_element
- delete_all_cookies
- delete_cookie
- execute_script
- full_screen_window
- get_screenshot_as_png
- maximize_window
- set_window_position
The level of interaction available to the developer or tester is extremely sophisticated. Basically, it allows to create testing scenarios quickly, easily, and with a high degree of accuracy compared to real-world conditions.
Selenium and automation testing
The Selenium Python module was created specifically to enable automation testing through browsers via Python code. It works principally with the WebDriver class seen above. But this isn’t the only functionality provided to Python developers by the Selenium Project.
In addition to the various methods touched on above, Selenium has additional functionality included in the Selenium Grid module that significantly extends Python’s ability to automate testing.
What is the Selenium Grid?
The Selenium Grid is a server that enables tests on remote machines. This is crucial when testing browser functionality or applications designed to specifically run in a distributed way across the web. What happens on one single machine may not represent what occurs when hundreds or even hundreds of thousands of instances of an application are running across different machines on the internet.
The Selenium Grid creates one server to act as a central hub for all of the other instances. Scripted tests then make calls to this hub to gain access to the various browser instances on different machines and platforms. Testing can then occur in parallel in real-time, with multiple browser versions, operating system platforms, and browser configurations.
All of this variation is managed centrally by the central server instance controlled by the Selenium Grid. Not only does this mimic real-world conditions, but it also allows the testing load to be distributed across multiple machines. AS a result, the process is more efficient and less prone to load-based software issues.
Ready to learn more?
If you would like to know more, try the exercises of Selenium with Python included in some of our practice projects.