Uncategorized

Protractor & Standalone Selenium Server & PhantomJs & Jenikins on Multiple Systems

If you want to run UI tests on your (Angular) website, use Protractor is straight way.

There exists dozens of tutorials over the internet how to setup it (e.g.  official web). The problem is that most of them describes solution when you install Protractor globally and run Selenium server for multiple tests runs and you don’t stop it. But there is another possibility how to run it – you can configure Protractor to:

  1. Start Selenium server
  2. Run tests
  3. Stop the server

After long searching I din’t find any working tutorial and had to setup it myself by test/fault. So there is final working solution which works on multiple systems (Win 8.1, Ubuntu 14.04, MacOS 10.10).

1. Add required libraries into yout app confgiguration (for npm) – set actual or appropriate package version for you

...
"devDependencies": {
...
"protractor": "~2.0.0", //protractor
"jasmine-reporters": "~1.0.2", //reports for JUnit format
"phantomjs": "~1.9.16" //PhantomJS browser
 },
...

2. Define configuration for Protractor

It is the most important part, so it will be described in depth.

exports.config = {
	// if using seleniumServerJar, do not specify seleniumAddress !!!
	seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
        //port of the server
	seleniumPort: 4444,
	seleniumArgs: ['-browserTimeout=60'],
	//seleniumAddress: 'http://localhost:4444/wd/hub',
	framework: 'jasmine',
	troubleshoot: false, //true if you want to see actual web-driver configuration
	capabilities: {
		'browserName': 'phantomjs',
		//Can be used to specify the phantomjs binary path.
		//This can generally be ommitted if you installed phantomjs globally.
		'phantomjs.binary.path': require('phantomjs').path,
		'phantomjs.cli.args': ['--ignore-ssl-errors=true', '--web-security=false']
	},

	specs: ['test/e2e/**/*.js'], //path to the test specs
	allScriptsTimeout: 60000,
	getPageTimeout: 60000,

	// Options to be passed to Jasmine-node.
	onPrepare: function () {
                //configuration of the JUnit reporting. This is configuration for jasmine-reporters 1.*
                //bacause Protractor uses Jasmine 1.*
		require('jasmine-reporters');
		jasmine.getEnv().addReporter(
			new jasmine.JUnitXmlReporter('protractor-results', true, true)
		);
	},
        //Jasmine configuration
	jasmineNodeOpts: {
		showColors: true,
		defaultTimeoutInterval: 60000,
		isVerbose: false,
		includeStackTrace: false
	}
};

The most important lines:

  1. seleniumServerJar – here you specify path to the standalone server jar file
  2. browserName – name of the browser, in our case PhantomJs
  3. phantomjs.binary.path – path to the PhantomJs exec file
  4. phantomjs.cli.args – PhantomJs parameters. This is very important because there exist multiple confuguration possibilities ovet the internet (all paramters in one string, array of paramters, etc.). In our case –ignore-ssl-errors=true, –web-security=false  force PhantomJs to accept https sites (problem in our case). If this parameters are not set, test run waits for the server and fails because of timeout.

3. Create first test spec

describe('E2E: Open website and login', function () {


   beforeEach(function () {
      browser.driver.manage().window().setSize(1124, 850); // needed for PhantomJs
   });


   var address = 'www.example.org';


   it('should be redirected to login', function () {
      // Arrange
      browser.get(address);
      var expected = address + 'login';

      // Act
      var result = browser.getCurrentUrl();

      // Assert
      expect(result).toEqual(expected);
   });
});

4. Setup & Run it

  1. Update webdriver (to download binaries of browsers): node ./node_modules/protractor/bin/webdriver-manager update
  2. Run test: node ./node_modules/protractor/bin/protractor protractor.conf.js

Thats all. Now you can run your tests with standalone server which will start before your tests and will be killed after the tests end.

5. Jenkins Build Configuration (Execute shell)


# Install packages
npm install
# Setup webdriver-manager (update)
node ./node_modules/protractor/bin/webdriver-manager update
# Running Protractor tests
node ./node_modules/protractor/bin/protractor protractor.conf.js

Advertisement