catchpoint / WebPageTest.agent

Cross-platform WebPageTest agent
Other
210 stars 138 forks source link

Not able to set the executionContext using Id of iframe #623

Open yugal-compro opened 1 year ago

yugal-compro commented 1 year ago

Use Case:

I would like to change the execution context using Id of the iframe

I referred to the documentation https://docs.webpagetest.org/scripting/#setexecutioncontext for switching the execution context and was able to switch it using origin but was unable to switch the execution context using the ID of the cross-origin iframe.

Script:

navigate https://codepen.io/juno_okyo/pen/yOjaEZ
setExecutionContext ID=result
execAndWait document.getElementById('name-input').value = 'Yugal Vashishth'
execAndWait document.querySelector('#form > button').click()

Expected Action:

  1. Change the execution context using Id of the iframe
  2. Type 'Yugal Vashishth' inside input
  3. Click 'OK' button

Issue Faced: The input value is not updating to 'Yugal Vashishth' This script works fine when I change the execution context using the 'origin' of the iframe but It doesn't work when I use the 'ID' of the iframe.

Note: I am using ID instead of origin because I have to access two iframes having the same origin, so when I set the execution context using 'origin' it always returns the execution context of the first iframe but I would also like to access the second iframe.

pmeenan commented 1 year ago

Sorry for the confusion in the docs, the "ID" referred isn't the frame ID in the DOM, it's the dev tools ID for the execution context. If you look at the JSON result of a test it will have the list of all of the execution contexts with the ID's and origins.

I'm pretty sure the execution context's are just sequentially numbered but you'll want to verify the ID in the JSON

pmeenan commented 1 year ago

FWIW, for that pen, these are the execution context ID's and origins (at least when tested with desktop):

"execution_contexts": [
    {
        "id": 2,
        "origin": "https:\/\/cdpn.io",
        "name": ""
    },
    {
        "id": 1,
        "origin": "https:\/\/codepen.io",
        "name": ""
    },
    {
        "id": 3,
        "origin": "https:\/\/codepen.io",
        "name": ""
    },
    {
        "id": 5,
        "origin": "https:\/\/js.stripe.com",
        "name": ""
    },
    {
        "id": 6,
        "origin": "https:\/\/m.stripe.network",
        "name": ""
    }
],
pmeenan commented 1 year ago

It looks like the 2 stripe origins are actually different so you could probably use the named origin as well.

yugal-compro commented 1 year ago

Thanks for your response, I tried setting the execution context using the ID given in the JSON result still, this is not changing the execution context

aartajew commented 1 year ago

There is a type mismatch when setting context by id key.

You are comparing string with integer which results in false here: https://github.com/WPO-Foundation/wptagent/blob/eb86253f0e27463a9ccf2268d41f13e370f7a1bc/internal/devtools.py#L1250

Setting by origin works ok, see here: https://www.webpagetest.org/result/230629_AiDcS7_6GB/ Setting by id fails, see here: https://www.webpagetest.org/result/230629_AiDc1W_6GC/

Here are the contexts from JSON:

"execution_contexts":
[
    {
        "id": 2,
        "origin": "https://cdpn.io",
        "name": ""
    },
    {
        "id": 1,
        "origin": "https://codepen.io",
        "name": ""
    },
    {
        "id": 3,
        "origin": "https://codepen.io",
        "name": ""
    },
    {
        "id": 5,
        "origin": "https://js.stripe.com",
        "name": ""
    },
    {
        "id": 6,
        "origin": "https://m.stripe.network",
        "name": ""
    }
]

So for now setting context by id is impossible. Could you fix this please? Thx in advance 😄

aartajew commented 1 year ago

For a temporary workaround you can use jQuery for switching to another frame (same origin only):

execAndWait $("#content-iframe").contents().find("#Assessments_Navigation_Bar1 .section_0 .button")[0].click()
aartajew commented 1 year ago

Is the fix released yet?

It still works by origin: https://www.webpagetest.org/result/230724_AiDcZ6_BRQ/

But not yet with id:

pmeenan commented 1 year ago

Doesn't look like it. The prod branch hasn't been updated since the fix landed.