Protractor Interview Questions – Part-1

In this series of interview questions, I’ll be adding the interview questions that I’ve faced and have seen been asked in the Protractor interviews – a lot of them are specific to Protractor and some of them require knowledge of JavaScript as well.

  • What is Protractor?

Ans : Protractor is an end-to-end test framework, designed for Angular and Angular JS applications. Protractor is a node.js port of the webdriver.io , which is the JavaScript implementation of Selenium framework.

  • Why Protractor and not Selenium with any other language like Java.

Ans : It totally depends on the choice of the team and the ease of the implementation for the QA team. There is a no hard and fast rule that you should use Protractor when the UI is built with Angular or Angular JS.

The real benefit for using Protractor narrow down to the two major points that I think should be taken into consideration while deciding whether or not to use Protractor

  1. Protractor handles the $http or $timeout synchronisation issues arising out of Angular/Angular JS very well.
  2. Since Angular/Angular JS code is written in JavaScript/TypeScript, and Protractor supports both these languages, so there is a sync between the devs and QA when using a single operating language for FE as well as the test framework.
  • What are the locators in Protractor

Ans : Apart from the basic locator mechanism that Selenium implementation gives , Protractor has some Angular JS specific locators

  1. By Binding

  2. By Model

  3. By ButtonText

  4. By Exact Binding

  5. By Partial Button Text

  6. By Repeater

  7. By Exact Repeater

  8. By CSS Containing Text

  9. By Options

  10. By deepCss

  11. Custom Locators

  • What is ElementFinder and ElementArrayFinder

Ans : The Protractor documentation mentions the distinction in most simple way –

ElementFinder

The ElementFinder simply represents a single element of an ElementArrayFinder (and is more like a convenience object). As a result, anything that can be done with an ElementFinder, can also be done using an ElementArrayFinder.

The ElementFinder can be treated as a WebElement for most purposes, in particular, you may perform actions (i.e. click, getText) on them as you would a WebElement. Once an action is performed on an ElementFinder, the latest result from the chain can be accessed using the then method. Unlike a WebElement, an ElementFinder will wait for angular to settle before performing finds or actions.

ElementArrayFinder

ElementArrayFinder is used for operations on an array of elements (as opposed to a single element).

The ElementArrayFinder is used to set up a chain of conditions that identify an array of elements. In particular, you can call all(locator) and filter(filterFn) to return a new ElementArrayFinder modified by the conditions, and you can call get(index) to return a single ElementFinder at position ‘index’.

Similar to jquery, ElementArrayFinder will search all branches of the DOM to find the elements that satisfy the conditions (i.e. all, filter, get). However, an ElementArrayFinder will not actually retrieve the elements until an action is called, which means it can be set up in helper files (i.e. page objects) before the page is available, and reused as the page changes.

SO, basically, if you’ve ever worked with Selenium in the past, you’d recognise that it is nothing but the difference between FindElement and FindElements.

  • Can I use Protractor for non Angular JS Apps.

Ans : Absolutely Yes. Protractor can be used for testing non Angular JS or non Angular apps.

  • What are Promises.

Ans : This is not a Protractor specific question actually. This question related to the concepts of JavaScript in general. You can take examples to clearly explain the concept of what Promises are. Here are a few helpful links that will help you understand the concept of Promises – link1, link2, link3

  • What are Protractor, Jasmine, Mocha. How can we use them.

Ans : Protractor , as described above, is the node.js implementation of webdriver.io, which is in turn the .js implementation of Selenium. So in short , it is in fact, the Selenium implementation in .js optimised for Angular or Angular JS applications. It is the overall framework that provides with the baseline methods and functions to interact with the HTML/DOM elements on a web page.

Jasmine – It is a test framework , like JUnit (Java) and UnitTest (Python). It comes bundled with a lot of assertions and it is the default framework when working with Protractor. Jasmine supports BDD- styled tests using the describe- it syntax.

Mocha – Mocha is a test runner. Many people confuse it with Jasmine as a test framework. It is not a framework. Although it gives you a lot of features like a Test frameworks like hooks, the describe-it syntax, but it can not add assertions and other key unit testing components. You will always need it to pair with something like chai or chai-as-promised.

  • How to assert something in Protractor

Ans : It depends on the assertion framework you’re using. In general , most of the e2e implementation done is based out of the default Jasmine 2.0 , which provides the assertion in this format

expect(something).toEqual(someotherthing).

In case you’re using Mocha with a Chai as the assertion engine, you can use either of the three assertion types provided by Chai as

expect(something).to.equal(someotherthing)

something.should.equal(someotherthing)

assert.equal(something,someotherthing)

See the different types of assertions that Chai provides here.

  • Explain how you can set up Protractor in your project. 

Ans : It is very simple to set up Protractor in your project. Protractor is a node.js program, so it can be easily installed via any package manager that supports node.js installation – both npm or yarn are the go-to package managers in most scenarios.

Your system set up should have node.js runtime installed. Since npm comes by default with node.js, so you can use it or you can download and use yarn.

In case of npm, installing Protractor is just a matter of running this simple command – for node v.8.0+

npm i -g protractor or

npm install -g protractor

This command installs both Protractor and Webdriver-Manager. Once Protractor is installed, you can check the version using

protractor -version

Now, once Protractor is installed, you’ll not be ready for running the tests yet. You need the ChromeDriver or the GeckoDriver binaries for establishing connection with the browser.

To achieve this, you’ll need to run

webdriver-manager update

This will install the webdriver-server along with the browser dependencies.

Once this is done, you’ll have Protractor set-up in your project. Now the next step is to write the test cases using the spec.js file and set up the Protractor configuration using the conf.js file.

  • Can you walk me through a simple set up of Protractor

Ans : This is similar or another way of asking the same question answered above. Just walk through how to install Protractor in the project.

  • Why do we need to use webdriver-manager update command?

Ans : The webdriver-manager update command downloads the necessary and latest binaries for the Protractor tool to talk with the browsers under target using Selenium server. That’s why we need this command.

Adding a sample screenshot of how the webdriver-manager update command in a macOS terminal looks like

Screen Shot 2018-07-22 at 9.37.31 PM.png

  • I do not want to start the Selenium server every time. How can I overcome this.

Ans : In order to not have to do this, you need to set the directConnect flag to true in your conf.js file.

  • What does ignoreSynchronisation do.

Ans : In Protractor, while running tests, this is a common problem, when the Protractor execution starts, but the Angular code is still trying to synchronise the $http or $timeout requests. Setting the browser.ignoreSynchronization to true means that we’re telling Protractor to not wait for the Angular promises to be resolved.  You can read about this in detail here on StackOverflow.

  • What are the various timeouts in Protractor

Ans : Since Protractor tests are asynchronous, and it relies on a lot of components, there are various reasons for which timeouts can occur in Protractor tests.

This may include –

  1. Timeout due to waiting for page to Load.
  2. Timeout due to fail to wait for Angular.
  3. Timeout due to waiting for Angular on page Load.
  4. Timeout due to no action performed in the script.
  5. Timeout due to Jasmine.
  6. Timeout from SauceLabs (if using with SauceLabs).

All of them have been described along with the failure message and remedy for overcoming these timeouts in the Protractor’s github docs. Please read through all of these links in order to understand the timeouts. This will also help you to understand and answer questions like – Why do so and so timeout occurred in Protractor scripts?.

  • Can I see console statements from Chrome console using Protractor

Ans : In order to capture the console statements from your Chrome console, you can do
browser.manage().logs()
.get('browser').then(function(browserLog) {
console.log('log: ' +
require('util').inspect(browserLog));
});

  • I am using the getText() method but it is not returning the result. Why?

Ans : This is faced by a lot of beginners and a lot of people who are moving from languages like Java, Python over to Protractor.

In Java, or Python, if you simply write the getText() method, it returns the corresponding text from the element.

In Protractor, however, this is a bit different, and so many times people will see that the result of the getText() method is something like [objectObject].

This is because the getText() method returns a Promise, which you need to resolve in order to get the text. So what you need to do is

element.getText().then(function(text){
console.log(text);
});

or using the arrow operator syntax, which I prefer,

element.getText().then((text)=>{
console.log(text);
});

Read more about this here , and if you want to know more about Promises read here.

  • How to use Cucumber with Protractor?

Ans : Earlier Cucumber used to come bundled with Protractor. However, as of Protractor versio 3.0, this has been excluded. So you need to install and configure your conf.js file to include the Cucumber configuration.

 

If you’ve installed Protractor globally, using the -g flag , then you’d need to do the same for protractor-cucumber-framework, so, first install the dependencies

npm install -g protractor-cucumber-framework

 

Now, in the conf.js file, you need to change the framework flag to custom and then set the path of the framework to resolve it to the installed protractor-cucumber-framework.

So, your conf.js file should have this

framework: 'custom',  // set to "custom" instead of cucumber.

frameworkPath: require.resolve('protractor-cucumber-framework'),  // path relative to the current config file

Now, similarly, you’d need to change the path of the spec inside the conf.js file to be the directory where your feature files are stored.

For a complete example, see this repo – this has complete set up of a Protractor and Cucumber set up.

  • What frameworks can be used with Protractor?

Ans : Protractor can be used with a variety of frameworks. By default, Protractor comes bundled with Jasmine 2.0. You can also pair Protractor to use mocha, CucumberJS, Serenity.

If you want to use it with any other custom frameworks, then you can do that also. Read about this configuration from the Protractor docs here.

  • How can you do reporting in Protractor?

Ans : Protractor doesn’t comes bundled with any reporting tool. However based on the framework that you’re using, you can use a lot of reporters. For eg, you can use Allure with Jasmine 2.0 with Protractor, Mochawesome when using with Mocha and Cucumber specific reporters when working with Cucumber.

Here is a detailed tutorial about using reporters with Jasmine in Protractor.

  • Can I use Protractor with TypeScript?

Ans : Absolutely Yes. You can use TypeScript with Protractor. There is a whole section in Protractor docs dedicated to configuration and examples.  You can also use this example to get configured.

  • How can I add custom locator for Protractor?

Ans : You can use the addLocator method to do the same. Please go through this sample example to understand how it can be done.

  • What is the default time for a Protractor with Jasmine spec to fail and can I change it.

Ans : By default, any spec in Protractor will fail after a period of 30 seconds because that is the default time out for a Jasmine spec to fail.

Yes, this timeout period can be configured to be increased or decreased.  In order to change the default timeout period for all specs, you need to add

jasmineNodeOpts: {defaultTimeoutInterval: timeout_in_millis}

to you conf.js file , where the second option is the time period that you want to set.

Note : In case you’re using with Mocha, this can be set using mocha.opts file.

  • How to run multiple specs in Protractor?

Ans : In order to run multiple spec files in Protractor, you just need to mention them in the spec flag in an array.

For eg, let’s say I have two different spec files test1_spec.js and test2_spec.js, so I can do this

specs : ['./test/test1_spec.js','./test/test2_spec.js']

This will make Protractor run these multiple spec files.