With all the major functionalities packed into the badgeyay web application, it was time to add some automation testing to automate the review process in case of known errors and check if code contribution by contributors is not breaking anything. We decided to go with Selenium for our testing requirements.
What is Selenium?
Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language. In other words, Selenium does browser automation:, Selenium tells a browser to click some element, populate and submit a form, navigate to a page and any other form of user interaction.
Selenium supports multiple languages including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. Here, we are going to use Python (and specifically python 2.7).
First things first:
To install these package run this code on the CLI:
Don’t forget to add them in the
We also need to have Firefox installed on your machine.
Writing the Test
An automated test automates what you’d do via manual testing – but it is done by the computer. This frees up time and allows you to do other things, as well as repeat your testing. The test code is going to run a series of instructions to interact with a web browser – mimicking how an actual end user would interact with an application. The script is going to navigate the browser, click a button, enter some text input, click a radio button, select a drop down, drag and drop, etc. In short, the code tests the functionality of the web application.
A test for the web page title:
Run the test using
Clicking the element
For our next test, we click the menu button, and check if the menu becomes visible.
Uploading a CSV file:
For our next test, we upload a CSV file and see if a success message pops up.
The entire code can be found on: https://github.com/fossasia/badgeyay/tree/development/app/tests
We can also use the Phantom.js package along with Selenium for UI testing purposes without opening a web browser. We use this for badgeyay to run the tests for every commit in Travis CI which cannot open a program window.
- Selenium with Python by Baiju Muthukadan: http://selenium-python.readthedocs.io
- Getting started with UI autometed tests using (Selenium + Python) by Daniel Anggrianto: https://engineering.aweber.com/getting-started-with-ui-automated-tests-using-selenium-python/
- Selenium Webdriver Python Tutorial For Web Automation by Meenakshi Agarwal: http://www.techbeamers.com/selenium-webdriver-python-tutorial/
- How to Use Selenium with Python by Guru99: https://www.guru99.com/selenium-python.html