Skip to content

issue in _WD_ExecuteScript / __WD_EscapeString when comments // was used in JavaScript string #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mlipok opened this issue Jul 30, 2023 · 24 comments · Fixed by #489
Closed

Comments

@mlipok
Copy link
Contributor

mlipok commented Jul 30, 2023

Bug report

Describe the bug

$sData = StringRegExpReplace($sData, '[\v\t]', '')
from here:

au3WebDriver/wd_core.au3

Lines 1747 to 1749 in 42dda7d

Func __WD_EscapeString($sData, $iOption = 0)
If BitAND($iOption, $JSON_MLREFORMAT) Then
$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs

strips [\v] chars and when // comment is used entire $sJavaScript becames commented out.

How to reproduce

_Example()
Func _Example()
	Local $sJavaScript = _
			"// comment" & @CRLF & _
			"return document.readyState;" & @CRLF & _
			""
	_WD_DebugSwitch($_WD_DEBUG_Full)
	_WD_ExecuteScript($sSession, $sJavaScript)
	_WD_DebugSwitch()
EndFunc   ;==>_Example

Expected behavior

get result from: return document.readyState;

Screenshots

none

Additional context

https://www.autoitscript.com/forum/topic/208640-webdriver-udf-help-support-iv/?do=findComment&comment=1521863

System under test

not related

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

interesting.
Using:

_Example()
Func _Example()
	Local $sJavaScript = _
			"// comment" & @CRLF & _
			"return document.readyState;" & @CRLF & _
			""
	_WD_DebugSwitch($_WD_DEBUG_Full)
	_WD_ExecuteScript($sSession, $sJavaScript)
	_WD_DebugSwitch()
EndFunc   ;==>_Example

I get proper results:

__WD_Post: URL=HTTP://127.0.0.1:4444/session/e1192324-9090-4743-8d1c-c390f190cdda/execute/sync; Data={"script":"\/\/ comment\r\nreturn document.readyState;\r\n", "args":[]}
__WD_Post ==> Success [0] : HTTP status = 200 ResponseText={"value":"complete"}

the same result with:

_Example()
Func _Example()
	Local $sJavaScript = _
			"// comment" & @CR & _
			"return document.readyState;" & @CR & _
			""
	_WD_DebugSwitch($_WD_DEBUG_Full)
	_WD_ExecuteScript($sSession, $sJavaScript)
	_WD_DebugSwitch()
EndFunc   ;==>_Example

and with:

_Example()
Func _Example()
	Local $sJavaScript = _
			"// comment" & @LF & _
			"return document.readyState;" & @LF & _
			""
	_WD_DebugSwitch($_WD_DEBUG_Full)
	_WD_ExecuteScript($sSession, $sJavaScript)
	_WD_DebugSwitch()
EndFunc   ;==>_Example

@Danp2
Copy link
Owner

Danp2 commented Jul 30, 2023

I get proper results:

Not me. I got this --

     __WD_Post: URL=HTTP://127.0.0.1:4444/session/0cab983f-b61b-4acf-afa7-055cb4397c0a/execute/sync; Data={"script":"\/\/ commentreturn document.readyState;", "args":[]}
     __WD_Post ==> Success [0] : HTTP status = 200 ResponseText={"value":null}

Edit: Notice the difference in the Data portion of mine VS yours. Are you sure you are testing with the latest release?

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

I get proper results:

__WD_Post: URL=HTTP://127.0.0.1:4444/session/e1192324-9090-4743-8d1c-c390f190cdda/execute/sync; Data={"script":"\/\/ comment\r\nreturn document.readyState;\r\n", "args":[]}
__WD_Post ==> Success [0] : HTTP status = 200 ResponseText={"value":"complete"}

oops.. my mistake. I had little modified verison because I was testing this issue.

my results are:

_WD_DebugSwitch:  /  stack size: 2
__WD_Post: URL=HTTP://127.0.0.1:4444/session/758b3de6-2777-4a45-a73b-c4171a079a57/execute/sync; Data={"script":"\/\/ commentreturn document.readyState;", "args":[]}
__WD_Post ==> Success [0] : HTTP status = 200 ResponseText={"value":null}
_WD_ExecuteScript ==> Success [0]

the issue can be solved by:

Func __WD_EscapeString($sData, $iOption = 0)
	If BitAND($iOption, $JSON_MLREFORMAT) Then
;~ 		$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs ; original
		$sData = StringRegExpReplace($sData, '\t', '') ; Strip tabs ; modified

and results are:

_WD_DebugSwitch:  /  stack size: 2
__WD_Post: URL=HTTP://127.0.0.1:4444/session/cbaddcad-3adb-4f2e-91da-8fb50c008226/execute/sync; Data={"script":"\/\/ comment\nreturn document.readyState;\n", "args":[]}
__WD_Post ==> Success [0] : HTTP status = 200 ResponseText={"value":"complete"}
_WD_ExecuteScript ==> Success [0]

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

we should re-test:
#465

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

from: #465
REPRO:
Using file: UserTesting_EscapeString.au3 with following content:

ConsoleWrite("! Start: UserTesting_EscapeString.au3" & @CRLF)

ConsoleWrite("- Test 1:" & @CRLF)
_WD_Navigate($sSession, 'https://www.google.com')
_WD_LoadWait($sSession)
MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONWARNING, "Warning #" & @ScriptLineNumber, "If you see COOKIE accept panel close them before you will continue.")

ConsoleWrite("- Test 2:" & @CRLF)
; REMARK:  __SetVAR($IDX_VAR, $value) will set  $_VAR[$IDX_VAR] = $value
__SetVAR(0, _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//textarea[@name='q']"))

ConsoleWrite("- Test 3:" & @CRLF)
__SetVAR(1, ' " ! @ # $ % ^ & * ( ) - _ + = { } [ ] ; : \ | , . < > / ? ~ ' & " ' " & ' @CR' & @CR & ' @CRLF' & @CRLF & ' @LF' & @LF & ' @TAB' & @TAB & 'END')
_WD_ElementAction($sSession, $_VAR[0], "VALUE", $_VAR[1])
;~ _WD_SetElementValue($sSession, $_VAR[0], $_VAR[1], $_WD_OPTION_Advanced)

ConsoleWrite("! $_VAR[1] -" & @CRLF & $_VAR[1] & @CRLF)

ConsoleWrite("! __WD_EscapeString($_VAR[0]) -" & @CRLF & __WD_EscapeString($_VAR[1]) & @CRLF)
ConsoleWrite("! END: UserTesting_EscapeString.au3" & @CRLF)

together with current modification:

Func __WD_EscapeString($sData, $iOption = 0)
	If BitAND($iOption, $JSON_MLREFORMAT) Then
;~ 		$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs ; original
		$sData = StringRegExpReplace($sData, '\t', '') ; Strip tabs ; modified

I get:

_WD_FindElement ==> Success [0] : Parameters:   Strategy=xpath   Selector=//textarea[@name='q']   StartNodeID=Default   Multiple=Default   ShadowRoot=Default
- Setting $_VAR[0] = 9559b64a-277b-4171-b3fb-e1ace0755842
- Test 3:
- Setting $_VAR[1] =  " ! @ # $ % ^ & * ( ) - _ + = { } [ ] ; : \ | , . < > / ? ~  '  @CR
 @CRLF
 @LF
 @TAB	END
__WD_Post ==> Success [0] : HTTP status = 200
_WD_ElementAction ==> Success [0] : Parameters:   Command=VALUE   Option=<masked>
! $_VAR[1] -
 " ! @ # $ % ^ & * ( ) - _ + = { } [ ] ; : \ | , . < > / ? ~  '  @CR
 @CRLF
 @LF
 @TAB	END
! __WD_EscapeString($_VAR[0]) -
 \" ! @ # $ % ^ & * ( ) - _ + = { } [ ] ; : \\ | , . < > \/ ? ~  '  @CR\r @CRLF\r\n @LF\n @TAB\tEND
! END: UserTesting_EscapeString.au3

image

for me the following fix

Func __WD_EscapeString($sData, $iOption = 0)
	If BitAND($iOption, $JSON_MLREFORMAT) Then
;~ 		$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs ; original
		$sData = StringRegExpReplace($sData, '\t', '') ; Strip tabs ; modified

works fine.

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

DemoUpload from wd_demo.au3 also works fine.

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

@Danp2 If you agree with my testing route and results, I pass the solution of this problem to you (please make PR).

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

btw.
__WD_EscapeString tested also with:
#486 (comment)

results - works fine.

@Danp2
Copy link
Owner

Danp2 commented Jul 30, 2023

we should re-test: #465

Yes, I think it is important to understand why the original change was made before reverting so that we can hopefully avoid creating a different issue.

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

I think this was my mistake.

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

quick review and I think it starts here:
#465 (comment)

@mlipok
Copy link
Contributor Author

mlipok commented Jul 30, 2023

finall fix proposal:

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __WD_EscapeString
; Description ...: Escapes designated characters in string.
; Syntax ........: __WD_EscapeString($sData[, $iOption = 0])
; Parameters ....: $sData   - the string to be escaped
;                  $iOption - [optional] Any combination of $JSON_* constants. Default is 0.
; Author ........: Danp2, mLipok
; Modified ......:
; Remarks .......: $JSON_MLREFORMAT will strip tabs from a multiline string (ie. indentation) as they are not supported by WebDriver, use \t where it is needed
;                  See $JSON_* constants in json.au3 for other $iOption possibilities.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __WD_EscapeString($sData, $iOption = 0)
	If BitAND($iOption, $JSON_MLREFORMAT) Then
		$sData = StringRegExpReplace($sData, '\t', '') ; Strip tabs
		$iOption = BitXOR($iOption, $JSON_MLREFORMAT) ; Flip bit off
	EndIf

	$sData = Json_StringEncode($sData, $iOption) ; Escape JSON Strings
	Return SetError($_WD_ERROR_Success, 0, $sData)
EndFunc   ;==>__WD_EscapeString

@Danp2
Copy link
Owner

Danp2 commented Jul 30, 2023

An alternative is to drop the use of $JSON_MLREFORMAT in _WD_ExecuteScript, but still awaiting your clarification here.

mlipok referenced this issue Jul 31, 2023
* _WD_ElementSelectAction 'selectedOptions' group (#375)
* _WD_ElementSelectAction 'options' added group name (#374)
* _WD_ElementSelectAction 'selectAll' JS validation (#373)
* _WD_ElementSelectAction 'selectedLabels' JS refact (#376)
* _WD_ElementSelectAction + SINGLESELECT (#367)
* _ElementSelectAction : JS refactoring + case insensivity (#381)
* Return result from `selectAll`
* selectAll + $_WD_ERROR_NoMatch
@mlipok
Copy link
Contributor Author

mlipok commented Jul 31, 2023

take a look here:
5bb7a7a#r123141584

from this time my experience says to mee each time I use TABS in $sJavaScript they need to be stripped.

; StringReplace() removes all possible @TAB's because they are used only for indentation and are not needed in JSON string

@mlipok
Copy link
Contributor Author

mlipok commented Jul 31, 2023

I will do test today to clarify if TABS are allowed or not.

btw.
https://www.autoitscript.com/forum/topic/208640-webdriver-udf-help-support-iv/?do=findComment&comment=1521937

@Danp2
Copy link
Owner

Danp2 commented Jul 31, 2023

If your results match mine, then I would suggest that we drop the usage of $JSON_MLREFORMAT

@mlipok
Copy link
Contributor Author

mlipok commented Jul 31, 2023

So please do this test:

  1. run DemoSelectOptions from wd_demo.au3 for Chrome browser
  2. check results
  3. open wd_helper.au3
  4. change all , @TAB, '') to , '@TAB', '')
  5. skip If BitAND($iOption, $JSON_MLREFORMAT) Then
  6. run DemoSelectOptions from wd_demo.au3 for Chrome browser
  7. check results

this should explain why I said that TABS used directly in $sJavaScript are not supported by WebDriver

my results are:

! Error = 8 occurred on: DemoSelectOptions
! _WD_LastHTTPResult = 404
! _WD_LastHTTPResponse = {"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"xpath\",\"selector\":\"//*[@name=\"pets\"]\"}\n  (Session info: chrome=115.0.5790.110)","stacktrace":"Backtrace:\n\tGetHandleVerifier [0x0058A813+48355]\n\t(No symbol) [0x0051C4B1]\n\t(No symbol) [0x00425358]\n\t(No symbol) [0x004509A5]\n\t(No symbol) [0x00450B3B]\n\t(No symbol) [0x0047E232]\n\t(No symbol) [0x0046A784]\n\t(No symbol) [0x0047C922]\n\t(No symbol) [0x0046A536]\n\t(No symbol) [0x004482DC]\n\t(No symbol) [0x004493DD]\n\tGetHandleVerifier [0x007EAABD+2539405]\n\tGetHandleVerifier [0x0082A78F+2800735]\n\tGetHandleVerifier [0x0082456C+2775612]\n\tGetHandleVerifier [0x006151E0+616112]\n\t(No symbol) [0x00525F8C]\n\t(No symbol) [0x00522328]\n\t(No symbol) [0x0052240B]\n\t(No symbol) [0x00514FF7]\n\tBaseThreadInitThunk [0x767D7D59+25]\n\tRtlInitializeExceptionChain [0x77A5B79B+107]\n\tRtlClearBits [0x77A5B71F+191]\n"}}

@mlipok
Copy link
Contributor Author

mlipok commented Jul 31, 2023

last post edited (sorry for the confusion)

@mlipok
Copy link
Contributor Author

mlipok commented Jul 31, 2023

my results are:

! Error = 8 occurred on: DemoSelectOptions
! _WD_LastHTTPResult = 404
! _WD_LastHTTPResponse = {"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"xpath\",\"selector\":\"//*[@name=\"pets\"]\"}\n  (Session info: chrome=115.0.5790.110)","stacktrace":"Backtrace:\n\tGetHandleVerifier [0x0058A813+48355]\n\t(No symbol) [0x0051C4B1]\n\t(No symbol) [0x00425358]\n\t(No symbol) [0x004509A5]\n\t(No symbol) [0x00450B3B]\n\t(No symbol) [0x0047E232]\n\t(No symbol) [0x0046A784]\n\t(No symbol) [0x0047C922]\n\t(No symbol) [0x0046A536]\n\t(No symbol) [0x004482DC]\n\t(No symbol) [0x004493DD]\n\tGetHandleVerifier [0x007EAABD+2539405]\n\tGetHandleVerifier [0x0082A78F+2800735]\n\tGetHandleVerifier [0x0082456C+2775612]\n\tGetHandleVerifier [0x006151E0+616112]\n\t(No symbol) [0x00525F8C]\n\t(No symbol) [0x00522328]\n\t(No symbol) [0x0052240B]\n\t(No symbol) [0x00514FF7]\n\tBaseThreadInitThunk [0x767D7D59+25]\n\tRtlInitializeExceptionChain [0x77A5B79B+107]\n\tRtlClearBits [0x77A5B71F+191]\n"}}

oops.
Something went really wrong.
Wait a moment I need some more time to investigate this new issue.

@Danp2
Copy link
Owner

Danp2 commented Jul 31, 2023

I think the simplest way to perform this testing is to change the following line in _WD_ExecuteScript --

$sScript = __WD_EscapeString($sScript, $JSON_MLREFORMAT)

to

$sScript = __WD_EscapeString($sScript)

Then test all JS functionality to see if there are any negative effects.

@mlipok

This comment was marked as off-topic.

@mlipok

This comment was marked as off-topic.

@mlipok
Copy link
Contributor Author

mlipok commented Aug 1, 2023

I think the simplest way to perform this testing is to change the following line in _WD_ExecuteScript --

$sScript = __WD_EscapeString($sScript, $JSON_MLREFORMAT)

to

$sScript = __WD_EscapeString($sScript)

Then test all JS functionality to see if there are any negative effects.

After some more testing I think this should be fine.

I think that here:
81154ab

au3WebDriver/wd_core.au3

Lines 1690 to 1699 in 81154ab

Func __WD_EscapeString($sData, $iOption = 0)
If BitAND($iOption, $JSON_MLREFORMAT) Then
$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs
$iOption = BitXOR($iOption, $JSON_MLREFORMAT) ; Flip bit off
Endif
$sData = Json_StringEncode($sData, $iOption) ; Escape JSON Strings
Return SetError($_WD_ERROR_Success, 0, $sData)
EndFunc ;==>__WD_EscapeString

image

this following part was bad as it was no taking all cases:

Local $sRegEx = "([" & $_WD_ESCAPE_CHARS & "])"

and thus I wanted to add:

$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs

but you propose to use:

$sData = Json_StringEncode($sData, $iOption) ; Escape JSON Strings

your proposal was covering also my request about fixing \t so currently $JSON_MLREFORMAT should be treated as useless and can be romoved from wd_core.au3 especially from:

au3WebDriver/wd_core.au3

Lines 1691 to 1694 in 81154ab

If BitAND($iOption, $JSON_MLREFORMAT) Then
$sData = StringRegExpReplace($sData, '[\v\t]', '') ; Strip tabs and CR/LFs
$iOption = BitXOR($iOption, $JSON_MLREFORMAT) ; Flip bit off
Endif

@mlipok
Copy link
Contributor Author

mlipok commented Aug 1, 2023

If your results match mine, then I would suggest that we drop the usage of $JSON_MLREFORMAT

#489

@Danp2 Danp2 closed this as completed in #489 Aug 1, 2023
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 a pull request may close this issue.

2 participants