playwright-community/playwright-go

How does `Evaluate` work?

jonfriesen opened this issue · 3 comments

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)
}

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?

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 :)

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