In one of our previous posts, we showcased how to run Selenium tests using a task scheduler in Windows 7. Today, we’ll show how you can run Selenium tests using crontab in Ubuntu. It’s a built-in Linux command to schedule your routine tasks such as automation.
An important point which you should note that there may not be any desktop session open while running the Selenium tests on Ubuntu. So, we will also be handling such situations in this post. We will make sure to provide you with all the script code to take care of such use cases.
You might be wondering why didn’t we write a post on using Jenkins or Bamboo to schedule these tests. It’s because both of these tools internally use the same way that crontab already does. So you should prefer using the CI tools only if you need deployment and installation as well.
Because crontab won’t help you there. For our readers, we’ll surely do a detailed blog post on how to run Selenium tests using Jenkins. Let’s now begin to understand what you need to run Selenium tests using crontab in Ubuntu.
Run Selenium Tests Using Crontab in Ubuntu 14.04
We’ll walk you through all the steps you can use to run Selenium tests on Ubuntu. We’ve tested all these steps on a Ubuntu 14.04 test bed. Though, they’ll run on other platforms too except few of the installation steps.
The scope of this post is limited to setting up Ubuntu for Selenium and running tests. So, we won’t be telling you how to create a Selenium project in Eclipse or write your first test case using TestNG in Eclipse. 🙂
But you are welcome to follow the links provided in line with the above text if you wish to learn Selenium Webdriver from scratch. We do have some advanced Selenium Webdriver tutorials on our blog so you may even refer them as well.
Let’s now run through the steps. We suggest you try these steps on your system as we go along with this post.
1. Install <xvfb> on Ubuntu
1.1- First, update the Ubuntu, and then go on installing the headless Firefox with <xvfb>.
apt-get update apt-get install xvfb
Note: For RHEL based system, you need to use the <yum> command instead of the <apt-get> command in Ubuntu.
# In RHEL, use this command. yum install Xvfb firefox
1.2- If you don’t have <firefox>, then install it as well.
apt-get install firefox
1.3- Make sure, you’ve <java> installed. Run the below command.
java -version
If <java> is not available, then download and install it.
# It'll install <jre> to run your java program. sudo apt-get install default-jre # It'll install <jdk> to build your java program. sudo apt-get install default-jdk
2. Setup a virtual screen using <xvfb>
2.1- Run the below command on the console. You need to run crontab as <sudo (root)>.
sudo crontab -e
2.2- Add the following command as your first cronjob.
@reboot sh -c 'Xvfb :99 -ac -screen 0 1024x768x8 > /tmp/xvfbmsg.log 2>&1 &'
3. Prepare your Selenium tests to run from the command line
For example, if you are using a Selenium TestNG project, then use the below command to run the tests.
#!/bin/bash # make sure the xvfb service is running. # run the following to be on safe side. Xvfb :99 -ac & export DISPLAY=:99 java -cp "path/to/testng.jar:path/to/testClass" org.testng.TestNG testng.xml
Save the above command say <seleniumtests.sh>.
4. Time to run Selenium tests using crontab
4.1- Run the below command on the console. You need to run crontab as <sudo (root)> so that your tests will run as root.
crontab -e
4.2- crontab uses the following syntax.
minute hour * * * command
4.3- Add the following entry in the crontab console.
0 22 * * * seleniumtests.sh
4.4- Alternatively you can try the following crontab option. It’s equivalent to the <0 0 * * *>.
@daily seleniumtests.sh
Now we are at the end of this blog post and hope you would now be ready to run Selenium tests using crontab on your test system. This blog post is our second submission to the series of Selenium testing tutorials that we recently started. Be ready for more such scintillating stuff. Till then, do something like this.
Get a tool, set up a lab, and start an experiment.
Best,
TechBeamers