Search code examples
node.jsprotractorjasmine-node

Console.log is executing before browser launches URL


Team,

I am declaring an element as a variable like txtSearch : element(by.xpath('//input[@type="search"]')), in some point I want to log the statement as

var clickOn = function(webElement){
webElement.click();
console.log("Successfully clicked on the " + webElement);
};

I am calling this function from Spec file like clickOn (txtSearch)

this console.log is logging the console before the statement gets executed i.e. first all the console.log statement is executing first even before URL has launched.

Please find the required details below:

Node Version: 3.10.3 Protractor Version: 4.0.3

Thanks in advance.


Solution

  • Welcome to asynchronous world!

    Because everything is async, to make execution order that you want, try this:

    var clickOn = function(webElement){
        return webElement.click().then(()=> {
            console.log("Successfully clicked on the " + webElement);
        });
    };
    

    This would schedule console log only when click was actually done on browser side.

    But i strongly suggest to read about promises and control flow in webdriverjs: http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise.html