DEV Community

Cover image for Selenium
RRARI504
RRARI504

Posted on

Selenium

This blog is a slight introduction into what Selenium is. No, not the chemical element denoted with the symbol 'Se' and the atomic number 34; Selenium testing, an open-source suite of tools designed for automating web browsers for testing purposes.


Timeline

Before diving into what Selenium testing is and how it works, we need to understand how this tool even came about. An engineer, Jason Huggins, working for the global technology consultancy, Thoughtworks, had an idea to make a JavaScript tool called JavaScriptTestRunner. The reason behind the tool was to help him while he was developing an application that needed frequent testing. The goal of the tool was to avoid manual testing by automating user actions like clicking and typing.

JavaScriptTestRunner was later named Selenium Core and became a tool that was used internally at Thoughtworks, that was popular among Huggins colleagues; but it came with a couple of draw backs. One, it was not an open sourced framework 3 and two, it could not interact with different domains because of the 'same origin policy' 2.

Around 2005, Selenium Remote Control was made to solve this same origin policy issue. It made it so that test could be written in different programming languages and "run across different browsers using a server to inject JavaScript" 1. This, along with open sourcing allowed for the tool to grow in use and it eventually made it over to Google.

In 2007 Huggins joined Google and continued to stabilize Selenium RC while another developer at Thoughtworks made an even better browser automation tool which was called WebDriver. It had a higher level API and did not rely on using a server to inject Javascript. 4 The two projects later merged and became Selenium WebDriver. Since then, multiple other open source projects have started around Selenium, and Selenium Web Driver is a key component in these projects and what they do.


Components

Today, the Selenium Suite has four main components. Selenium IDE, Selenium RC, Selenium WebDriver and Selenium Grid.

  • Selenium IDE is a browser extension for Chrome and Firefox that can records user interactions in the browser like selections or clicks. These interaction can then be played back as test scripts generated in Selenese 7 and then can be exported in various programming languages like Javascript, Python, C#, etc.

  • Selenium RC or 'remote control', writes automated tests for a web application in any programming language. So it works with the browser and acts as a Javascript event handler between your code and the browser. (Comes with limitations)

  • Selenium Web Driver can directly communicate with the browser via browser-specific native methods and eliminates the limitations that Selenium RC had.

  • Selenium Grid allows for 'parallel distributed test execution across multiple machines' so it allows for cross platform testing and cross browser testing. This reduces the time needed to run and complete a test suite.

    In depth breakdown of components


How to use Selenium

To use Selenium you will first need to install it with npm in your computers terminals using this command 10.

npm install selenium-webdriver
Enter fullscreen mode Exit fullscreen mode

You will then need to download components that will enable automation on whatever major browser you are using. For Apple's Safari browser the following command can be ran in the to enable automation in the – /usr/bin/safaridriver path.

safaridriver --enable
Enter fullscreen mode Exit fullscreen mode

Now, lets see an example of writing a text script using Javascript. This test code will go to Google's home page and send its title as an output in the console 9. This will happen automatically whenever you run this code so you do not have to go through the hassle of switch tabs, typing and clicking.

var webdriver = require(‘selenium-webdriver’);

var browser_name = new webdriver.Builder();

withCapabilities(webdriver.Capabilities.safari()).build();

browser.get(‘http:/www.google.com’);

var promise = browser_name.getTitle();

promise.then(function(title) 

{

console.log(title);

});

browser.quit();
Enter fullscreen mode Exit fullscreen mode

Conclusion

Selenium WebDriver is exceptional in making automation testing easier and more efficient. Allowing the user to use languages like Javascript, Python, etc. to make test scripts becomes useful in situation where new features have to be added to an application every week. This flexibility is why Selenium is widely used for automated user interface testing of applications. Automated testing makes for more frequent test and more productivity, so it is very useful to web developers.

  1. https://katalon.com/resources-center/blog/selenium-testing-tutorial
  2. https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
  3. https://www.ibm.com/think/topics/open-source
  4. https://www.selenium.dev/history/
  5. https://en.wikipedia.org/wiki/Selenium_(software)
  6. https://www.browserstack.com/selenium
  7. https://www.tutorialspoint.com/what-is-selenese
  8. https://www.browserstack.com/selenium
  9. https://www.browserstack.com/guide/automation-using-selenium-javascript
  10. https://www.selenium.dev/selenium/docs/api/javascript/

Top comments (0)