For running our tests we will use Protractor. Protractor is an end-to-end test framework for AngularJS applications.
We will use an existing Selenium grid to run our tests.

You will need NodeJS to install Protractor. You can download it from here: http://nodejs.org/.

To install Protractor in command prompt go to your project directory and type:

npm install protractor

or you can install Protractor globally by typing then next:

npm install -g protractor

Here is a simple test:

describe('angularjs homepage', function() {
	
	beforeEach(function() {
	    // before function
	  });
	  
	afterEach(function() {
	    // after function
	  });
	
  it('test title', function() {
    browser.get('http://juliemr.github.io/protractor-demo/');
    
    var firstNumber = element(by.model('first'));
    var secondNumber = element(by.model('second'));
    var goButton = element(by.id('gobutton'));
    
    var options = element.all(By.css('select option:nth-child(2)'));
    
    firstNumber.sendKeys(15);
    secondNumber.sendKeys(3);
    
    options.click();
    
    goButton.click();
	
    expect(element(by.binding('latest')).getText()).toEqual('5');
  });
});

 

Running the test requires a config file. The config file should contain your selenium grid address and the name of the tests you want to run.

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['test.js']
};

To run the test on a specific browser you can add the capabilities option to your config file:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['test.js'],
  capabilities: {
	    browserName: 'firefox'
	  }
}

To run the tests more than once on multiple browsers you can do the next:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['test.js'],
	multiCapabilities: [{
		browserName: 'firefox',
		count:2
			
	}, {
		browserName: 'chrome',
		count:2
  }]
}

To run the test enter the next line into the command prompt in your project root directory. If you installed Protractor locally:

node node_modules\protractor\bin\protractor config\conf.js

or if you installed Protractor globally:

protractor config\conf.js

After running the test it will tell you did the test pass in the command prompt. You can see the test results on the image below

TestResult

Similar Posts from the author: