playwright-community / playwright-go

Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
https://playwright-community.github.io/playwright-go/
MIT License
1.94k stars 144 forks source link

How does `Evaluate` work? #459

Open jonfriesen opened 1 month ago

jonfriesen commented 1 month ago

Hello,

I'm in the progress of migrating some tests to remove uses of the deprecated ElementHandles. Something I've noticed is the following code. Running Evaluate on a Locator times out (except for the rare occasion). Running an Evaluate on an ElementHandle executes immediately.

Can someone help me understand what's going on here and the proper way to call Evaluate on Locators? Sample code below.

// cell is a playwright.Locator
cellHandle, err := cell.ElementHandle()
if err != nil {
    return nil, fmt.Errorf("could not get cell handle: %w", err)
}

// runs immediately and succeeds
handleTagName, err := cellHandle.Evaluate(`e => e.tagName`)
if err != nil {
    return nil, fmt.Errorf("could not get tag: %w", err)
}

// times out
locatorTagName, err := cell.Evaluate(`e => e.tagName`, nil)
if err != nil {
    return nil, fmt.Errorf("could not get tag: %w", err)
}
canstand commented 1 month ago

The difference between the Locator and ElementHandle is that the latter points to a particular element, while Locator captures the logic of how to retrieve that element. (https://playwright.dev/docs/release-notes#-new-locators-api)

Maybe when calling locator.Evaluate, the locator cannot find the element that meets the corresponding logic?

jonfriesen commented 1 month ago

In my case, I'm calling cell.ElementHandle() and the cellHandle.Evaluate(...) functions right next to eachother, just like my example.

Given that cell is the Locator and has to resolve to the ElementHandle, I expected removing that step and calling Evaluate directly on the Locator would resolve the same way, does that sound correct? Or does the Locator cache the ElementHandle?

Thanks for the response :)

canstand commented 3 weeks ago

Sorry for the delay. I haven't dug into the details of the upstream implementation, but I don't think Locator caches the ElementHandle.