3

I am using Selenium WebdriverJs in conjunction with Mocha to run tests on Sauce Labs via Travis CI. I have isolated my problem devoid of any project dependencies. Please help.

So, what happens is that if I try to define an additional object having properties of visiting the URL and scrolling down, inside the test script itself, and then use that object to do stuff then it works fine. The link to the test script is here https://github.com/Princu7/open-event-webapp/blob/stripped/test/serverTest.js

If we do it like this:

var eventPage = {                                                                                                                      

  init: function(webdriver) {                                                                                                          
    this.driver = webdriver;                                                                                                           
  },                                                                                                                                   

  visit: function(url) {                                                                                                               
    return this.driver.get(url);                                                                                                       
  },                                                                                                                                   

  scrollDown: function() {                                                                                                             
    function scroll() {                                                                                                                
      window.scrollTo(0, arguments[0]);                                                                                                
    }                                                                                                                                  
    return this.driver.executeScript(scroll, 800);                                                                                     
  }                                                                                                                                    
};     
var driver = // Initialize the selenium webdriver
eventPage.init(driver)
eventPage.visit('http://reddit.com')
eventPage.scrollDown().then(function() {
  console.log("This works fine on Sauce Labs");
});

This works fine on the Sauce Labs. Here is the link to the Travis build https://travis-ci.org/Princu7/open-event-webapp/builds/252652917 and the link to the Sauce Build https://saucelabs.com/beta/tests/4cf734a141fb42548fff1ee623130c44/logs#3

Now, if I create a file called eventPage.js and import it containing all the above functions into the test script, then it doesn't work. The link to the that file is https://github.com/Princu7/open-event-webapp/blob/stripped2/src/selenium/eventPage.js and the link to the test script is https://github.com/Princu7/open-event-webapp/blob/stripped2/test/serverTest.js

module.exports = {                                                                                                                     

  init: function(webdriver) {                                                                                                          
    this.driver = webdriver;                                                                                                           
  },                                                                                                                                   

  visit: function(url) {                                                                                                               
    return this.driver.get(url);                                                                                                       
  },                                                                                                                                   

  scrollDown: function() {                                                                                                             
    function scroll() {                                                                                                                
      window.scrollTo(0, arguments[0]);                                                                                                
    }                                                                                                                                  
    return this.driver.executeScript(scroll, 800);                                                                                     
  }                                                                                                                                    
};  

And then import it into my test script,

var eventPage = src('path of the above file');
var driver = // Initialize the selenium driver
eventPage.init(driver) 
eventPage.visit('http://reddit.com');
eventPage.scrollDown().then(function() {
  console.log("This given an error");
});

This gives an error on the Sauce Labs. Here is the link to the failed build on Travis CI https://travis-ci.org/Princu7/open-event-webapp/builds/252655787 and the Sauce Labs Link https://saucelabs.com/beta/tests/5d240513c5e74e639b9abb320316592d/logs#3. Just for confirmation, both the methods are working on my local machine. Please help. I have invested so much time on this. Thanks!! Have a nice day!

1 Answers1

2

Modules are cached and your imported module is a class prototype. Thus you need to create a new instance to avoid conflicts:

var EventPage = require('./EventPage.js');

var eventPage = Object.create(EventPage);
eventPage.init(driver) 
eventPage.visit('http://reddit.com');
eventPage.scrollDown().then(function() {
  console.log("This given an error");
});

EDIT

The issue is related to istanbul. The application injects a global variable in the scroll function to track the execution, but the variable remains undeclared since it is executed in the browser and not in node.

One way to overcome this issue is to call executeScript with the script as a string:

return this.driver.executeScript("window.scrollTo(0, arguments[0]);", 800);
Florent B.
  • 37,063
  • 6
  • 68
  • 92
  • Thanks for helping. I did try that but it still gives the same error.Here is the updated test script https://github.com/Princu7/open-event-webapp/blob/stripped3/test/serverTest.js and the same file which is exported https://github.com/Princu7/open-event-webapp/blob/stripped3/src/selenium/eventPage.js. The Travis fails and gives the exact same error https://travis-ci.org/Princu7/open-event-webapp/builds/252785280. Also, here is the link to the Sauce Build https://saucelabs.com/beta/tests/746198536e80411c9c08fc3f32e2a2fa/logs#3. What can I do? I have already contacted the Sauce Help Center. Help – Abhishek Rastogi Jul 12 '17 at 12:35
  • 1
    I had a look at your failing build. It seems that your issue is related to istambul and not Selenium. My guess is that istambul is injecting a global variable in your `scroll` function to track the execution, but the variable remains undeclared since it is executed in the browser and not in node. To overcome this issue, I would try to call the function as a string: `return this.driver.executeScript("window.scrollTo(0, arguments[0]);", 800);` – Florent B. Jul 12 '17 at 13:05
  • Yes. That was it. Thank you so much. I finally have a reason why it wasn't running there. Istanbul was causing the problem. I will now modify it accordingly. Thank you so much :) You are a savior!! – Abhishek Rastogi Jul 12 '17 at 14:00