Sauce Labs encountering issues when running JavaScript code

Currently, I am using Selenium WebdriverJs along with Mocha to conduct tests on Sauce Labs via Travis CI. After isolating the issue without any project dependencies, I am seeking help.

The interesting observation is that defining an additional object with properties for visiting a URL and scrolling down within the test script itself works perfectly fine. You can find the link to the test script here.

If we set it up 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 driver
eventPage.init(driver)
eventPage.visit('http://reddit.com')
eventPage.scrollDown().then(function() {
  console.log("This operates smoothly on Sauce Labs");
});

This setup performs well on Sauce Labs. The Travis build link is here, and the Sauce Build link is here.

However, when I create a file named eventPage.js and import it with all the above functions into the test script, it stops working. The link to that specific file is here, and the test script link is here.

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);                                                                                     
  }                                                                                                                                    
};  

Subsequently, importing it into my test script yields an error,

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 results in an error");
});

This error manifests on Sauce Labs. The failed build link on Travis CI is available here, while the corresponding Sauce Labs link is accessible here. Notably, both methods work flawlessly on my local machine. Any assistance would be greatly appreciated, as I have dedicated considerable time to resolve this issue. Thank you and have a pleasant day!

Answer №1

When working with modules, it's important to remember that they are cached and imported as class prototypes. This means that in order to prevent conflicts, you should create a new instance:

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

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

UPDATE

The issue at hand is connected to istanbul. The application injects a global variable into the scroll function for tracking execution, but this variable remains undeclared because the function is run in the browser, not Node.

To address this problem, one solution is to utilize executeScript with the script provided as a string:

return this.driver.executeScript("window.scrollTo(0, arguments[0]);", 800);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leverage ngFor to loop through a "highly intricate" data structure

In my project, I have stored data in a JSON file structured as follows: { "name": { "source1": ____, "source2": ____, "source3": ____ }, "xcoord": { "source1": ____, "source2": ____, "source3": _ ...

When scanning through the HTML code for the XPath, it illuminates the precise route that is required. However, despite this, it fails to function properly

Encountered an exception while running the code: Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(., ...

Traverse through the JSON data until a certain condition is satisfied, then proceed to tally the number

Looking to parse a json file containing user data and points. The goal is to loop through the data until the _id matches the userId, then determine the position of that user in the list based on the number of objects. The json file provided below already ...

What could be causing my jQuery script to malfunction?

After scouring through numerous Stack Overflow questions and conducting countless Google searches, I am still stumped by the issue at hand. As a beginner in web development, all I want is for this simple page to function properly. Can someone please point ...

Where is the ideal location to store non-component data properties in Vue.js?

Currently, I am in the midst of a Vue.js project where components are predominantly used. However, there are moments when I simply need to incorporate some Vue.js syntax without creating an entire component. Since I am not utilizing the render function as ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

Load images easily using jQuery

I am experiencing a small problem. I am attempting to display an image, but it doesn't seem to be correct. Although I am able to retrieve the data of the image, my goal is just to display it instead. What could be causing this issue? var imageUrl = ...

The Django POST request is rejecting due to a missing or incorrect CSRF token, despite having included the token in the form

I'm encountering a 403 response when making a POST request despite including csrf_token in the data for an AJAX request. I made sure that csrf_token is not empty before sending the request, so everything seems correct. What could be causing this error ...

To close the responsive menu, simply click anywhere outside of the navigation bar

My issue involves a responsive menu with Bootstrap. On desktop, the menu closes fine; however, on the responsive view, I want it to close when clicking outside of the nav menu in any area. Here is my navigation code: <!-- Navigation --> <nav id= ...

In what scenarios would it be beneficial to retrieve an object in the Page Factory design pattern?

After conducting extensive research on the internet, I have not been able to find the relevant information I need. Below is an example code snippet: public class HomePage { @FindBy(id = "fname") WebElement name; @FindBy(id = "email") Web ...

Is it possible to process HTML and JavaScript as a request in JMeter?

After receiving a response, I noticed that the HTML body contains a hidden form along with an internal JavaScript function to submit the form (view snapshot here). Is there a method in JMeter to execute JavaScript code that directly submits a form? I am ...

Button that vanishes when clicked, using PHP and HTML

I am in the process of creating an online store and I am seeking to implement a button that directs users to a new page, but then disappears permanently from all pages within the store. Here is the code snippet I have developed so far: <input class="b ...

Implementing a change event upon setting a value to an input element using JavaScript

My plan is to develop a Chrome extension that can automatically fill passwords. The code for this functionality looks like the following: //get the account document.querySelector("input[type=text]").addEventListener('input', () => { ...

Enhance the "content switcher" code

I have been working on improving my "contenthandler" function. It currently changes different articles when I click different buttons, which I am satisfied with. However, I believe there may be a better approach to this and would appreciate any advice on h ...

Reactive form allows you to easily format dates

Currently, the date displayed is 1/4/2022. We need it to display in the format 01/04/2022. Can we achieve this formatting using reactive forms with the sample Model form provided below? Thank you. How can we format it when starting from transactionStartD ...

Innovative functionality for adding and deleting elements in JQuery/JavaScript

Is there a way to dynamically add and remove items when the Append/Clear buttons are clicked using this code snippet? $("#btn1").click(function(){ $("ul").prepend("<li>List Item</li>"); }); $("#btn2").click(function(){ $("ul").remove ...

Struggling to find your way around the header on my website? Let me give

I'm looking to display certain links prominently at the top of a page, similar to this example: "home > page1 > link1 > article 1." Can someone advise on how to achieve this using HTML, PHP, or jQuery? ...

Python: Automate clicking on checkboxes with Selenium

Recently, I've encountered a problem and I'm in search of an answer. Below is some HTML code that I need to work with. My goal is to send a snippet of this HTML to selenium so that it can click on a checkbox. However, I don't want to use the ...

Learn how to efficiently execute a function multiple times using pure JavaScript

I am trying to create a tabbed content functionality with multiple elements. How can I utilize the same function for various elements declared in a variable? For example, I want to clone the parent div.tabs element with similar content but different ids an ...

Put a cookie in place to deter users from returning to the website

Looking for a way to prevent access to my contest site once the form has been submitted. Is there a way to achieve this? Thanks in advance ...