Skip to content Skip to navigation
Check Our New Demo Website!

Protractor basic Example Program

Let us now create a very basic test and execute using protractor. As we know protractor needs to files spec file (test file) and conf file (configuration file).

'describe' is block of code that implements the suite and 'it'is a block with combination of commands and expectations syntax is from the Jasmine framework.

browser is a global created by Protractor, which is used for browser-level commands such as navigation with browser.get(). (To make it work with Non-AngularJs application, we should use browser.driver.get(). We will look more in detail in later tutorials)

Below is the sample Test file named 'testspec.js'

describe('angularjs homepage', function() {
  it('should have a title', function() {
    browser.get('http://angularjs.org/');

    expect(browser.getTitle()).toContain('AngularJS');
  });
});

The above simple test will navigate to an AngularJS home page and checks for its page title.

Below is the sample config file named 'conf.js'

exports.config = {
	//The address of a running selenium server.
  seleniumAddress: 'http://localhost:4444/wd/hub',
 //Here we specify the name of the specs files.
  specs: ['testspec.js']
}

How to run???

Go to the command prompt, just type 'protractor conf.js', which will start executing your test in chrome browser by default. You can also change the browser using capabilities in conf.js file. We will look into more details in later tutorials.

Note: First you need to navigate to the location where you located and then try to execute. Or else just pass the complete path to protractor as protractor G:\path\conf.js

And also before executing the test, we should make sure that selenium server is running.

The complete title is "AngularJS — Superheroic JavaScript MVW Framework". As we are trying to assert using 'toContain' which will return true. It is like 'contains' method.

Protractor pass test

In the same test file if we use 'toEqual' instaed of 'toContain' test will result as failure. Please check the error in the below image

Protractor fail test

In the above screen shot, if you observe, it is displaying a message as "Expected 'AngularJS — Superheroic JavaScript MVW Framework' to equal 'AngularJs'. It is trying to find the complete title and returns fail as we have given a partial text.

Protractor Tutorials: 

Comments

Hi ,

i am new to this tool. Your tutorial saved my time and its easy to use.

Thanks for sharing .

one update from my side ::

And also before executing the test, we should make sure that selenium server is running..
using below command.webdriver-manager start.

Thanks
GS

Hi,
Can you please tell me how we can save the console log messages in the text file or how can we show the log messages(steps that we are performing through script) in report.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.