In a previous post, we talked about the value of using modules created by the community to enhance the functionality of Protractor, using Jasmine Data Provider as an example. While user generated plugins are awesome, itโs important not to forget how robust Protractor is on its own. If youโre willing to read the API docs on it and do a little experimenting, youโll find that Protractor has a number of built in features that can add yet another facet to your automation stack. One of those features is configuration parameters.
Why Parameters?
Protractor configuration parameters are…well, parameters! They are key-value pairs that can be defined within your configuration file and are then injected into your spec files where you would normally have hard coded values. More importantly, their values can be defined at the command line right before running. All of this is useful for a few reasons:
Keeps your code DRY
- – If you know youโre going to be using the same values in multiple spec files (i.e login email and password), parameters are great for removing unnecessary repetition.
Adds flexibility
- – Being able to change parameter values at runtime makes it easier to run the same tests with different data (think testing users with different admin privileges) all while never touching your code!
Increases security
- – Having passwords hard coded in your spec files sucks. Parameters give you the ability to keep them out and instead provide them at run time.
If all of this sounds great to you, then stay tuned! This tutorial will show you how easy it is to start using parameters in your own automation stack. Letโs do it.
Adding Parameters
An example used above was adding parameters for email and password values. These are good examples because more often than not in end-to-end testing, youโre going to be logging in a bunch. Weโll use these values below in our tutorial. To get parameters working properly, code needs to be added in two files: the Protractor config file, and the spec file you want to use the parameter in.
To start, add this code to your conf.js
file:
module.exports = { params: { login: { email: 'default', password: 'default' } }, // * other config options * }
Here, the parameters are defined so that the spec file will know what values to inject when referenced. In the example above, โdefaultโ is the supplied value for each, but you can pass any string to it since that value can be changed at runtime. If youโd like to have the option to run your tests without parameter flags in the command line (which weโll get to later in the tutorial) you can change them to values your application actually uses.
Next, you can reference these parameters in your spec file with code that looks like this:
describe('describe some test', function() { it('describe some step', function() { $('.email').sendKeys(browser.params.login.email); $('.password').sendKeys(browser.params.login.password); }); });
Now, instead of using hard coded values, Protractor will use what is provided in the config file unless the user changes those values via the command line when running the spec file. To do this, add these flags to your protractor call:
protractor conf.js --parameters.login.email=example@gmail.com --parameters.login.password=foobar
And thatโs it! Wherever you reference those parameters in your spec files, these new values will be filled in.
Conclusion
Parameters are just one of the many Config options Protractor provides right out of the box. For a full list you can check out Protractorโs example configuration file on their GitHub. Use any cool config options in your own test suite? Let us know in the comments!
Andrew Owen
Related Posts
-
Debug Protractor Automated Tests in Webstorm
Debugging automated tests assures that the tests behave as intended and avoid false positive /…
-
Protractor Automated Tests Structure
Defining a good test project approach is not an easy task. Testing is highly dependent…