Skip to content
Paul-Clewell edited this page Nov 30, 2011 · 4 revisions

Handling Javascript popups in Watir and Selenium has always been a hassle. The approach I have taken with page-object is to intercept the call to the popup and cause it to not happen. At the same time I am providing all of the information to the user that they would wish to receive if they had access to the popup. Let's look at a few examples.

Alert

The PageObject module has a method named 'alert'. Here is how you can use it.

message = @page.alert do
  @page.button_that_causes_the_alert
end
message.should == "My Alert!"

This call demonstrates that we are passing a block to the alert method. That block is the code that causes the alert to occur. The alert will not popup but the message that was included in the popup is returned by the block.

Confirm

PageObject also has a confirm method that handles confirm popups. It works the same way as the alert method except it requires a boolean parameter which is the value returned to the browser when the confirm popup is called. Here's an example.

message = @page.confirm(true) do
  @page.button_causing_the_confirm
end

Prompt

Finally, PageObject has a prompt method that follows the same pattern. There are two difference. The first is that it accepts a parameter that is the value returned from the prompt. The second is that it returns a Hash with two keys - :message contains the message from the confirm popup and :default_value contains the default value if one was provided.

confirm_result = @page.prompt("Cheese") do
  @page.what_do_you_like_button
end
confirm_result[:message].should == "What do you like?"