[Bug]: The exposeFunction method of the Java version of PlayWright does not take effect in the browser console
Version
1.42.0
Steps to reproduce
- git clone https://github.com/luxinfeng/java_exercise.git
- exeucte org.luxinfeng.chrome.ChromeTest#main
- call the sha256 method in the newly launched browser console
Expected behavior
I'm using the method example given in the official documentation, link. When I call the sha256 method in the terminal of the browser launched by PlayWright, the sha256 function in Java is not executed. But when I use the nodeJs version of the exposeFunction function, I can indeed trigger the sha256 function in the browser console.
Actual behavior
In the Java environment, the function provided by exposeFunction cannot be called through the browser terminal launched by PlayWright.
Additional context
I added console printing logic to the official sample code to verify whether the code is actually executed.
Environment
System:
Windows11
JDK8
Chrome Browser
The reason for this behaviour is that the program execution has already finished and Java is busy after main() has executed and waits for resources to clear up. This means that incoming window.sha256 calls can't get processed by Java.
To fix this, you can wait for something at the end of main(). The most primitive wait page.waitForTimeout(100000); would work there, but we suggest using an actual event for e.g. text to appear etc. which makes your program more reliable.