Skip to content

[BUG] Locator for 'role=img' sees SVGs with 'role=presentation' as images #26809

Closed

Description

System info

  • Playwright Version: 1.37.1
  • Operating System: macOS 13.3
  • Browser: Chromium (but probably affects others)
  • Other info:

Locator

Source code

Test file (self-contained)

const { chromium, test, expect } = require( '@playwright/test' );

test( 'image locator', async () => {
	const browser = await chromium.launch();
	const context = await browser.newContext();
	const page = await context.newPage();

	await page.setContent( `
	<div data-testid="images">
		<img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." />
		<svg class="components-spinner aecf-ce-a-fe-acea-1yxc2ud ea4tfvq2" viewBox="0 0 100 100" width="16" height="16" xmlns="http://www.w3.org/2000/svg" role="presentation" focusable="false"><circle cx="50" cy="50" r="50" vector-effect="non-scaling-stroke" class="aecf-ce-a-fe-acea-5zbsse ea4tfvq1"></circle><path d="m 50 0 a 50 50 0 0 1 50 50" vector-effect="non-scaling-stroke" class="aecf-ce-a-fe-acea-1r60s1s ea4tfvq0"></path></svg>
	</div>
` );

	const image = page.getByTestId( 'images' ).getByRole( 'img' );

	await expect( image ).toHaveAttribute( 'alt', 'Code is Poetry.' );
} );

Steps

  • Run the test
  • Test fails with Error: strict mode violation: getByTestId('images').getByRole('img') resolved to 2 elements

Expected

The test should only locate elements with role images.

Actual

The test incorrectly includes SVG in the located elements list.

This was working in Playwright v1.32.x.

Activity

  1. amankagithub commented on Apr 11, 2024

    @amankagithub
    Contributor

    I am facing the same issue , did we deploy any fix for this ?
    PW version 1.41.1

  2. added a commit that references this issue on Apr 18, 2024
    103ec90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions