question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] OR semantics with Playwright Selector Chaining (`>>`)

See original GitHub issue

Context:

  • Playwright Version: 1.24.2
  • Operating System: Mac
  • Node.js version: 18.5
  • Browser: All
  • Extra: N/A

Describe the bug

I’m using the >> selector in a broader statement made up of multiple selectors that is passed to a PlaywrightPage.locator, specifically, this:

fieldset:has-text('URL') >> input, button:has-text('Link Your Project')

Each of these independently (fieldset:has-text('URL') >> input and button:has-text('Link Your Project')) matches elements on the page, but when combined together, only the elements matching fieldset:has-text('URL') >> input are selected. This leads me to believe that the >> operator is corrupting the whole statement; Just like with CSS selectors, I’d expect the , to separate the individual statements so they can be evaluated independently, and their results combined.

Is there something I am misunderstanding about the >> operator, or is this a bug?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:10
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rwollcommented, Sep 20, 2022

I reviewed the following with @dgozman. It is working as expected and the last statement will fail:

import { test, expect } from '@playwright/test';

test('should work', async ({ page }) => {
  await page.setContent(`
    <form>
      <fieldset>
        <label for="url">URL</label><br/>
        <input type="text" id="url" name="url">
      </fieldset>
      <button>Link Your Project</button>
    </form>
  `);
  await expect(page.locator("fieldset:has-text('URL') >> input")).toBeVisible();
  await expect(page.locator("button:has-text('Link Your Project')")).toBeVisible();
  await expect(page.locator("fieldset:has-text('URL') >> input, button:has-text('Link Your Project')")).toHaveCount(2);
});

In this particular case, it can simply be fixed by dropping the use of >>:

  await expect(page.locator("fieldset:has-text('URL') input, button:has-text('Link Your Project')")).toHaveCount(2);

There’s currently not a great way of expressing OR semantics when using the Playwright chaining (>>) locator.

1reaction
rwollcommented, Sep 20, 2022

@GunjanSheth Thanks for your example! Looks like I forgot the awaits in my JS example leading me to the wrong conclusion. 🤦

Let me touch base with the team to sort out expected precedence of these operators so we can clarify in the docs as needed or apply fixes depending on the conclusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] Chaining a locator after a text selector that ... - GitHub
I can successfully achieve this until I then want to chain an additional locator from it e.g. using .nth() to ensure I get...
Read more >
Puppeteer documentation - DevDocs
The method runs document.querySelector within the page. If no element matches the selector, the return value resolves to null . Shortcut for page.mainFrame()....
Read more >
Changelog - Cypress Documentation
Fixed issue connecting to the cloud when a self-signed cert was in the cert chain. Fixes #24298. cy.origin() now supports more than 30...
Read more >
Cypress vs. Playwright: end-to-end testing showdown
However, bugs do have time. I'm not talking only about JavaScript, which we usually focus on, a CSS bug can also really screw...
Read more >
On Migrating from Cypress to Playwright - mtlynch.io
For basic testing, Cypress' semantics feel natural and familiar to someone who understands JavaScript. But when you stray off the beaten path, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found