Pages

Write Test case in Protractor

Write a Test case in protractor?
  • We will start writing a test case using the Jasmine test framework.
  • Protractor requires 2 essential files to run a test case.
  1. configuration file
  2. Spec file
Configuration file:
  • We will specify configurations to run a protractor test case like SeleniumServer(seleniumAddress), test framework name, specs file paths etc.,.
  • It is a javascript file.
Spec file:
  • In the Jasmine test framework, we write test cases using describe() and it() blocks.
  • It is also a javascript file.
First test case in protractor:
  • Let's create a project and then create a folder in any directory for testing.
  • Create your first test case in protractor, for that create javascript files(say testSpec.js and conf.js files)
testSpec.js:
describe('Keep learners - Learn Protractor', function() {
    it('This is my First Test case', function() {
        browser.get('https://keeplearners.blogspot.com/2018/03/Angular-elements.html');
        expect(browser.getTitle()).toEqual('Keep Learning: Learn Protractor');
    });
});
  • Here, describe and it syntax from jasmine framework
  • browser is a global created by Protractor, which is used for browser-level commands such as navigation with browser.get("your url"), etc.,.
conf.js:
exports.config = {
    framework: 'jasmine',
    seleniumAddress: 'http://localhost:4444/wd/hub',
    specs: ['testSpec.js']
}
  • Here, we specified configurations- the framework we are using i.e.,. jasmine
  • This configuration tells protractor where your tests i.e.,. spec files and where to talk to your selenium server(seleniumAddress)
  • It will use the defaults for all other configurations.
  • Its default browser is Chrome.
Run your protractor first test case:
  1. Open a command prompt and start 
  2. selenium server using the command webdriver-manager start
  3. It will start your selenium server
  4. In the command prompt, navigate to the folder where conf.js file(s) created and execute your test case using protractor conf.js
Run protractor test
Run protractor test
  • Once the test case executed successfully, the selenium server will output a bunch of logs and test output will show the status of the test case(pass/fail).
  • We can also execute test cases in any IDE tools like Webstorm.
Protractor spec file
Protractor spec file

Protractor Run test case
Protractor Run test case
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.