DEV Community

Madhesh
Madhesh

Posted on

Introduction to selenium

What Selenium Tells Us About Automation Testing Using Python

From the very beginning of the course, the first testing automation tool introduced to us was Selenium. I expected to encounter a simple playback application but was surprised to find out that there is a library which allows controlling the real browser programmatically.
**
What is Selenium?**

Selenium is a free and open-source suite of tools designed to automate web browsers. Instead of the tester performing all the actions manually in Chrome such as going to a URL, logging in, etc., we write the script that does those actions on behalf of the tester.

The main component of Selenium is Selenium WebDriver. The tool we use daily is Selenium WebDriver, which interacts with browsers through their drivers (for example, ChromeDriver for Chrome, GeckoDriver for Firefox). In addition, there is Selenium IDE which is a plugin for browsers that allows recording the actions and there is Selenium Grid, a functionality of Selenium that runs the tests in parallel on multiple machines/browsers.

What is important to mention here is that it is a real web session that is being automated. The web pages are loaded, JavaScript is being executed, web page elements are functioning properly and the whole action performed by the script is like a real person performing the given action on the web.

Why do we use it for automation?

Manual regression testing is tedious and costly. Every time there is a new release, one must verify if login functionality still works, if the basket calculates taxes properly, and if search functionality provides results. Doing all of this manually for the fiftieth time is bound to introduce errors since human attention wavers way sooner than the list finishes.

Selenium eliminates this problem. A test suite that takes two days to execute manually completes in less than an hour and runs during the night or after every code change. It runs in Chrome, Firefox, Edge, and Safari browsers, making cross-browser testing almost cost-free. It is compatible with Java, Python, C#, JavaScript, and Ruby languages, meaning that it can be used by a team without switching programming languages. Being open-source, Selenium is free of charge, and there is a huge community who already figured out how to resolve common beginners' issues.

Its application in Python:

Selenium works best with Python. It uses compact syntax, so the code is easy to understand. driver.find_element(By.ID, "username").send_keys("admin") almost speaks for itself. For me as a learner, this translates to focusing on the actual Selenium concepts and not getting bogged down by syntax.

Ecosystem wins anyway because pytest adds fixtures, parametrization, markers and reporting that lets me test my case against ten different sets of data without writing any new code lines. pytest-html provides a report which can be shared with a reviewer. The POM design pattern lets me keep all locators in a single class, and when something has to be changed, I just have to change it once in this file. Adding API validations via requests, data-driven tests with pandas and Allure as a reporting tool makes this script evolve into a complete framework.

Selenium is not perfect because it can work only with web applications, test cases fall apart if the interface is changed and waits drive crazy people when they first encounter them. Still, it is a base of almost any automation career path, and working with Selenium using Python gives me a much better understanding of the process than any theoretical chapter ever could do.

Top comments (0)