What is Protractor? How is it different from Selenium?
Similarities and differences between Protractor and Selenium with Java/C#
Protractor and WebDriver architecture
- Install Node.JS
- Install Protractor using command: npm install protractor -g
- Install/Update webdriver manager: webdriver-manager update
- Install latest version of NetBeans/phpStorm
Set up conf.js file
Conf.js file is the configuration file which controls the execution, where reports can be configured, environment details and browsers can be configured for a run. It looks something like this:
Apart from the usual element by XPath, by id, name, CSS etc, Protractor offers additional bindings like by model, repeater, buttontext, bindings and by partialbuttontext
Usage: (Click here to get the list)
Handling Multiple elements:
Let’s assume the page has multiple elements with the same definition which user has to verify/use in the script
// element.all returns a promise. We’ll discuss about promises further section
Let’s take the same example of button and textbox in the application.
Writing first test case using Jasmine
I’m using an example of a sample application https://confr.com/ mentioned in conf.jsFile
Save this as testcase1spec.js and place it under a folder called as spec
The test case would look something like this:
Running the test case
- Open the terminal
- Start webdriver Manager:
- Run webdriver Manager start in the terminal
- Execute test case using the configuration file
This is the tricky part of Asynchronous programming.
Behind the scenes of WebDriver, every call that interacts with the browser, such as get(), element(), sendKeys(), and click(), isPresent() is being scheduled and pushed onto the WebDriver ControlFlow allowing us to worry about using .then() on the resulting promises (unless we specifically want the result returned by the call).
would return promise. So, the user can use:
What truly happens in the above example:
Any time an asynchronous call is invoked, it’s put in the queue where it will wait for all previous commands to complete prior to execution.
Since control flow acts like a cashier in fast food where he serves multiple customers one after the other (there’s no scope for small talk). The control takes the order, promise to serve the order and asks the customer to move aside to take the next customer’s order. Eventually, each customer gets served in the order queue.
Basically the method element(by.XPath(‘//a[text()=”Register”]’)) would only run once the browser.get() promise is resolved. More details can be found here.
Will explain Page object model in future blogs
There are considerable advantages of using Protractor over Java/Selenium. They are as follows:
- Typecasting: This can be a major headache in object-oriented programming. When a function/method returns something, the calling function needs to handle the datatype.
- Asynchronous Programming: Though it might be a little tricky to understand at first, once the user gets the hang of it, it’s simple. The control doesn’t have time for small like
- What’s your data type?
- Have you converted the data?
- Can you send me only the type of object I want?
It can become a huge headache sometimes. This makes the execution faster.
- The user does not have to write explicit/implicit waits. The control flow waits for the page to load (rest calls to finish).
- Simple to code and you need to create objects, initialize it as static. Everything is managed internally so that memory is utilized. Come to think of it as, programmers who are bored with OOP.
- Thousands of NPM modules available to utilize. Ex-reporter, name and password generators which can be used in data generation.