I am setting up some browser testing using SauceLabs. I've been able to get tests running locally and via Sauce, so I am now trying to integrate it with my Jenkins install to trigger builds and browser tests automatically.

Most of this is all working, but I have one small issue. So that I can run my tests locally and via Sauce, I want to set the $browsers static property during the phpUnit setup() function, rather than hardcoding it. This doesn't seem possible.

3 Answers
3

After spending quite a bit of time digging through the source, I found a solution to the "multiple browsers" scenario. chapmatic's assertions regarding multiple browsers and the given answer were indeed correct: it doesn't work for parallel testing and runs the same browser several times if you have multiple browsers defined in your $browsers array. The solution still uses environment variables, but you must still use your $browsers array.

So, first make sure you define your $browsers array in your abstract test class. Then, let's say you define the env var BROWSER and assign to it the browser you want to test. You can set up the following static function in your abstract test class that extends Sauce\Sausage\WebDriverTestCase:

Now that browserSetup() is defined, you have to make sure it is called before the test suite is set up so that the tests are are set to run only on the browser you specified in your BROWSER environment variable. Let's look at PHPUnit_Extensions_Selenium2TestCase, which is expended by Sauce\Sausage\WebDriverTestCase; PHPUnit_Extensions_Selenium2TestCase defines the following method:

This method gets called to set up the test suite with all the browsers you specified in your $browsers array, so you need to override this method in your abstract test class, making sure to call browserSetup() before fromTestCaseClass() is called:

Now, if you define the environment variable BROWSER with the browser you wish to test, you can kick off your test suite and your $browsers array will be properly overridden with the settings you specified for the single browser defined in your BROWSER environment variable. Make sure jenkins is properly setting this environment variable in the Build->Execute shell section, and you are good to go.

Ok, so I worked this out. Should anyone else have the same issue, here's how I resolved it.

In the ANT script that Jenkins runs, which runs PHPUnit in turn, I included a config.xml file. This sets a config (environment) variable called sauce to true

<phpunit>
<php>
<env name="sauce" value="true" />
</php>
</phpunit>

Now the trick is to not actually use the $browsers static array, but instead to use the setupSpecificBrowser method. So , now in my setUp() function of my tests, I just switch on the sauce env variable and if it exists then I know we are running from Jenkins and so I use the supplied variables from it.

Did you manage to add more than one browser?
–
line-oApr 9 '13 at 15:06

Automatic naming of tests seems to break this way. All tests were named "unnamed job"
–
line-oApr 9 '13 at 15:17

Using my method you'd have to setup a seperate Jenkins task per browser you wanted to test - as these are being passed via the SauceOnDemand plugin in Ant. That said, the UI does allow you to multi-select browsers. I'm not sure how you would access these values if you did that (apols I don't have time to test right now)
–
Andre LackmannApr 10 '13 at 12:18

Just to note as well, that setting the browsers after the fact stops parallel testing. Moreover if you have three different browsers to start with (in $browsers static array) and set the browser to chrome then you will have three machines running chrome now.