Run your Automation script on Sauce Labs with travis CI

Posted by
Sauce labs let you allow to test on cloud, it provides you VMs and after each test run VMs are destroyed. You can view the live video of your test run, you can see where your test case is getting fail. Its really awesome what you need to do just get the subscription https://saucelabs.com/signup , don’t worry its gives you so much in free version.
It can be integrated with any poplar CI systems. Even you can access local host sites using Sauce Connect.Here I am gonna tell you how you can make your maven project with testNG using Sauce labs , how you can run your test cases on git commit.

How you can make your maven project with testNG using Sauce labs ?

As you are going to run your automation script on git commit, so keep your automation script in your local repo. As my local repo is PluginTest so I created my automation script directory in my repo

 mkdir -p ~/PluginTest/AutomationScript && cd ~/PluginTest/AutomationScript

Now create thew maven project with testNG , run the following command. Keep your groupID and package name same. Replace the bold text with your user name and access key.

mvn archetype:generate -DarchetypeRepository=http://repository-saucelabs.forge.cloudbees.com/release -DarchetypeGroupId=com.saucelabs -DarchetypeArtifactId=quickstart-webdriver-testng -DgroupId=webtests -DartifactId=testing -DarchetypeVersion=2.1.11 -Dversion=1.0-SNAPSHOT -Dpackage=webtests -Djava2career<sauce user name> -Dccdfc1a4-ef9b-4f28-a514-5f17c7866d72 <sauce access key>

 

Running this command will prompt you to enter sauce user name and access key and at the end prompt you to press y .

Hurray you are done with creating project  , you  can also refer this for detailed steps https://docs.saucelabs.com/tutorials/java/

How you can run your test cases on git commit?

Add your Git hub repo to Travis like explained in below screenshot:

  1. Open https://travis-ci.org/recent and sign in with GitHub
  2. Click on + sign
Travis CI.png

Now get the repo in Travis click on Sync now like in below screenshot:

Sync Now.png

Then enable the project by clicking OFF button as shown in below screenshot:

Switch on.png

In login mode open this https://docs.saucelabs.com/ci-integrations/travis-ci/

Set Up environment variable

Now create the .travis.yml and copy paste the below code in .travis.yml file. Replace the below environment variable with your environment variable created in above step.

 language:  
   - java  
   
 env:  
  global:  
   - secure: ozIwxJqor/eVclTeE3ZaYqzwrzOECQX1lgAjn9njB18yNZIcR/3nW57LVN+CKHwn6gJN+rDJD7sVyttTwkuL8azgNLFy0Rk8Auo+6HSxrrXL0gXbJ3u/1EgM0NQLesWlgY+MzSjrWQXZgXDocNs2IrhGuK0cZuI2fxDsFbWK+g==  
   - secure: Hc60hFbOxyMpnH+bXkfWYbWhLqGrBvIiYQjWpfUUYNWIZqZwS9RHv84Qz8iUmxwof6CnoG6n6AkwQBQeRJTLNWQxeyXCyFObbqEcf5nM0v2KnuuHX2wKgC4KOwxc8L3gvaqQUw6rE9RvM8exiecNq7B78yEQeAnjWABJF5LsO/U=  
   
 before_script:  
  - cd /home/travis/build/userName/PluginTest/AutomationScript/testing  
   
   
 script:  
  - mvn test  

My repository  structure is like below

GIT.png 

If you want to test some local host site then you will have to create the site there by some command and add this to host .
To add your site name to host and access on saucelabs you can use hosts and sauce_connect plugins and add this at the end of file in .travis.yml

 language:  
   - java  
   
 env:  
  global:  
   - secure: ozIwxJqor/eVclTeE3ZaYqzwrzOECQX1lgAjn9njB18yNZIcR/3nW57LVN+CKHwn6gJN+rDJD7sVyttTwkuL8azgNLFy0Rk8Auo+6HSxrrXL0gXbJ3u/1EgM0NQLesWlgY+MzSjrWQXZgXDocNs2IrhGuK0cZuI2fxDsFbWK+g==  
   - secure: Hc60hFbOxyMpnH+bXkfWYbWhLqGrBvIiYQjWpfUUYNWIZqZwS9RHv84Qz8iUmxwof6CnoG6n6AkwQBQeRJTLNWQxeyXCyFObbqEcf5nM0v2KnuuHX2wKgC4KOwxc8L3gvaqQUw6rE9RvM8exiecNq7B78yEQeAnjWABJF5LsO/U=  
   
 before_script:  
  - cd /home/travis/build/userName/PluginTest/AutomationScript/testing  
   
   
 script:  
  - mvn test  
   
 addons:  
  hosts:  
   - yoursite.com  
  sauce_connect:  
   username: "userName"  
   access_key:  
    secure: "accessKey"   

You can see your test cases running here https://saucelabs.com/tests , When you will click on test case , it will look like below screenshot

Test case in suace labs.png

Now whenever you will commit your code , your automation script will automatically run on Sauce Labs , you can see the screen cast on Sauce Labs.

 

Leave a Reply