ソースを参照

added alert methods to the modules

Gordon Child 14 年 前
コミット
d1578fb470
4 ファイル変更130 行追加3 行削除
  1. 16 0
      lib/Selenium/Remote/Commands.pm
  2. 84 0
      lib/Selenium/Remote/Driver.pm
  3. 25 0
      t/01-driver-live.t
  4. 5 3
      t/www/alerts.html

+ 16 - 0
lib/Selenium/Remote/Commands.pm

@@ -217,6 +217,22 @@ sub new {
                'method' => 'POST',
                'url' => "session/:sessionId/moveto"
         },
+        'getAlertText' => {
+               'method' => 'GET',
+               'url'    => 'session/:sessionId/alert_text'
+        },
+        'sendKeysToPrompt' => {
+               'method' => 'POST',
+               'url'    => 'session/:sessionId/alert_text'
+        },
+        'acceptAlert' => {
+               'method' => 'POST',
+               'url'    => 'session/:sessionId/accept_alert'
+        },
+        'dismissAlert' => {
+               'method' => 'POST',
+               'url'    => 'session/:sessionId/dismiss_alert'
+        },
         #'setVisible' => {
         #               'method' => 'POST',
         #               'url' => "session/:sessionId/visible"

+ 84 - 0
lib/Selenium/Remote/Driver.pm

@@ -277,6 +277,90 @@ sub status {
     return $self->_execute_command($res);
 }
 
+=head2 get_alert_text
+
+ Description:
+    Gets the text of the currently displayed JavaScript alert(), confirm()
+    or prompt() dialog.
+
+ Example
+    my $string = $driver->get_alert_text;
+
+=cut
+sub get_alert_text {
+  my ($self) = @_;
+  my $res = { 'command' => 'getAlertText' };
+  return $self->_execute_command($res);
+}
+
+=head2 send_keys_to_alert
+Synonymous with send_keys_to_prompt
+=cut
+
+sub send_keys_to_alert {
+  return shift->send_keys_to_prompt(@_);
+}
+
+=head2
+
+ Description:
+    Sends keystrokes to a JavaScript prompt() dialog.
+
+ Input:
+    {string} keys to send
+
+ Example:
+    $driver->send_keys_to_prompt('hello world');
+  or
+    ok($driver->get_alert_text eq 'Please Input your name','prompt appears');
+    $driver->send_keys_to_alert("Larry Wall");
+    $driver->accept_alert;
+
+=cut
+
+sub send_keys_to_prompt {
+  my ($self,$keys) = @_;
+  my $res = { 'command' => 'sendKeysToPrompt' };
+  my $params = { 'text' => $keys };
+  return $self->_execute_command($res,$params);
+}
+
+=head2
+
+ Description:
+    Accepts the currently displayed alert dialog.  Usually, this is
+    equivalent to clicking the 'OK' button in the dialog.
+
+ Example:
+    $driver->accept_alert;
+
+=cut
+
+sub accept_alert {
+  my ($self) = @_;
+  my $res = { 'command' => 'acceptAlert' };
+  return $self->_execute_command($res);
+}
+
+=head2
+
+ Description:
+    Dismisses the currently displayed alert dialog. For comfirm()
+    and prompt() dialogs, this is equivalent to clicking the
+    'Cancel' button. For alert() dialogs, this is equivalent to
+    clicking the 'OK' button.
+
+ Example:
+    $driver->dismiss_alert;
+
+=cut
+
+sub dismiss_alert {
+  my ($self) = @_;
+  my $res = { 'command' => 'dismissAlert' };
+  return $self->_execute_command($res);
+}
+
 =head2 mouse_move_to_location
 
  Description:

+ 25 - 0
t/01-driver-live.t

@@ -159,6 +159,31 @@ EXECUTE: {
         is($elem->get_attribute('id'),'multi','Async found proper element');
 }
 
+ALERT: {
+        $driver->get("$website/alerts.html");
+        $driver->find_element("alert",'id')->click;
+        is($driver->get_alert_text,'cheese','alert text match');
+        eval {$driver->dismiss_alert;};
+        ok(!$@,"dismissed alert");
+        $driver->find_element("prompt",'id')->click;
+        is($driver->get_alert_text,'Enter your name','prompt text match');
+        $driver->send_keys_to_prompt("Larry Wall");
+        eval {$driver->accept_alert;};
+        ok(!$@,"accepted prompt");
+        is($driver->get_alert_text,'Larry Wall','keys sent to prompt');
+        $driver->dismiss_alert;
+        $driver->find_element("confirm",'id')->click;
+        is($driver->get_alert_text,"Are you sure?",'confirm text match');
+        eval {$driver->dismiss_alert;};
+        ok(!$@,"dismissed confirm");
+        is($driver->get_alert_text,'false',"dismissed confirmed correct");
+        $driver->find_element("confirm",'id')->click;
+        eval {$driver->accept_alert;};
+        ok(!$@,"accepted confirm");
+        is($driver->get_alert_text,'true',"accept confirm correct");
+        $driver->accept_alert;
+}
+
 QUIT: {
         $ret = $driver->quit();
         ok((not defined $driver->{'session_id'}), 'Killed the remote session');

+ 5 - 3
t/www/alerts.html

@@ -10,8 +10,10 @@
 
 <p>This tests alerts: <a href="#" id="alert" onclick="alert('cheese');">click me</a></p>
 
-<p>This is a test of a confirm: <a href="simpleTest.html" id="confirm"
-                                   onclick="return confirm('Are you sure?');">test confirm</a></p>
+
+<p>This is a test of a prompt: <a href="#" id="prompt" onclick="alert(prompt('Enter your name'));">test prompt</a></p>
+
+<p>This is a test of a confirm: <a href="#" id="confirm" onclick="alert(confirm('Are you sure?'));">test confirm</a></p>
 
 </body>
-</html>
+</html>