0

i need some help with playwright and cucumber

i set up a gherkin and some tests for it, but i have problems with the expect command.

here are my imports:

import { ICustomWorld } from "../support/custom-world";
import { Given, When, Then } from "@cucumber/cucumber";
import { expect } from "@playwright/test";

and here is a sample of the problem:

Then(
  "The Menu Niederlassungsdaten is default",
  async function (this: ICustomWorld) {
    const { page } = this;
    const TitleLabel = await page?.locator('#app > div.container-fluid > div.row.content.justify-content-center > div > div > div > div.col-12.col-md-3.col-lg-2.tabs > div > a.list-group-item.text-center.router-link-exact-active.active');
    await expect(TitleLabel).toHaveAttribute("aria-current","page");
  }
);

This expect.toHaveAttribute is working perfectly without cucumber. But in the given when then from cucumber is it not working. The error is:

Error: toHaveAttribute must be called during the test

So it seems the expect command is missing this playwright test command. What can i do to get this working in cucumber?

KiritoNG
  • 1
  • 3
  • You do not need to add await infront of expect statement. It should be called without await keyword as TitleLabel already have in it. – Sreenivasulu Nov 25 '21 at 09:03

1 Answers1

0

The issue you're experiencing is because the expect function from Playwright's test library is specifically designed to work in the context of Playwright's test runner. It doesn't quite play well when you try to run it within Cucumber because Cucumber uses a different test runner.

To solve this problem, you will need to switch out the assertion library you're using with one that doesn't have context-specific restrictions, e.g. Node.js' built-in assert module is pretty solid. It's not as feature-rich as some other libraries, but it can definitely handle an attribute check.

Luc Gagan
  • 301
  • 9