For a few years, when it got here to automating net UI testing, Selenium was the king. And it’s protected to say it nonetheless is! As a broadly identified and confirmed automation testing instrument, it’s typically a default selection for a lot of software program initiatives. However does it imply one shouldn’t discover different applied sciences and frameworks? In fact not! On this article, I’ll talk about the details behind using Protractor and clarify methods to set it up for seamless use with Cucumber and Web page Object Mannequin.
The primary level behind utilizing Protractor is that it was developed primarily for testing Angular functions. Since Angular has its personal particular person properties – strategies, options, and synchronization, Protractor addresses these to make testing such apps simpler and extra dependable. It’s price mentioning that Protractor is a wrapper over webdriver.js – the official JavaScript implementation of Selenium Webdriver. What this implies is that in exams improvement explicit Angular parts could be reached with out-of-the-box check framework strategies and it nonetheless seems to be much like what would have been coded in a typical Selenium undertaking. On prime of that, Protractor is able to synchronizing with Angular, which additionally helps with the soundness of the exams.
The assumptions for establishing the undertaking have been much like earlier endeavors with check automation initiatives – the construction must be clear (Web page Object Mannequin) and check scripts must be clear and comprehensible, even for non-technical staff members (Cucumber and Gherkin). The selection of programming language fell on JavaScript since Protractor is a node.js software and the opposite viable possibility, TypeScript, would require a bit extra coding. This time the undertaking will make the most of Visible Studio Code as IDE.
To start establishing the undertaking, first, you’ll want to put in node.js. After putting in it in your machine, you’ll be able to confirm that the set up was profitable by typing in ‘node -v’ within the terminal. Whilst you’re at it, in the identical place you can too confirm the Node Packages Supervisor, typing in ‘npm – v’. Then, kind in ‘npm set up -g protractor’ and confirm its profitable set up with ‘protractor –model’. Simply in case, you’ll be able to replace the motive force every so often by utilizing the “net driver-manager replace” command.
Our subsequent step will likely be establishing the IDE for comfy work. First, in Visible Studio Code set up the “Cucumber (Gherkin) full help” extension. As soon as that’s achieved, we now have to deal with our dependencies. In our undertaking’s package deal.json file we’ll want to incorporate chai and chai-as-promised for assertions, cucumber, and protractor – all within the dependencies part. In devDependencies, we’ll want protractor-cucumber-framework to attain the aim we’re striving for.

To have consolation and readability throughout the improvement course of, one of many options that present it’s the potential to shortly search for what code is executed behind every gherkin step. To realize that in a Protractor undertaking, we’ll have to specify Cucumber choices within the conf.js file. What is important is the trail to the steps folder.

Then, within the settings.json file, we’ll have to specify the paths to folders containing step definitions and strategies which might be executed behind them. We are able to do that within the following method:

Once we do that, we will simply navigate by the undertaking by clicking the step/definition/technique/aspect specified within the code with a CTRL or CMD button pressed. It’s a easy factor, however it may possibly dramatically increase productiveness and reduce the time spent on growing or debugging exams!
Our subsequent premise that we have to deal with is operating the exams by tags. Whereas including a tag to a characteristic file is fairly easy, the half the place these are run requires offering a path to Cucumber Function information within the conf.js file.

As you’ll be able to observe within the above piece of code, the cucumberOpts part within the conf.js file requires a variable named ‘tags’ as an empty checklist.
Whereas we’re at it, it is very important level out that the conf.js file must have a bit the place we specify the Cucumber as our testing framework:

The general construction of the automated testing undertaking created in Web page Object Mannequin is comparable throughout applied sciences. An summary for Protractor could be noticed under:

When you create all the required information and end the configuration, it’s time to write the exams themselves.
Since we’re working in BDD framework, let’s begin with a easy Function File with a easy state of affairs specializing in verifying a Registration kind (with a tag for operating it later)

As soon as that’s achieved, we will specify what occurs in every step in /steps/registration.js:

In that file, we first specify the trail to the file containing strategies which might be going to be referred to as in every of the step definitions. Then we’re calling assertion libraries and establishing timeouts.
Step definition implementation is fairly easy; the Cucumber key phrase precedes a regex and a parameter; the physique of a step calls a technique from /pages/registration.js file. Often, one step calls for only one technique however check steps may very well be extra intricate if want be. Discover that if a technique returns a Boolean worth, we’re invoking assertion on the stage of a step definition (line 23).
Within the/pages/registration.js file, we have to specify a locator dictionary for parts that we’re going to work together with. You are able to do this within the following method:

Please observe the selectors used for finding the weather; you need to use varied out-of-the-box strategies for finding parts in Protractor, which have been extensively described within the official Protractor Information (link)
The identical goes for strategies used to work together with parts:

(PS. Don’t retailer your login credentials within the check automation code… The above is only for demonstration functions)
What occurs above is that we’re implementing strategies that we’ve referred to as in /steps/registration.js file, utilizing the weather we’ve put within the locator dictionary (highlighted in mild blue) and interacting with them utilizing Protractor strategies (highlighted in purple).
Then it’s time to run the exams. In VS Code, open a brand new terminal window and hit the “net driver-manager begin” command. Webdriver needs to be up and operating now.
To run the check you’ve written and tagged accordingly, all you’ll want to do now could be you need to open one other new terminal window in VS Code and enter the command:
protractor protractor.conf.js –cucumberOpts.tags=’@smoke1′ – tagging the specified characteristic accordingly.
And there you may have it! Now you may have a prepared, arrange Protractor testing framework built-in with Cucumber, Web page Object Mannequin which you’ll run with the assistance of tags. If you wish to discover out extra about Protractor, I encourage you to go to the Protractor web site, which comprises complete documentation with code examples here.