Bug 224422

Summary: REGRESSION(r275262) [WebDriver] Make automation JS code aware of injected script headers
Product: WebKit Reporter: Lauro Moura <lmoura>
Component: WebDriverAssignee: Lauro Moura <lmoura>
Status: RESOLVED FIXED    
Severity: Normal CC: bburg, bugs-noreply, cgarcia, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=223961
Attachments:
Description Flags
Patch
none
Patch none

Description Lauro Moura 2021-04-11 21:08:15 PDT
https://trac.webkit.org/changeset/275262/webkit changed the injected scripts to have the "//# sourceURL=__InjectedScript_*.js" header.

This led to about 1.5k WebDriver tests to fail. Most (all?) of them with the following error:

E           JavascriptErrorException: javascript error (500): Unexpected keyword 'function'. Expected ')' to end a compound expression.                                                                    
                                                                                                                                                   
body       = {'using': 'css selector', 'value': 'input'}
err        = <JavascriptErrorException http_status=500>
method     = 'POST'
response   = <Response status=500 error=<JavascriptErrorException http_status=500>>
self       = <Session bb6344e1-8a15-487d-b05c-0cdc06fc85ce>
timeout    = None
url        = 'session/bb6344e1-8a15-487d-b05c-0cdc06fc85ce/element'

When evaluating the following expression in the test:

element = session.find.css("input", all=False)

The issue happens due to WebAutomationSessionProxy.js wrapping the executing code in "(async <CODE>)". With the header, this resulted in the following code:

```
(async //# __injectedScript...
 function (...) {...})
```

And the "Unexpected keyword" error appeared.

A candidate fix is to skip the header lines from the function code before wrapping it in async. Patch in the next comment.
Comment 1 Lauro Moura 2021-04-11 21:16:15 PDT
Created attachment 425718 [details]
Patch
Comment 2 Carlos Garcia Campos 2021-04-12 01:15:09 PDT
Comment on attachment 425718 [details]
Patch

Is that comment useful tor WebDriver scripts? Maybe we could add an option to make-js-file-arrays.py  not to include the directive and use that for WebDriver scripts?
Comment 3 Lauro Moura 2021-04-12 10:29:55 PDT
(In reply to Carlos Garcia Campos from comment #2)
> Comment on attachment 425718 [details]
> Patch
> 
> Is that comment useful tor WebDriver scripts? Maybe we could add an option
> to make-js-file-arrays.py  not to include the directive and use that for
> WebDriver scripts?

I think it's not directly used by the WebDriver code, but intended for the inspector UI, so there is the scenario of pausing a webdriver session to inspect the code in the browser. In this case, IIUC, the directive should be present in the source.
Comment 4 BJ Burg 2021-04-12 13:50:00 PDT
Comment on attachment 425718 [details]
Patch

the //# directive is a source map directive. It labels these evaluations, which is essential when debugging WebAutomationSessionProxy code via Web Inspector.

Perhaps you could search for the first non-comment line and wrap the remainder of the evaluation there, lifting any initial comments outside the async statement.
Comment 5 Lauro Moura 2021-04-12 14:13:27 PDT
Created attachment 425789 [details]
Patch
Comment 6 BJ Burg 2021-04-13 09:02:30 PDT
Comment on attachment 425789 [details]
Patch

r=me
Comment 7 EWS 2021-04-13 15:42:43 PDT
Committed r275915 (236479@main): <https://commits.webkit.org/236479@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 425789 [details].
Comment 8 Radar WebKit Bug Importer 2021-04-13 15:43:15 PDT
<rdar://problem/76614329>