Often one of the trickiest aspects of running end-to-end tests is getting a browser to run within a CI environment. These environments are almost always headless, and while both Chrome and Firefox are bringing headless browsing to the mainstream in coming releases, right now you still have to do some work with utilities like xvfb to run browsers without a display. And if you want to test on Microsoft browsers like Edge or IE, or mobile browsers, your Linux CI server just isn’t going to cut it.
Services like Browserstack provide a solution by allowing you to run your Selenium tests on browsers hosted by them. This gives you access to multiple browsers in multiple operating systems. It has been hard to justify the expense of a service like BrowserStack for individual development, but recently Microsoft and Browserstack teamed up to offer unlimited hours of testing against Edge.
Using Iridium it is dead simple to get started with end-to-end testing in Browserstack.
Before we begin, make sure you have Java 8 installed on your PC.
Then sign up for a Browserstack account. In your account settings, you will find a username and access key that you can use to for the Automate service, which is how we run remote tests.
To start running remote tests with Iridium you’ll need 3 files. The first is the feature file that defines the actual test. In honor of Microsoft giving us all free testing, I have written a short test for the Contoso University, which is a sample .NET web app supplied by Microsoft and made available online here.
This test clicks a few links and updates a student name.
Feature: End to end test of Contoso University Scenario: Create aliases Given I set the alias mappings | Alexander Carson Edit | /html/body/div/table/tbody/tr/td/a | | Last Name | LastName | Scenario: Open the app Given I open the page "https://contoso.azurewebsites.net" Then I wait "30" seconds for the page to contain the text "Contoso University" Scenario: Open the main links And I click the "About" link Then I wait "30" seconds for the page to contain the text "Student Body Statistics" And I click the "Students" link Then I wait "30" seconds for the page to contain the text "Find by name" And I click the "Courses" link Then I wait "30" seconds for the page to contain the text "Department" And I click the "Instructors" link Then I wait "30" seconds for the page to contain the text "Hire Date" And I click the "Departments" link Then I wait "30" seconds for the page to contain the text "Administrator" Scenario: Edit a student And I click the "Students" link Then I wait "30" seconds for the page to contain the text "Find by name" And I click the "Alexander Carson Edit" link And I clear the "Last Name" text box And I populate the "Last Name" text box with "John" And I click the "Save" button
Next, you will need a configuration file that defines the capabilities of the browser that we will be testing against in Browserstack. In our case, we are requesting the Edge browser running in Windows 10. This combination gives us free testing in Browserstack.
<profile> <settings> <desiredCapabilities enabled="true" group="Edge"> <capability name="platform" value="WINDOWS"/> <capability name="browserName" value="edge"/> </desiredCapabilities> </settings> </profile>
Finally, we need a Web Start file to run the test. This file is a launcher that Java can use to download Iridium and run the selected test.
Note the properties called browserStackUsername and browserStackAccessToken. You will need to set these values to those that match your BrowserStack credentials. You'll also want to update the testSource and configuration values to reflect the path of the feature and xml file.
<?xml version="1.0" encoding="UTF-8"?> <jnlp spec="1.0+" codebase="https://s3-ap-southeast-2.amazonaws.com/ag-iridium/"> <information> <title>Iridium</title> <vendor>Auto and General</vendor> </information> <resources> <j2se version="1.8+" href="http://java.sun.com/products/autodl/j2se"/> <property name="jnlp.packEnabled" value="false"/> <property name="javaws.testSource" value="/Users/me/Documents/contosouniversity.feature"/> <property name="javaws.configuration" value="/Users/me/Documents/contosouniversity.xml"/> <property name="javaws.testDestination" value="BrowserStack"/> <property name="javaws.browserStackUsername" value="matthewcasperson2"/> <property name="javaws.browserStackAccessToken" value="accesskeygoeshere"/> <property name="javaws.openReportFile" value="true"/> <property name="javaws.saveReportsInHomeDir" value="true"/> <jar href="IridiumApplicationTesting.jar" main="true" /> </resources> <application-desc name="Web Application tester" main-class="au.com.agic.apptesting.Main"> </application-desc> <update check="timeout" policy="always"/> <security> <all-permissions/> </security> </jnlp>
Assuming you have installed Java 8 correctly, double clicking on the jnlp file will launch Iridium and run the test in BrowserStack against a remote Edge browser. You can see the end result of such a test in this Youtube video.
You can download these test files here.
If you are interested in writing end-to-end tests like the one shown above, check out the Iridium documentation page, and take a look at the courses available on Udemy. Iridium itself is a free and open source project on GitHub.