Question

I am implementing a project with Selenium 2, which currently doesn't support confirm dialog boxes very well.

There is a workaround to this limitation, you just overwrite the window.confirm to return the value which you need to return for the specific test case.

The following strings can be set and then executed:

public static final String CONFIRM_DIALOG_BOX = 
    "window.confirm = function(msg) { return true; }";

public static final String CANCEL_DIALOG_BOX = 
    "window.confirm = function(msg) { return false; }";

This seems pretty straightforward as a template method, however I have multiple test cases on the same page object where I need to confirm / deny after interacting with the page. Thus having a single method to do those all of these tests at once doesn't work.

Injecting a command to run into the test method could make sense, but my end goal here is to allow our less technical staff to create tests by writing some strings into XML and then executing it with Spring Expression Language; this removes some of the "easiness" out of writing the tests.

Major caveat is that this test suite is actually an application due to requirements and not a set of test cases which are individually run. It would be much easier if they were small test cases as I could just extends an abstract test case and utilize the same setup and teardown routines.

What I'm ultimately looking for is something along this template method, but I need to be able to support multiple test cases on a single page object.

public final void executeTest(boolean confirmDialogBoxResponse) {

    // store normal functionality and allow us to return 
    // window.confirm to normal state upon completion of test.
    prepare(confirmDialogBoxResponse);

    testMethod(); // What about params to these methods?

    /* Adding an interface to the method would be nice, but 
     * would make things a bit more cumbersome for our
     * less technical staff, which would allow me to replace
     * the above with this:
     *
     *    executeTest(TestCommand command, confirmDialogResponse);
     *         command.execute(); // Still have params issue
     */

    // restore window.confirm to normal state -- if we cancel we stay 
    // on same page, and need to restore things as they were, the majority
    // of our confirm cases take you to new pages whereby window.confirm 
    // will be restored for us
    if (!confirmDialogResponse) {
        restore();
    }
}
Was it helpful?

Solution

Have you looked at the Selenium Issue #27 recently (comment#46)? It has some patches attached that provides support for alert and confirm?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top