Permalink
Browse files

Control JavaScript confirmation dialogs from Ruby

Adds #alert_messages, #confirm_messages, #prompt_messages,
 #accept_js_confirms!, #dismiss_js_confirms, #accept_js_prompts!,
 #dismiss_js_prompts, and #js_prompt_input= .
  • Loading branch information...
1 parent a4eb401 commit e677cb9c8e460ec7ca1611f397059740590d35ec @danivovich danivovich committed with jferris Jul 10, 2012
View
@@ -77,6 +77,20 @@ capybara-webkit supports a few methods that are not part of the standard capybar
page.driver.error_messages
=> {:source=>"http://example.com", :line_number=>1, :message=>"SyntaxError: Parse error"}
+**alert_messages, confirm_messages, prompt_messages**: returns arrays of Javascript dialog messages for each dialog type
+
+ # In Javascript:
+ alert("HI");
+ confirm("Ok?");
+ prompt("Number?", "42");
+ # In Ruby:
+ page.driver.alert_messages
+ => ["Hi"]
+ page.driver.confirm_messages
+ => ["Ok?"]
+ page.driver.prompt_messages
+ => ["Number?"]
+
**resize_window**: change the viewport size to the given width and height
page.driver.resize_window(500, 300)
@@ -92,6 +106,69 @@ capybara-webkit supports a few methods that are not part of the standard capybar
page.driver.cookies["alpha"]
=> "abc"
+**accept_js_confirms!**: accept any Javascript confirm that is triggered by the page's Javascript
+
+ # In Javascript:
+ if (confirm("Ok?"))
+ console.log("Hi");
+ else
+ console.log("Bye");
+ # In Ruby:
+ page.driver.accept_js_confirms!
+ visit "/"
+ page.driver.console_messages.first[:message]
+ => "Hi"
+
+**dismiss_js_confirms!**: dismiss any Javascript confirm that is triggered by the page's Javascript
+
+ # In Javascript:
+ if (confirm("Ok?"))
+ console.log("Hi");
+ else
+ console.log("Bye");
+ # In Ruby:
+ page.driver.dismiss_js_confirms!
+ visit "/"
+ page.driver.console_messages.first[:message]
+ => "Bye"
+
+**accept_js_prompts!**: accept any Javascript prompt that is triggered by the page's Javascript
+
+ # In Javascript:
+ var a = prompt("Number?", "0")
+ console.log(a);
+ # In Ruby:
+ page.driver.accept_js_prompts!
+ visit "/"
+ page.driver.console_messages.first[:message]
+ => "0"
+
+**dismiss_js_prompts!**: dismiss any Javascript prompt that is triggered by the page's Javascript
+
+ # In Javascript:
+ var a = prompt("Number?", "0")
+ if (a != null)
+ console.log(a);
+ else
+ console.log("you said no"));
+ # In Ruby:
+ page.driver.dismiss_js_prompts!
+ visit "/"
+ page.driver.console_messages.first[:message]
+ => "you said no"
+
+**js_prompt_input=(value)**: set the text to use if a Javascript prompt is encountered and accepted
+
+ # In Javascript:
+ var a = prompt("Number?", "0")
+ console.log(a);
+ # In Ruby:
+ page.driver.js_prompt_input = "42"
+ page.driver.accept_js_prompts!
+ visit "/"
+ page.driver.console_messages.first[:message]
+ => "42"
+
Contributing
------------
@@ -55,6 +55,18 @@ def error_messages
end
end
+ def alert_messages
+ command("JavascriptAlertMessages").split("\n")
+ end
+
+ def confirm_messages
+ command("JavascriptConfirmMessages").split("\n")
+ end
+
+ def prompt_messages
+ command("JavascriptPromptMessages").split("\n")
+ end
+
def response_headers
Hash[command("Headers").split("\n").map { |header| header.split(": ") }]
end
@@ -105,6 +117,30 @@ def get_window_handle
alias_method :window_handle, :get_window_handle
+ def accept_js_confirms
+ command("SetConfirmAction", "Yes")
+ end
+
+ def reject_js_confirms
+ command("SetConfirmAction", "No")
+ end
+
+ def accept_js_prompts
+ command("SetPromptAction", "Yes")
+ end
+
+ def reject_js_prompts
+ command("SetPromptAction", "No")
+ end
+
+ def set_prompt_text_to(string)
+ command("SetPromptText", string)
+ end
+
+ def clear_prompt_text
+ command("ClearPromptText")
+ end
+
def command(name, *args)
@connection.puts name
@connection.puts args.size
@@ -68,6 +68,18 @@ def error_messages
browser.error_messages
end
+ def alert_messages
+ browser.alert_messages
+ end
+
+ def confirm_messages
+ browser.confirm_messages
+ end
+
+ def prompt_messages
+ browser.prompt_messages
+ end
+
def response_headers
browser.response_headers
end
@@ -107,6 +119,30 @@ def window_handle
browser.get_window_handle
end
+ def accept_js_confirms!
+ browser.accept_js_confirms
+ end
+
+ def dismiss_js_confirms!
+ browser.reject_js_confirms
+ end
+
+ def accept_js_prompts!
+ browser.accept_js_prompts
+ end
+
+ def dismiss_js_prompts!
+ browser.reject_js_prompts
+ end
+
+ def js_prompt_input=(value)
+ if value.nil?
+ browser.clear_prompt_text
+ else
+ browser.set_prompt_text_to(value)
+ end
+ end
+
def wait?
true
end
View
@@ -403,6 +403,180 @@ def in_iframe_request?
driver.error_messages.length.should eq 1
end
+ it "empties the array when reset" do
+ driver.reset!
+ driver.console_messages.should be_empty
+ end
+
+ end
+
+ context "javascript dialog interaction" do
+ context "on an alert app" do
+ let(:driver) do
+ driver_for_html(<<-HTML)
+ <html>
+ <head>
+ </head>
+ <body>
+ <script type="text/javascript">
+ alert("Alert Text Goes Here");
+ </script>
+ </body>
+ </html>
+ HTML
+ end
+
+ before { driver.visit("/") }
+
+ it "should let me read my alert messages" do
+ driver.alert_messages.first.should == "Alert Text Goes Here"
+ end
+
+ it "empties the array when reset" do
+ driver.reset!
+ driver.alert_messages.should be_empty
+ end
+ end
+
+ context "on a confirm app" do
+ let(:driver) do
+ driver_for_html(<<-HTML)
+ <html>
+ <head>
+ </head>
+ <body>
+ <script type="text/javascript">
+ function test_dialog() {
+ if(confirm("Yes?"))
+ console.log("hello");
+ else
+ console.log("goodbye");
+ }
+ </script>
+ <input type="button" onclick="test_dialog()" name="test"/>
+ </body>
+ </html>
+ HTML
+ end
+
+ before { driver.visit("/") }
+
+ it "should default to accept the confirm" do
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "hello"
+ end
+
+ it "can dismiss the confirm" do
+ driver.dismiss_js_confirms!
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "goodbye"
+ end
+
+ it "can accept the confirm explicitly" do
+ driver.dismiss_js_confirms!
+ driver.accept_js_confirms!
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "hello"
+ end
+
+ it "should collect the javsacript confirm dialog contents" do
+ driver.find("//input").first.click
+ driver.confirm_messages.first.should == "Yes?"
+ end
+
+ it "empties the array when reset" do
+ driver.find("//input").first.click
+ driver.reset!
+ driver.confirm_messages.should be_empty
+ end
+
+ it "resets to the default of accepting confirms" do
+ driver.dismiss_js_confirms!
+ driver.reset!
+ driver.visit("/")
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "hello"
+ end
+ end
+
+ context "on a prompt app" do
+ let(:driver) do
+ driver_for_html(<<-HTML)
+ <html>
+ <head>
+ </head>
+ <body>
+ <script type="text/javascript">
+ function test_dialog() {
+ var response = prompt("Your name?", "John Smith");
+ if(response != null)
+ console.log("hello " + response);
+ else
+ console.log("goodbye");
+ }
+ </script>
+ <input type="button" onclick="test_dialog()" name="test"/>
+ </body>
+ </html>
+ HTML
+ end
+
+ before { driver.visit("/") }
+
+ it "should default to dismiss the prompt" do
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "goodbye"
+ end
+
+ it "can accept the prompt without providing text" do
+ driver.accept_js_prompts!
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "hello John Smith"
+ end
+
+ it "can accept the prompt with input" do
+ driver.js_prompt_input = "Capy"
+ driver.accept_js_prompts!
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "hello Capy"
+ end
+
+ it "can return to dismiss the prompt after accepting prompts" do
+ driver.accept_js_prompts!
+ driver.dismiss_js_prompts!
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "goodbye"
+ end
+
+ it "should let me remove the prompt input text" do
+ driver.js_prompt_input = "Capy"
+ driver.accept_js_prompts!
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "hello Capy"
+ driver.js_prompt_input = nil
+ driver.find("//input").first.click
+ driver.console_messages.last[:message].should == "hello John Smith"
+ end
+
+ it "should collect the javsacript prompt dialog contents" do
+ driver.find("//input").first.click
+ driver.prompt_messages.first.should == "Your name?"
+ end
+
+ it "empties the array when reset" do
+ driver.find("//input").first.click
+ driver.reset!
+ driver.prompt_messages.should be_empty
+ end
+
+ it "returns the prompt action to dismiss on reset" do
+ driver.accept_js_prompts!
+ driver.reset!
+ driver.visit("/")
+ driver.find("//input").first.click
+ driver.console_messages.first[:message].should == "goodbye"
+ end
+ end
end
context "form app" do
View
@@ -0,0 +1,11 @@
+#include "ClearPromptText.h"
+#include "WebPage.h"
+#include "WebPageManager.h"
+
+ClearPromptText::ClearPromptText(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {}
+
+void ClearPromptText::start()
+{
+ page()->setPromptText(QString());
+ emit finished(new Response(true));
+}
View
@@ -0,0 +1,9 @@
+#include "SocketCommand.h"
+
+class ClearPromptText : public SocketCommand {
+ Q_OBJECT;
+
+ public:
+ ClearPromptText(WebPageManager *manager, QStringList &arguments, QObject *parent = 0);
+ virtual void start();
+};
Oops, something went wrong.

0 comments on commit e677cb9

Please sign in to comment.