Home > Automation Testing > Advance Selenium > Protractor: Script execution using custom command

Protractor: Script execution using custom command

This post was most recently updated on July 29th, 2024

This article will cover below points: 

  • Use of custom command instead of protractor along with user defined options to control the execution of script.
  • Protractor script execution for different test suites using different configuration files i.e. In regression test suite we will execute all spec files In smoke test suite we execute only selected spec files. 
  • This article contains code to store console output and error logs on separate files.

For example:

  1. custom_cmd -s : It will execute protractor script with config file written for smoke test suite execution
  2. custom_cmd -r : It will execute protractor script with config file written for regression test suite execution

Where custom_cmd is keyword we are using in place of protractor (Note: here you can use your own keyword).
while -r and -s are the options which are used to control the execution based on configuration file.

Setup And Installations :

Before starting with detail steps, here are the list of pre-requisite

  1. Install Eclipse IDE for Java (latest version)
  2. Install Enide (Studio) 2015 from eclipse market place.
  3. Change perspective from Window > perspective > Open Perspective > Other(click on other to view more) and select Node from available options
    Image 1: Open perspective

    Image 2: Select Node from perspective

Project Creation And Project File Structure

  1. To create new “Node.js” project. Go to File → New → Node.js Project
  2. Provide project name & select none/empty template to use.
  3. Create below mention folders for the project
    1. → bin : It contains index.js file
    2. → exec_config : It will contain different config.js files
    3. → TestData : It will contain test data in form of excel or json.
    4. → TestScript : It will contain test script or specs which will contain test cases.

Steps To Create Sample Project With Custom command

Step 1: Create package.json file using npm int command 

  1. → Open command prompt and go to project folder location.
  2. → Run “npm init” command and create package.json file

npm init

Step 2: Edit package.json file
→ In package json replace {“main”: “index.js”,} with {“main”: “./bin/index.js”,} (neglect curly braces).
→ add 2 more keys in package.json file.

For “bin” key, 

“bin”: { “custom_cmd”: “./bin/index.js” }, 

where custom_cmd is our custom command (we use to execute our script instead of protractor)

For “devDependencies” key,

“devDependencies”: { “commander”: “^4.0.1” }, 

Finally our package.json will be look like.

Step 3: Install “commander” module using below command.

→ npm i commander

Step 4: Install “protractor” at project location using below command.(without -g)
→ npm i protractor

Step 5: Create index.js file.

  1. Right click on bin folder and select New > JavaScript file.
  2. Give file name as “index.js” and click on finish button.

Here is a javascript code for index.js file

Step 6: Create config file.
As per index.js file, we required at least two config file in exec_config folder.
→ One for regression test suite pack
→ Second for smoke test suite pack

Here is sample code for implementation of one configuration file

Here is a screenshot for implementation of both configuration file.

Step 7: Create 2 spec files under TestScript folder which contains test cases.

Two spec files for test case execution.

  1. For regression test suite → spec01.js and spec02.js 
  2. While for smoke test suite → spec01.js

We have used sample spec files available at protractor site.

Here two spec files

spec01.js

spec02.js

Step 8: Install our project using npm install command .

npm install -g . 

Package successfully installed.

Step 9: Execute test script 

Now we are ready to use our custom command to execute protractor script.

  1. Custom command: custom_cmd
  2. Options:
    1. -r to execute regression test suite pack.
    2. -s to execute smoke test suite pack.

Go to project location and hit

  1. custom_cmd -r to execute regression test suite pack. OR
  2. custom_cmd -s to execute smoke test suite pack.

After each test suite pack execution, script will generate output and error log files.

Image: Both script executed using custom command and options

Image: After execution, output and error log generated.

This Article is TAGGED in , , , , , , . BOOKMARK THE permalink.

Avatar photo
Neeraj Vishwakarma
Senior QA Engineer Skills: Automation Testing, Manual Testing, STLC, Java, Selenium Webdriver, Protractor, Database Testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">