anmar7889 / chromiumembedded

Automatically exported from code.google.com/p/chromiumembedded
0 stars 1 forks source link

CEF3: Cannot accept/dismiss JS alerts in CEF3 apps with Selenium/ChromeDriver #1128

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Paste the following html code to a html file and save (say as 
c:/tryme.html). Rebuild the cef3 binary with the following change in 
client_handler.cpp: 'm_StartupURL = "file:///c:/tryme.html";' (line #109). 
Check that when you launch cefclient.exe, this html gets loaded and clicking 
'clickme' button shown a JS alert.

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
    alert("Hello World!");
}
</script>
</head>
<body>

<input type="button" onclick="myFunction()" value="Click me" />

</body>
</html>

2.  Execute this Selenium test (here browser refers to the cefclient.exe built 
in setp 1 above. You need to launch this exe instead of Chrome browser):

    def test_alert(self):
        self.browser.switch_to_default_content()
        self.browser.find_element_by_xpath("//input").click()
        time.sleep(2)
        alert = self.browser.switch_to_alert()  
        print alert.text
        alert.accept()        
        time.sleep(2)

What is the expected output? What do you see instead?
The alert should get accepted in CEF3 application, instead the following 
exception is thrown:  

(IMPORTANT INFO: The exception is thrown at alert.accept() statement. The 
statement 'print alert.text' prints the alert text correctly. Also, the same 
test works fine when run in Chrome browser. The exception occurs only while you 
try to accept/dismiss alerts in "CEF3 apps")

 alert.accept()
  File ".../python/lib/python2.7/site-packages/selenium-2.29.0-py2.7.egg/selenium/webdriver/common/alert.py", line 35, in accept
    self.driver.execute(Command.ACCEPT_ALERT)
  File ".../python/lib/python2.7/site-packages/selenium-2.29.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 160, in execute
    self.error_handler.check_response(response)
  File "...//python/lib/python2.7/site-packages/selenium-2.29.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 149, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: 'Message: u\'unknown error: unhandled inspector 
error: {"code":-32603,"message":"No JavaScript dialog to handle"}\\n  (Session 
info: chrome=content shell)\\n  (Driver info: 
chromedriver=2.4.226107,platform=Windows NT 6.1 SP1 x86) (WARNING: The server 
did not provide any stacktrace information)\\nCommand duration or timeout: 15 
milliseconds\\nBuild info: version: \\\'2.35.0\\\', revision: \\\'c916b9d\\\', 
time: \\\'2013-08-12 15:42:01\\\'\\nSystem info: os.name: \\\'Windows 7\\\', 
os.arch: \\\'x86\\\', os.version: \\\'6.1\\\', java.version: 
\\\'1.7.0_45\\\'\\nSession ID: 722bbb9f6cc9a8d78ce6f95c639e007d\\nDriver info: 
org.openqa.selenium.chrome.ChromeDriver\\nCapabilities [{platform=XP, 
acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, 
chrome={chromedriverVersion=2.4.226107}, rotatable=false, 
locationContextEnabled=true, version=content shell, cssSelectorsEnabled=true, 
databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, 
webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, 
takesScreenshot=true}]\' ; Stacktrace: Method newInstance0 threw an error in 
None \n-------------------- 

What version of the product are you using? On what operating system?
Selenium version: 2.29.0
OS: Win 7 32 bit
Browser: Google Chrome
Browser version:  30.0.1599.101 m
Chromedriver: chromedriver_win32_2.4
CEF3 version: cef_binary_3.1547.1412_windows32

Please provide any additional information below.
Tried with latest selenium version (2.37.0) as well but no luck.

Thanks,
-Amboo S.

Original issue reported on code.google.com by amboo.sr...@gmail.com on 1 Nov 2013 at 7:11

Attachments:

GoogleCodeExporter commented 9 years ago
We need to implement JavaScriptDialogManager::HandleJavaScriptDialog in our 
CefJavaScriptDialogManager for this feature of selenium to work.

Original comment by Daniel.S...@gmail.com on 3 Nov 2013 at 3:07

GoogleCodeExporter commented 9 years ago
What we are seeing is that all future calls to the web driver fail with an 
"There was an unexpected Alert! " error. Note that we have dismissed the alert 
by clicking the accept button with our testing tool (Test Complete). This is a 
huge problem since all our web driver tests fail as soon as a JavaScript alert 
comes up. Is there an ETA on this issue?

Original comment by jkeno...@connectwise.com on 15 Jan 2014 at 9:24

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hi!

After debugging the issue. Here is a little info about what is happening.
When using cef without a webdriver the alert/confirm/prompt window is created 
and given a windows callback function CefJavaScriptDialog::DialogProc. This way 
whenever the user gets an alert and clicks on accept, the callback is triggered.

This is what happens when using chromeDriver:
Instead of clicking the accept button and triggering a callback, 
ChromeDriver sends a message to cef telling what to do. Something like this:
{\"id\":42,\"method\":\"Page.handleJavaScriptDialog\",\"params\":{\"accept\":tru
e,\"promptText\":\" ## ACCEPT ME  ##   \"}}

This is telling cef to accept the prompt and to set the text to  ## ACCEPT ME  
##.This is the mechanism chromedriver uses with cef to simulate a click on an 
alert. This was not working because 
JavaScriptDialogManager::HandleJavaScriptDialog was not properly implemented. 

To see this message you can put a breakpoint in 
DevToolsHttpHandlerImpl::OnWebSocketMessage.

This message triggers JavaScriptDialogManager::HandleJavaScriptDialog in cef.
Since CefJavaScriptDialogManager inherits from JavaScriptDialogManager,
we only have to implement HandleJavaScriptDialog by imitating the behaviour
of the windows callback.

I will post a patch soon.

Micael

Original comment by micael.l...@gmail.com on 14 Sep 2014 at 2:30

GoogleCodeExporter commented 9 years ago
Hi all,

Here is the patch.
Download the files, build libcef.dll
and have fun.

Please try it and post your comments.

Micael

Original comment by micael.l...@gmail.com on 15 Sep 2014 at 11:16

Attachments:

GoogleCodeExporter commented 9 years ago
Forgot to say: 
The location of these files is: src\cef\libcef\browser

Original comment by micael.l...@gmail.com on 15 Sep 2014 at 11:17

GoogleCodeExporter commented 9 years ago
CEF is transitioning from Google Code to Bitbucket project hosting. If you 
would like to continue receiving notifications on this issue please add 
yourself as a Watcher at the new location: 
https://bitbucket.org/chromiumembedded/cef/issue/1128

Original comment by magreenb...@gmail.com on 14 Mar 2015 at 3:28