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 ajax/js to dynamically refresh a single select tag within a set of multiple

While working on a project, I encountered a challenge while using JavaScript to update a <select> tag within a page. The specific issue arises because the page is designed in a management style with multiple forms generated dynamically through PHP ba ...

Encountering an error: "Unable to assign the 'id' property to an undefined object while attempting to retrieve it"

I'm running into an issue while attempting to retrieve a specific user from Firebase's Firestore. export class TaskService { tasksCollection: AngularFirestoreCollection<Task>; taskDoc: AngularFirestoreDocument<Task>; tasks: Obs ...

"Ensuring Contact Information is Unique: A Guide to Checking for Existing Email or Phone Numbers in

I'm encountering an issue with validating email and phone numbers in my MongoDB database. Currently, my code only checks for the presence of the email but does not respond to the phone number. const express = require("express"); const router ...

Discovering the method to extract a specific section from a lengthy string

Looking to extract phone numbers from an HTML source code using PHP? Each phone number in the code starts with 'phone=' and ends with %. For example, consider the following sample HTML code: b2e1d163b0b4dc6ebfa5&amp;t=s&amp;phone=9535503 ...

React-native horizontal sliding plugin

Can anyone recommend a reliable horizontal slider plugin for react-native? I've come across some options, but they haven't been working as smoothly as I'd hoped. ...

Issue locating the bottom of the scroll bar

My attempt to detect when the scroll reaches the bottom of a div involves using this code: $('.scrollpane').scroll(function(){ if ($(this).scrollTop() + $(this).height() === $("#results").height()) { alert('scroll at bottom&a ...

Ajax - unable to show posts upon submission

Whenever I submit a post, I am unable to display them and I'm not sure why it's not working. The getPosts() function works fine when I refresh the page. However, after submitting the posts, I can't seem to retrieve them. I am using a JSON fa ...

Utilizing titanium to develop a functionality that listens for button presses on any area of the screen

I am trying to simplify the action listener for 9 buttons on a screen. Currently, I have individual event handlers set up for each button, which seems inefficient. Is there a way to create an array of buttons and manipulate them collectively? For example ...

What occurs when socket.io events are not properly handled?

Is socket.io ignoring or dropping messages? I am asking this because there is a client with multiple states, each having its own set of socket handlers. The server notifies the client of a state change and then sends messages specific to that state. Howeve ...

Struggling to generate an appium-android xpath with following-sibling and child elements

Can someone assist me in creating an XPath for locating UNLIMITED DATA using the code //android.widget.TextView[@text='Active']? I attempted to find the element with the XPath below, utilizing following-sibling and child, but it was unsuccessful ...

Request for Selenium element

I'm struggling to locate an element using the Selenium library. I've attempted to find it by id, by Xpath, but so far, no luck... I suspect the issue lies within the HTML iframe or framesets, despite also trying driver.switch_to.frame without any ...

Setting the counter based on the status (Pass or Fail) of the test case

Whenever the automated script runs, a counter should increase for each execution except when it's a new system date. If the system date changes, the counter should reset to 0 and start from 1 on the next run. I aim to record both the current date and ...

In the React js and emotion framework, fonts are only loaded in production mode after the page has been re

I have set up emotion's Global component to globally apply a font to my app: export const GlobalStyle: FC<{}> = () => ( <Global styles={css` @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;300;400;50 ...

Electron's start script leads to project failure

Despite extensively searching the internet for a solution, I have yet to find a definitive answer that actually works. My last resort was downloading the example from Electron's website and directly inserting the scripts into my project. However, whe ...

Discovering the art of incorporating various color palettes within a single stylesheet using HTML

I'm curious about incorporating multiple color schemes into a single stylesheet that can be activated by clicking, similar to the functionality found in platforms like WordPress and Magento. These platforms offer themes with various color options avai ...

Deactivate the other RadioButtons within an Asp.net RadioButtonList when one of them is chosen

Is there a way to disable other asp.net radio buttons when one of them is selected? I have three radio buttons, and I want to disable the other two when one is selected. After doing some research, I managed to disable the other two when the third one is se ...

Enabling individuals to transfer their content to Amazon S3

I have set up an S3 bucket named BUCKET in region BUCKET_REGION. I want to enable users of my web and mobile apps to upload image files to this bucket, with specific restrictions based on Content-Type and Content-Length (specifically, only allowing jpegs u ...

Customizing the width of the JQuery $ui.progressbar by overriding its properties

After spending some time, I successfully overrode the function that controls the width of the progress bar element in the jQuery UI widget version 1.12.1 from September 14, 2016. Initially, I needed the progress bar to completely fill a column in a table. ...

Import Document from Project - Automation Framework Visual Studio 2010 C# using Selenium WebDriver

Is it possible to upload a file to a webpage using Selenium WebDriver in VisualStudio10 without specifying the file's path on my computer? Instead, can I add the file to the project and access it through code without having to hardcode the file path? ...

Obtain data from the DOM using ng-select in Angular 10

In my Angular 10 project, I have certain views where I utilize ng-select to showcase and obtain data. The QA team has tests in place that depend on element id and DOM value, specifically designed for basic select elements. With a simple select, we are able ...