File Coverage

blib/lib/Selenium/PageObject.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Selenium::PageObject;
2             {
3             $Selenium::PageObject::VERSION = '0.007';
4             }
5              
6 1     1   540368 use strict;
  1         5  
  1         26  
7 1     1   4 use warnings;
  1         2  
  1         21  
8              
9 1     1   4 use Carp;
  1         5  
  1         55  
10 1     1   5 use Scalar::Util qw(reftype blessed);
  1         2  
  1         41  
11 1     1   5 use Try::Tiny;
  1         1  
  1         61  
12              
13 1     1   712 use Selenium::Remote::WDKeys; #Needed to send things like tabs for navigation
  1         519  
  1         38  
14 1     1   597 use Selenium::Element;
  0            
  0            
15              
16             =head1 NAME
17              
18             Selenium::PageObject - Selenium's PageObject pattern in Perl. Now your module @ISA pageObject!
19              
20             =head1 SYNOPSIS
21              
22             This module is intended to be a base for PageObjects.
23             It abstracts a good deal of the things required to get/set various page inputs, and provides callback hooks so you can do special things like wait for JS.
24             It also is driver agnostic -- use WWW::Selenium or Selenium::Remote::Driver if you want.
25             I recommend Selenium::Remote::Driver due to it having a more complete feature set.
26              
27             Refer to the other module in this distribution L for the rest.
28              
29             =head1 CONSTRUCTOR
30              
31             =head2 new(driver,uri)
32              
33             Create a new PageObject using the provided driver, and navigate to the provided URI.
34             If you have subclassed your driver, you will need to override this method to not do it's strict driver class checks.
35              
36             B:
37              
38             I - The driver object
39              
40             I - the page this object should fiddle with (saved as $self->{'page'})
41              
42             B:
43              
44             new Selenium::PageObject object
45              
46             =cut
47              
48             sub new {
49             my ($class,$driver,$uri) = @_;
50             confess("Constructor must be called statically, not by an instance") if ref($class);
51             confess("Driver must be an instance of Selenium::Remote::Driver or WWW::Selenium") if !( grep {defined(blessed($driver)) && $_ eq blessed($driver)} qw(Selenium::Remote::Driver WWW::Selenium) );
52              
53             my $self = {
54             'drivertype' => blessed($driver) eq 'WWW::Selenium',
55             'driver' => $driver,
56             'page' => $uri
57             };
58              
59             $self->{'drivertype'} ? $driver->open($uri) : $driver->get($uri); #Get initial page based on what type of driver used
60              
61             bless $self, $class;
62             return $self;
63             }
64              
65             =head1 UTILITY
66              
67             =head2 driver
68              
69             The base selenium driver is available to you here.
70              
71             =cut
72              
73             sub driver {
74             my $self = shift;
75             return $self->{'driver'};
76             }
77              
78             =head1 GETTERS
79              
80             =head2 getElement(SELECTOR,SELECTORTYPE)
81              
82             Get the first element matching the provided selector and selector type. Refer to your driver's documentation as to valid types.
83              
84             B:
85              
86             I - Instructions for finding some element on the page
87              
88             I - Specifiation by which above instructions are parsed
89              
90             B:
91              
92             new Selenium::Element object
93              
94             =cut
95              
96             sub getElement {
97             my ($self,$selector,$selectortype) = @_;
98             my $element;
99             if ($self->{'drivertype'}) {
100             $element = $self->{'driver'}->is_element_present("$selectortype=$selector") ? "$selectortype=$selector" : undef;
101             } else {
102             try {
103             $element = $self->{'driver'}->find_element($selector,$selectortype);
104             } catch {
105             carp "# $_ \n";
106             $element = undef;
107             }
108             }
109             return Selenium::Element->new($element,$self->{'drivertype'} ? $self->{'driver'} : $self->{'drivertype'},[$selector,$selectortype]);
110             }
111              
112             =head2 getElements(SELECTOR,SELECTORTYPE)
113              
114             Get the elements matching the provided selector and selector type. Refer to your driver's documentation as to valid types.
115             WWW::Selenium is designed to work with single elements, so this method will fail when using it. Consider refining your selectors and looping instead.
116              
117             B:
118              
119             I - Instructions for finding some element on the page
120              
121             I - Specifiation by which above instructions are parsed
122              
123             B:
124              
125             array of new Selenium::Element objects
126              
127             =cut
128              
129             sub getElements {
130             my ($self,$selector,$selectortype) = @_;
131             my $elements = [];
132             confess ("WWW::Selenium is designed to work with single elements. Consider refining your selectors and looping instead.") if $self->{'drivertype'};
133             try {
134             @$elements = $self->{'driver'}->find_elements($selector,$selectortype);
135             };
136             return map {Selenium::Element->new($_,$self->{'drivertype'} ? $self->{'driver'} : $self->{'drivertype'},[$selector,$selectortype])} @$elements;
137             }
138              
139             =head1 GLOBAL EVENTS
140              
141             =head2 tab
142              
143             Send a tab to the page, to test tab navigation, or to de-focus the current element (useful for lose focus listeners, etc).
144              
145             =cut
146              
147             sub tab {
148             my $self = shift;
149             #9 is VK_TAB
150             $self->{'drivertype'} ? $self->driver->key_press_native(9) : $self->driver->send_keys_to_active_element(KEYS->{'tab'});
151             }
152              
153             1;
154              
155             __END__