This is a quick tutorial to get you started running automated tests completely in the cloud as part of your development process using Travis CI and real browsers in Sauce Labs. As a guinea pig during this blog post, I used a small, open source Python project of my own called cssify. You can find the cssify build results on Travis and even watch running jobs or videos of the browsers in action in the project's Open Sauce page. Steps involved:
Set up your tests to run on Travis CI
Configure Travis CI to start Sauce Connect
Configure your Selenium tests to run on Sauce Labs
Provide additional details to Sauce
Travis CI is a hosted continuous integration service for the open source community. It is integrated with GitHub and offers first class support for: C, C++, Clojure, Erlang, Go, Groovy, Haskell, Java, JavaScript (with Node.js), Perl, PHP, Python, Ruby and Scala. Their CI environment provides multiple runtimes (e.g. Node.js or PHP versions), data stores and so on. Because of this, hosting your project on Travis means you can effortlessly test web applications against multiple runtimes and data stores without even having all of them installed locally.
There's multiple tutorials out there to get started using Travis CI. One of my favorite things about Travis is how easy it is to configure and get started. Check out the official Travis CI docs to get started in your own programming language. For running your Selenium tests, remember to install the right dependencies, including the Selenium client bindings, like I did in my project. Also, make sure you make Travis start your webapp before the tests start. Your application needs to be serving pages to be able to test it using our browsers :)
Sauce Connect creates a magical rainbow tunnel that will allow the Sauce browsers driven by your Selenium tests to access the Web Application that your Travis build starts. Once a Sauce Connect tunnel is running, browsers in our cloud can use it to hit localhost, internal IPs or even to just proxy the public internet through the right location. I've created a small Bash script that takes care of downloading, starting and waiting for Connect to be ready. To use Sauce Connect in your build, all you need to do is curl and run this script in your .travis.yml before_script section as I did in my project:
before_script:
Once your builds are happily running and Sauce Connect is started automatically by Travis, the next step is having your Selenium scripts run in the Sauce Labs cloud and hit the Application Under Test (AUT) through the tunnel. To start running real browsers in the Sauce Cloud, make your scripts use Remote WebDriver and point the command executor to localhost:4445
(Sauce Connect will listen in that port and securely and efficiently relay to Sauce). You can find an example of this on the cssify codebase.
self.username = os.environ['SAUCE_USERNAME'] self.key = os.environ['SAUCE_ACCESS_KEY'] hub_url = "%s:%s@localhost:4445" % (self.username, self.key) self.driver = webdriver.Remote(desired_capabilities=self.caps, command_executor="http://%s/wd/hub" % hub_url)
Now for these browsers to be able to hit the AUT when opening localhost, you need to specify an additional Desired Capability, tunnel-identifier. The value this capability should be set to is stored in the environment variable TRAVIS_JOB_NUMBER. You can find an example in the cssify codebase for this as well.
self.caps['tunnel-identifier'] = os.environ['TRAVIS_JOB_NUMBER']
In addition to running your tests in real browsers using the cloud, there's additional details you can provide to speed up debugging and navigating test results. In the cssify tests you can find some examples Desired Capabilities that Sauce uses and that you can pull from Travis CI's environment variables.
self.caps['build'] = os.environ['TRAVIS_BUILD_NUMBER'] self.caps['tags'] = [os.environ['TRAVIS_PYTHON_VERSION'], 'CI']
You're set! You are now running your tests using Travis CI, they start a Sauce tunnel on their own and drive real browsers in the cloud. Time to start thinking about running these in parallel to speed up your builds now! Here are examples of what your Travis CI build should look and what you'll see from the Sauce side.