Passing a function as parameter [Protractor + Javascript]

If you look for how to pass a function as a parameter in javascript you will find solutions like this:

bar(function(){ foo("Hello World!") });

This week I learned how to use bind which I found more readable than the method above.

The bind structure is:

function.bind(thisArg,arg1,arg2,...)

thisArg – set the value of “this” to an specific object. This becomes very helpful as sometimes this is not what is intended.

arg1, arg2 – a list of values whose elements are used as the first arguments to any call to the wrapped function.

So, in this assertFirst I call a function passing another function as a parameter.

assertFirst: function() {
    return this.assertion(consumer.assertThatConsumerIsValid);
}

And after assertSecond I call a function and pass a function with bind parameters, ignoring the context.

assertSecond: function(element) {
    return this.assertion(consumer.assertThatConsumerIsDisplayed.bind(null,
 element));
}

Then I receive the function as a parameter and call it inside this assertion.

assertion: function(assert) {
    var consumers = browser.model.getConsumers();
    var promises = [];
    for (var i = 0; i < consumers.length; i++) {
        browser.navigation.goToDetailsConsumer(i);
        promises.push(assert());
    }
    return Q.all(promises);
}

 

Basically I am calling this assertion function to go to each consumer page and assert that is valid and has an element for each of them.

Promise Manager (Protractor – WebDriverJs)

Hey guys,

I have coded in Java, C# and Ruby since I’ve started work with Automation, so about 8 years ago. It’s my first time coding in Javascript for AngularJs pages and for those who are like me you need to know that if you are working with protractor or webdriverjs they return promises from all of its browser interactions, which could lead to some confusing behaviour.

 

What is the promise manager ?

Protractor and webdriverjs are asynchronous and every result returns a promise. They use a custom promise library called ControlFlow, which implicitly synchronizes asynchronous actions, making it so you only have to register a promise callback when you want to catch an error or access a return value. The promise module coordinates the flow of the asynchronous tasks.

This Promise Manager ensures that calls to the browser are run in sequence, and you only need to worry about dealing with the Promises when you wish to do something with data from the page.

In practice, this means that you can ignore the returned promises while interacting with the page.

 

So, instead of:

 

driver.get('https://azevedorafaela.wordpress.com')
 .then(function() {
   return driver.findElement({class: '.searchtip'});
 })
 .then(function(search) {
   return search.sendKeys('webdriver');
 })
 .then(function() {
   return driver.findElement({class: '.button'});
 })
 .then(function(button) {
   return button.click();
 });

 

You can have:

 driver.get('https://azevedorafaela.wordpress.com');
 driver.findElement({class: '.searchtip'}).sendKeys('webdriver');
 driver.findElement({class: '.button'}).click();

 

The trick part is when you want to extract the values from the page to do an assertion for example:

 driver.get("https://azevedorafaela.wordpress.com");
    driver.getTitle().then(function(pageTitle) {
        assert.equal(pageTitle, "Rafaela Azevedo");
 });

 

You can find more information on the links below. Thank you ! See you next weekend 🙂

 

Resources: http://blog.scottlogic.com/2015/03/04/webdriverjs-and-promises.html

https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise.html

 

How to setup Protractor + Cucumber

Hello guys, I will post about how to install protractor and cucumber to test an AngularJS and how to import this project on your Eclipse.

You need JDK and Node.js installed:

– Install node in your mac

brew install node
node --version

– Check the version of you java

java -version

– Git clone this project as an example:

https://github.com/AndrewKeig/protractor-cucumber.git

– Install protractor and cucumber through npm, update and start the webdriver server

npm install -g protractor
npm install -g cucumber
protractor --version
webdriver-manager update
webdriver-manager start

– You will see a message like this on your console:

11:13:08.586 INFO - Selenium Server is up and running

– Run cucumber.js on the protractor-cucumber’s project folder

– If you have the error below, move the features folder to outside the examples folder and open the /features/step_definitions/steps.js and remove one of the “../”:

  return binding.lstat(pathModule._makeLong(path));

                 ^

Error: ENOENT: no such file or directory, lstat '/Users/Documents/workspace/protractor-cucumber/features'

    at Error (native)

    at Object.fs.lstatSync (fs.js:887:18)

    at Object.realpathSync (fs.js:1518:21)

    at Object.expandPathWithExtensions (/usr/local/lib/node_modules/cucumber/lib/cucumber/cli/path_expander.js:14:23)

    at /usr/local/lib/node_modules/cucumber/lib/cucumber/cli/path_expander.js:8:27

    at Array.map (native)

    at Object.expandPathsWithExtensions (/usr/local/lib/node_modules/cucumber/lib/cucumber/cli/path_expander.js:7:31)

    at Object.expandPaths (/usr/local/lib/node_modules/cucumber/lib/cucumber/cli/feature_path_expander.js:9:38)

    at Function.Configuration (/usr/local/lib/node_modules/cucumber/lib/cucumber/cli/configuration.js:21:63)

    at getConfiguration (/usr/local/lib/node_modules/cucumber/lib/cucumber/cli.js:46:38)

Rafaelas-MBP-2:protractor-cucumber rafaelasouza$ cucumber.js

module.js:341

 

If you want to run on Eclipse:
– Install nodeclipse, a Node.js plugin for Eclipse

npm install -g nodeclipse

nodeclipse -g

– Import this project into your workspace and install Enide on your eclipse.

Thank you guys ! See you tomorrow !