What is causing my beforeAll() function to not be properly awaited for completion?

I am currently developing an automation framework for a web application using WebDriver and Jasmine. The application functions as an online library, displaying various products with unique content. My automation script is designed to iterate through the different sections of each product and execute tests on them.

To test different products, I require an identifier as input. This identifier is used in the beforeAll function to fetch information about the specific product from a web service.

const identifier = process.argv[3];

describe('product check', function() {
  var appData;

  beforeAll(async function() {
    // Retrieving data from async web service
    appData = await getAppData(identifier);
  })
}

The automation script should then loop over the appData structure and create expectations based on its contents.

In my understanding of implementing loops within Jasmine, expectations need to be placed inside a function and called repeatedly within the loop:

// Incorrect approach
for(var i = 0; i<appData.numInstances; i++) {
  it('is within a for loop', async function() {
    expect(...); 
  })
}

// Correct approach
function containsIt(i) {
  it('is within a function', async function() {
    expect(...); 
  })
}

for(var i = 0; i<appData.numInstances; i++) {
  containsIt(i)
}

However, if the expectations are placed within a function as shown above, the automation script does not wait for the beforeAll function to complete before calling the function containing it(), resulting in an error:

TypeError: Cannot read property 'numInstances' of undefined

I have verified that getAppData() functions correctly and that appData is populated within the beforeAll() function.

While I could incorporate the loop over appData within an it() block, this would mean all my expect() statements would be grouped together in the same block, losing detailed reporting from it().

Is there a way to implement a loop where functions containing it() are called iteratively while still loading application data in the beforeAll function? If so, how can this be achieved?

Answer №1

I have found myself in a similar situation in the past, and it seems that newer versions of Jasmine may have resolved this issue, although I cannot be entirely certain.

Upon reviewing the documentation, it appears that there is now support for async/await within the beforeEach function, suggesting that newer versions ensure everything has completed before moving on to the next steps.

To address your current issue, you can utilize the done callback provided by Jasmine.

beforeAll(async function(done) { // include done callback
    // asynchronous data retrieval from web service 
    appData = await getAppData(identifier);
    done(); // signal completion to Jasmine
  })

Integrating the done callback should help maintain the sequence where beforeAll is executed and finished prior to the first it.

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

Selenium-Web Driver encounters an issue with reading the 'filter' property as undefined

Recently, I started working with Selenium and encountered an issue while trying to wait for a specific element to load. The error message that popped up was: (node:8472) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to ...

The Node.js server is outputting an HTTP status code of 404

I have recently set up a small server. Interestingly, when I attempt to perform a GET request to my server through a browser, I can see the correct data. However, when I try to make a POST request to my server using code, I receive an HTTP status 404 error ...

Utilize Vue Fullcalendar's eventRender feature to seamlessly incorporate Vue components into your

I am utilizing the fullcalendar.io vue extension. I would like to customize event rendering in order to add actions, but the event callback only contains JavaScript elements. Is there a way to inject a vue component into it? <FullCalendar ref="fu ...

How can I include JavaScript in an HTML document?

My folder structure is as follows: Inside webapp/WEB-INF/some.jsp, I also have a javascript file located in the same directory at webapp/WEB-INF/js/myform.js. I referenced it in some.jsp like this: <script type="text/javascript" src="js/myform.js"> ...

Issue with dropdown menu in selenium is that it is unable to switch to the second option

Currently, I am in the process of web scraping on a specific page . When Selenium initiates the initial loading of the page, I encounter a requirement to select both the state and city. To address this, I attempted using the .click method. select_state = d ...

access the useState value within the event listener

I need to show a list of coordinates collected from a click event on a DIV. The listener should only activate when a button is clicked. import { createRoot } from "react-dom/client"; import React, {useCallback, useEffect, useState} from 'rea ...

Using AngularJS to show content based on a callback function

I'm a beginner in angular and it seems like I might be overlooking something. For the registration form, I need users to provide their location. Depending on whether they allow/support navigator.geolocation, I want to display a drop-down menu for cho ...

The "getJson" function fails to execute when attempting to run a Python

I've come across a simple code snippet, but for some reason, my getjson function isn't working as expected. My Python script properly prints a JSON encoded value. Does anyone have any suggestions? HTML <!DOCTYPE html> <html> <h ...

Display a single video on an HTML page in sequential order

My webpage contains a video tag for playing online videos. Instead of just one, I have utilized two video tags. However, when attempting to play both videos simultaneously, they end up playing at the same time. I am seeking a solution where if I start pla ...

Continuously pressing a button in Selenium with Python until it is no more visible

I'm having trouble with scraping data from the website: Specifically, I'm facing an issue with the while loop not clicking the "Load More" button after entering a zip code. The code seems to be encountering an error when trying to fetch location ...

Move the div containing an <audio></audio> tag

Is it possible to drag a div with a width of 200px and an element into a droppable area, and once the div is dropped, have its size change according to the sound duration (e.g. 1px per second)? Check out this example on jsFiddle. Below is the code snipp ...

Modifying inner content without altering CSS styling

Inside this div, there is a paragraph: This is the HTML code: <div id="header"><p>Header</p></div> This is the CSS code: #header p { color: darkgray; font-size: 15px; Whenever a button is clicked, the innerHTML gets updated: H ...

Troubleshooting the malfunctioning AngularJS ui-view component

My ui-view isn't functioning properly, but no errors are being displayed. Can anyone help me figure out what I'm missing to make this work? This is the main template, index.html. <!DOCTYPE html> <html> <head> <meta charset= ...

Can you embed a hyperlink within the foundation title accordion?

I am attempting to create clickable links within the Foundation accordion title, but every method I've tried so far only expands the accordion content instead. Check out this CodePen where I demonstrate exactly what I am aiming for (I substituted < ...

Unable to Click on Selenium Element

When a link is clicked on a website, the URL remains unchanged but a popup appears on the screen containing elements that can only be accessed after running driver.switchTo().defaultContent(). To access these elements, I have used a javascript executor com ...

The unordered list generated from XML in Ajax HTML code is not being added to the div

My approach involves creating an XML file to store items for my unordered list and accessing it via AJAX call on page load. However, I'm facing an issue where the AJAX result is not getting appended in my div element as expected. Interestingly, when I ...

Utilizing CodeIgniter for Efficient AJAX Posting

I'm struggling to submit a post using the ajax post method. It appears that the request is being posted without any error messages, but the model doesn't seem to respond and insert the row into the database. While I'm not well versed in jQu ...

Issues with executing google.script.run in Google Web App

I am encountering an issue with the function I have defined in my .gs file: function doGet(e) { return HtmlService.createHtmlOutputFromFile('index') } function goToMaster() { const ss = SpreadsheetApp.getActiveSpreadsheet() const sheet = s ...

ChartJS v2: Displaying scale value based on click coordinates for time scale

I am having an issue with a time-based line chart where I am trying to retrieve the values for each scale at the click coordinates. In my ChartJS options, I have defined an onClick function: onClick: function(event, elementsAtEvent) { console.log(eve ...

I am currently facing an issue in my Node.js environment specifically with the 'oracledb' package

I am encountering an issue with the oracledb modules. Fortunately, I was able to successfully install oracledb. When I run the command like this, -> npm install oracledb njsOracle.cpp njsPool.cpp njsConnection.cpp njsResul ...