Skip to content

Exposed guessLocator via element function on Webpage class#219

Open
jack-sandeman wants to merge 1 commit into
pestphp:4.xfrom
jack-sandeman:expose-element
Open

Exposed guessLocator via element function on Webpage class#219
jack-sandeman wants to merge 1 commit into
pestphp:4.xfrom
jack-sandeman:expose-element

Conversation

@jack-sandeman
Copy link
Copy Markdown

This is more of a question than an improvement. I was wondering why elements aren't exposed to the end user to test directly. To give some context, I ran into a bit of an issue testing where I needed to check the order of elements when using sorting on the page and I couldn't find any way to do it with the current assertion function. This requires the user to use the script function to retrieve element content using querySelector

EG

$page->click('@sort-button');

$names = $page->script('
    const names = document.querySelectorAll("[data-test=\'names\']")
    Array
        .from(names)
        .map(name => name.textContent);
');  

 expect($names)->toBe(['Name 1', 'Name 2']);

Resorting to the query selector means that all of playwrights locator magic gets lost! I could be missing something but it seems like it would be useful to be able to access the locator returned from guessLocator directly instead.

EG

$page->click('@sort-button');

$names = $page->element('@names')->all()
$names = array_map(fn ($name_el) => $name_el->textContent())

 expect($names)->toBe(['Name 1', 'Name 2']);

Let me know if I'm missing something, or if another approach can be taken.

@jack-sandeman
Copy link
Copy Markdown
Author

I've found out that you can access the playwright selectors by using ->page(). I couldn't see any mention of this on the documentation, would it be worth updating?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant