Code for sample protractor script: In protractor there are two basic files to execute any automation script. Those are configuration file and specification file. Configuration file contains all configuration related code. And specification file contains test scripts. Configuration file is one for the project and we can have any number specification files in single project.
Basic Configuration file: Create configuration file with name “conf.js” and add below code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
exports.config = { directConnect: true, allScriptsTimeout: 300000, framework: 'jasmine', seleniumAddress: 'http://localhost:4444/wd/hub', specs: ['Spec1.js',], //We need to mention specification file names based on comma separated multiCapabilities: [ {'browserName': 'chrome', //browser name we should provide here. 'chromeOptions': { //We can provide chrome options here. prefs: { download: { 'prompt_for_download': false, 'directory_upgrade': true, 'default_directory': global.downloadloc, } } } } ], } }; |
In specs we should mention specification file names. In multiCapabilities we need to mention browser names.
Specification file:
Specification file is test script file where we can write test case scripts.
Create specification file with name “Spec1.js”. It should contain “describe” and “it” blocks as shown below.
1 2 3 4 5 6 7 8 9 10 |
describe('Click on learn link', function() { it('Launch URL and maximize the browser', function() { browser.get("https://angularjs.org/"); browser.driver.manage().window().maximize(); }); it('Click on learn link', function() { element(by.xpath("//a[text()='Learn']")).click(); }); } |
In “describe” we can mention test case description. In “it” block we can have test step description. In simple words “describe” is test case definition and “it” block is test step definition. Inside “it” block we need to have code for test steps.
Steps to Execute scripts:
- Open command prompt and make selenium server up and running using the command “webdriver-manager start”
- To execute the scripts open command prompt and navigate to project location
- Execute the command “protractor conf.js”