Protractor Jasmine experiencing issues with describe block within the it block

I am currently exploring Jasmine and attempting to incorporate shared steps in my test cases. I want to reuse certain steps between two scenarios, but when I try to execute a common describe block inside an it block, it does not run as expected. Here is a simplified example of the code:

describe('Main Method 1', (){
  it('Function 1', (){
      console.log('Function 1');
      describe('Function 1',(){
         it('Function 1.1',(){
         console.log('Function 1.1');
         })
         it('Function 1.2',(){
         console.log('Function 1.2');
         })
      });
  });
  it('Function 2', (){
      console.log('Function 2');
      describe('Function 2',(){
         it('Function 2.1',(){
         console.log('Function 2.1');
         })
         it('Function 2.2',(){
         console.log('Function 2.2');
         })
      });
  });
});

The issue here is that Function 1 & Function 2 are considered separate scenarios and I am looking for a way to share specific scenarios within Main Method 1.

While Function 1 & Function 2 get printed, the nested steps like Function 1.1, Function 1.2, Function 2.1, and Function 2.2 do not appear in the output.

If anyone has any insights or suggestions on this matter, I would greatly appreciate it.

In a more practical implementation scenario, the structure might be similar to:

describe('Main Method', (){
   it('F1', () {
       Function1();
   });
   it('F2', () {
       Function2();
   });
});

Function1(){
  describe('Function 1',(){
     it('Function 1.1',(){
     console.log('Function 1.1');
     })
     it('Function 1.2',(){
     console.log('Function 1.2');
     })
  });
}
Function2(){
  describe('Function 2',(){
     it('Function 2.1',(){
     console.log('Function 2.1');
     })
     it('Function 2.2',(){
     console.log('Function 2.2');
     })
  });
}

Answer №1

If you have multiple similar tests, it's a good practice to group them all under one describe block. You can use beforeAll to set up anything that needs to happen before the tests and afterAll for tasks after the tests. Additionally, you can insert actions between each test using the beforeEach() function.

It is important to assign each operation to its own it block, like "Open browser" or "Click login button". Nesting a describe block inside an it block is not recommended.

describe('Main Method 1', () {

    describe('Function 1', () {

     beforeAll(function() {
         console.log('Start of Function 1');
     });

     beforeEach(function() {
         console.log('Start of next test');
     });

     afterAll(function() {
         console.log('End of Function 1');
     });

     it('Function 1.1',(){
         OpenPage();
     });

     it('Function 1.2',(){
         console.log('Function 1.2');
     });
  });

    describe('Function 2', () {

     beforeAll(function() {
         console.log('Start of Function 2');
     });

     it('Function 2.1',(){
         OpenPage();
     });

     it('Function 2.2',(){
         console.log('Function 2.2');
     });
  });
});

function OpenPage() {
    // open page 
    browser.get('http://google.com/');
    // Additional code
}

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

Python script to extract data from a dynamic JavaScript webpage

I've been involved in a research project where we are aiming to gather a list of reference articles from the Brazil Hemeroteca (The specific page reference we are seeking: , needs to be located by extracting data from two hidden elements on this page: ...

Having trouble with finding elements using the driver.find_element_by_xpath method?

<div id="primefacesmessagedlg" class="ui-message-dialog ui-dialog ui-widget ui-widget-content ui-corner-all ui-shadow ui-hidden-container" role="dialog" aria-labelledby="primefacesmessagedlg_title" aria-hidden=&qu ...

Placing a blank object after every line within a TypeScript array

I'm currently working on creating an iterator using the .map() function, specifically in this way: const csv = this.invoices .map(data => ({ invoiceId: data.invoiceId, invoiceDate: data.invoiceDate, invoiceType: data.invoiceType, ...

Configuring Firefox settings in Nightwatch

Is there a way to set Firefox preferences in nightwatch? I am trying to achieve the same thing in Java using nightwatch. To set Firefox preferences in nightwatch, you can use the following code snippet: FirefoxProfile profile = new FirefoxProfile(); prof ...

What is the method for exporting all functions from a TypeScript file with a default export included?

An example of the export type mentioned is React. To use it, we can include the following code: import React, {Component} from "react". This allows us to access both React.Component and Component. What steps are necessary to make this possible? ...

Determining when to include @types/packagename in React Native's dev dependencies for a specific package

Just getting started with React Native using typescript. Take the package vector icon for example, we need to include 2 dependencies: 1. "react-native-vector-icons": "^7.1.0" (as a dependency) 2. "@types/react-native-vector-icons": "^6.4.6" (as a dev ...

`In Selenium using Python, the browser window opens a URL but abruptly closes``

On opening the Facebook page in the browser, I am encountering an issue where it fails to click on the "Create New Account" button as expected. I have used the following XPATH for locating the Create New Account button: //a[text()='Create New Account& ...

Protractor throwing error: 'Cannot access click property of undefined'

I need assistance with clicking on a specific CSS locator to perform a "math floor" operation. Can someone help me with this? The CSS code snippet in question: <input ng-attr-id="industry_{{$index}}" ng-attr-value="{{$index}}" ng-model="stateData.curr ...

Combining Repetitive Elements in an Array

Trying to combine an array of products with the same order_id while also including all objects from a second products array. Below are some sample orders: const orders = [ { "order_details": { }, "order_id": "1", ...

Is there a way to store the information from my second loop in a separate row within a CSV file?

In the code snippet below, a function is defined to search for specific words within a web page and save the results in a CSV file. However, there seems to be an issue where the data from the second loop replaces the data saved from the first loop. class ...

A guide to updating a table in Angular using an API response

My form allows me to select events from a drop-down list and choose a date. Depending on the selected date, it retrieves the number of events that occurred on that specific date. If I select an event and a date where events took place, the "All Events" sec ...

Serve both .ts and .js files to the browser using RequireJs

In my ASP.NET Core Project, the following files are present: greet.ts export class WelcomMesssage { name: string; constructor(name: string) { this.name = name; } say(): void { console.log("Welcome " + this.name); } } GreetExample.ts import * as ...

What is the process of instructing Webdriver to utilize a particular edition of ChromeDriver?

Looking to execute Selenium tests in a command line interface, I've encountered an issue with Chrome version compatibility. Upon running the tests, WebDriver automatically downloads and utilizes ChromeDriver version 2.27, although I require version 2. ...

Checking JavaScript files with TSLint

After spending many hours attempting to make this work, I still haven't had any success... I am wondering: How can I utilize TSLint for a .js file? The reason behind this is my effort to create the best possible IDE for developing numerous JavaScrip ...

Using the <head> or <script> tag within my custom AngularJS2 component in ng2

When I first initiate index.html in AngularJS2, the structure looks something like this: <!doctype html> <html> <head> <title>demo</title> <meta name="viewport" content="width=device-width, initial-scal ...

Utilizing Selenium in Python has proven successful in locating elements by tag name and executing loops effectively, but unfortunately, the same level

I am in search of a 'div' element with the title 'Type a message'. The code below is successful: div_nodes = driver.find_elements_by_tag_name('div') for element in div_nodes: if element.get_attribute('title') == ...

Using the Ajax method from a separate class in TypeScript: A step-by-step guide

Recently, I started learning about typescript and ajax. One of the challenges I encountered was while creating a method in typescript for making ajax calls that can be used across classes: myFunc(value: string): JQueryPromise<any> { var dfd = $. ...

Zero duration recorded on Google Analytics for pages accessed by Selenium automated testing tools

My goal is to access a website using Selenium and Python with the following code snippet: browser = webdriver.Firefox() browser.get(url) time.sleep(20) browser.close() However, when I analyze the duration in Google Analytics after an hour, the duration s ...

Is there a way to access the path to the global npm modules in JavaScript?

Is there a way to retrieve the path to the global npm_modules in javascript? When using the command line, I can obtain the base path to the npm modules by running the following: npm config get prefix How can this path be accessed within a javascript fil ...

Display the dynamic change of minutes and seconds every second on an Ionic HTML page

I created a JavaScript countdown counter that displays minutes and seconds using the following line of code: document.getElementById("demo").innerHTML = Number(this.minutes) + 'm' + Number(this.seconds) + 's '; However, on iOS devices ...