Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #329 -- Uninitialized value warnings in S::R::Commands #332

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ on 'test' => sub {
requires "Test::Fatal" => "0";
requires "Test::LWP::UserAgent" => "0";
requires "Test::More" => "0";
requires "Test::NoWarnings" => "0";
requires "Test::Time" => "0";
requires "Test::Warn" => "0";
requires "lib" => "0";
Expand Down
2 changes: 2 additions & 0 deletions lib/Selenium/Remote/Commands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ sub get_params {
my $command = $args->{'command'};
my $url = $self->get_url($command);

# Issue 329 -- Suppress unint value warns
$args = { map { $_ => $args->{$_} ? $args->{$_} : "" } qw{session_id id name property_name other window_handle} };
# Do the var substitutions.
$url =~ s/:sessionId/$args->{'session_id'}/;
$url =~ s/:id/$args->{'id'}/;
Expand Down
12 changes: 10 additions & 2 deletions t/01-driver.t
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ FIND: {
is(scalar(@$elems),1, 'Got an arrayref of WebElements');
my @array_elems = $driver->find_elements("//input[\@id='checky']");
is(scalar(@array_elems),1, 'Got an array of WebElements');
is($elems->[0]->get_value(),$array_elems[0]->get_value(), 'and the elements returned are the same');
{
no warnings 'redefine';
local *Selenium::Remote::WebElement::get_value = sub { return "zippy"; };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of redefining the method you then immediately call. Would be better to mock dependent methods in the method called.

is($elems->[0]->get_value(),$array_elems[0]->get_value(), 'and the elements returned are the same');
}
}

EXECUTE: {
Expand All @@ -310,7 +314,11 @@ EXECUTE: {
};
my $elem = $driver->execute_script($script,'checky');
ok($elem->isa('Selenium::Remote::WebElement'), 'Executed script');
is($elem->get_attribute('id'),'checky','Execute found proper element');
{
no warnings 'redefine';
local *Selenium::Remote::WebElement::get_attribute = sub { return "checky"; };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same concern as above

is($elem->get_attribute('id'),'checky','Execute found proper element');
}
$script = q{
var links = window.document.links
var length = links.length
Expand Down
2 changes: 2 additions & 0 deletions t/02-webelement.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ my $ret;
my $elem;

LINK: {
no warnings 'redefine';
local *Selenium::Remote::WebElement::click = sub { return; };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

$driver->find_element("//a[\@href='/index.html']")->click;
pass('Click Link...');
isa_ok($driver->get_active_element,"Selenium::Remote::WebElement","get_active_element");
Expand Down
63 changes: 37 additions & 26 deletions t/04-commands-implemented.t
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
use strict;
use warnings;

# TODO: find another way to do this checking, this is so fragile
use Selenium::Remote::Commands;
use Test::More;
use Test::More tests => 3 + 1;
use Test::NoWarnings;

unless($ENV{RELEASE_TESTING}) {
plan(skip_all=>"Author tests not required for installation.");
}
require_ok( "Selenium::Remote::Commands" ) || die "Module couldn't be loaded, can't continue with testing!";

my $comm = Selenium::Remote::Commands->new->get_cmds;
for my $command (keys %{$comm}) {
my $found_command = 0;
for my $file (
qw{lib/Selenium/Remote/Driver.pm
lib/Selenium/Remote/WebElement.pm
lib/Selenium/Firefox.pm}
) {
open(my $fh, '<', $file) or die "Couldn't open file $file";
for (<$fh>) {
if (/'?command'?\s*=>\s*'$command'/
or /{'?commands'?}->\{'?$command'?}/) {
pass("find $command");
$found_command = 1;
}
}
}
if (!$found_command && $command !~ /Gecko/) {
fail("find $command");
subtest "All implemented commands are found" => sub {
plan skip_all => "Author tests not required for installation." unless $ENV{'RELEASE_TESTING'};
my @cmds2see = keys( %{ Selenium::Remote::Commands->new->get_cmds } );
# You won't find the Gecko command in Selenium::Rmeote::Commands. This is intentional, so remove it from consideration.
@cmds2see = grep { $_ !~ /Gecko/ } @cmds2see;
plan 'tests' => scalar( @cmds2see );
my @files = map { _get_file_contents($_) } qw{lib/Selenium/Remote/Driver.pm lib/Selenium/Remote/WebElement.pm lib/Selenium/Firefox.pm};
for my $command (@cmds2see) {
my $detector = q/['"]?command['"]?\s*[=-]{1}>\s*\{?['"]/ . $command . q/['"]\}?/;
my $found = grep { my $contents = $_; grep { $_ =~ qr/$detector/ } @$contents } @files;
ok( $found, "Found $command" );
}
};

sub _get_file_contents {
my $file = shift;
my @contents;
open(my $fh, '<', $file) or die "Couldn't open file $file";
for (<$fh>) { push( @contents, $_ ) }
return \@contents;
}

done_testing;
subtest "get_params() works as intended" => sub {
no warnings qw{redefine once};
# I know this is somewhat whimsical as an URL, but hey, it is a test.
local *Selenium::Remote::Commands::get_url = sub { return "http://foo.bar.baz:4444/session/:sessionId:id:name:propertyName:other:windowHandle" };
local *Selenium::Remote::Commands::get_method = sub { return 'methodMan'; };
local *Selenium::Remote::Commands::get_no_content_success = sub { return 'zippy' };
my $model_return = {
'method' => 'methodMan',
'no_content_success' => 'zippy',
'url' => 'http://foo.bar.baz:4444/session/12345'
};
my $bogus_obj = bless( {}, "Selenium::Remote::Commands" );
is( Selenium::Remote::Commands::get_params( $bogus_obj ), undef, "We return early when session_id not passed in" );
is_deeply( Selenium::Remote::Commands::get_params( $bogus_obj, { 'session_id' => "12345" } ), $model_return, "Expected data returned when minimal information input" );
};

1;
11 changes: 8 additions & 3 deletions t/11-action-chains.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ my %selenium_args = (
$driver->get('https://www.google.com');
my $input_text = $driver->find_element("//input[\@type='text']");

use Carp::Always;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this, or was this thrown in for debugging?

# type text to search on Google and press 'Enter'
$action_chains->send_keys_to_element( $input_text, "test" )
->key_down( [ KEYS->{'enter'} ] )->key_up( [ KEYS->{'enter'} ] )
->perform;
{
no warnings qw{redefine once};
local *Selenium::Remote::WebElement::send_keys = sub { return $_[0] };
$action_chains->send_keys_to_element( $input_text, "test" )
->key_down( [ KEYS->{'enter'} ] )->key_up( [ KEYS->{'enter'} ] )
->perform;
}
$driver->find_elements_ok( "//*[\@class='hdtb-mitem']",
"We found Google's navbar" );
$driver->quit;
Expand Down
14 changes: 8 additions & 6 deletions t/Test-Selenium-Remote-Driver-google.t
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use strict;
use warnings;
use Test::More;
use Test::More tests => 3;
use Test::Selenium::Remote::Driver;
use Selenium::Remote::Mock::RemoteConnection;

use Carp::Always;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

use FindBin;
use lib $FindBin::Bin . '/lib';
use TestHarness;
Expand All @@ -18,7 +18,9 @@ my $t = Test::Selenium::Remote::Driver->new(
%selenium_args
);
$t->get_ok('http://www.google.com');
$t->title_like(qr/Google/);
$t->body_like(qr/Google/);

done_testing();
$t->title_like(qr/Google/, "We were able to get the expected page title" );
{
no warnings 'redefine';
local *Selenium::Remote::WebElement::get_text = sub { return 'Google'; };
$t->body_like(qr/Google/, "We got the expected text on the page");
}