Closing a window.confirm in protractor with phanto

2019-06-03 04:36发布

问题:

I'm writing E2E tests with Protractor for my AngularJS app.

At some point the browser will encounter a window.confirm.

When using Chrome as the test browser, the following code works fine :

var ptor = protractor.getInstance();
ptor.switchTo().alert().accept();

But on PhantomJS it raises the following error :

UnknownError: Invalid Command Method
==== async task ====
WebDriver.switchTo().alert()
    at tests/E2E/spec/search.spec.js:73:33
==== async task ====
Asynchronous test function: it()
Error
    at null.<anonymous> (tests/E2E/spec/search.spec.js:63:5)
    at Object.<anonymous> (tests/E2E/spec/search.spec.js:6:1)

Any clue on how to handle it with PhantomJS ?

回答1:

Since there is no support yet for switchTo().alert() for PhantomJS/GhostDriver

I went for the following solution : mocking window.confirm as following :

beforeEach(function() {
    // bypassing PhantomJS 1.9.7/GhostDriver window.confirm (or alert) bug.
    // as WebDriver's switchTo().alert() is not implemented yet.
    browser.executeScript('window.confirm = function() {return true;}')
});

NB: I used jasmine for my Protractor tests, therefore I needed to put it in the beforeEach, else it would have no effect.