In one of our previous posts, we’d showcased how to run Selenium tests using 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 would also be handling such situations in this post. We would make sure to provide you 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 what 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 all 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 setup Ubuntu for Selenium and run 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.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 <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.
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.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 &'
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.1- Run the below command on the console. You need to run crontab as <sudo (root)> so that your tests would run as root.
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 * * *>.
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.