Skip to content

Commit

Permalink
Add missing cookie logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JRaspass committed Aug 30, 2015
1 parent 7080842 commit 3692f1a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.003 ???
- Add support for ChromDriver.
- Document how to use different backends.
- Add cookie getter/setter.
- Rename delete_cookie to cookie_delete.

0.002 2015-07-05
- $drv->() caches the closure and is now twice as fast.
Expand Down
33 changes: 25 additions & 8 deletions lib/WebDriver/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ sub base_url {
$_[0][2];
}

sub cookie {
my ( $self, $name, $value, @args ) = @_;

if ( @_ == 2 ) {
my $cookie = $self->_req( GET => "/cookie/$name" )->{value};

# FIXME PhantomJS returns all cookies.
return ref $cookie eq 'ARRAY'
? grep $_->{name} eq $name, @$cookie : $cookie;
}

$self->_req( POST => '/cookie',
{ cookie => { name => $name, value => $value, @args } } );

$self;
}

sub cookie_delete {
my $self = shift;

$self->_req( DELETE => '/cookie' . ( @_ ? '/' . shift // '' : '' ) );

$self;
}

sub cookies {
+{
map { $_->{name} => $_ }
Expand Down Expand Up @@ -187,14 +212,6 @@ sub find {
: bless [ $self, @ids ], 'WebDriver::Tiny::Elements';
}

sub delete_cookie {
my $self = shift;

$self->_req( DELETE => '/cookie' . ( @_ ? '/' . shift // '' : '' ) );

$self;
}

sub execute {
my ( $self, $script, @args ) = @_;

Expand Down
28 changes: 23 additions & 5 deletions lib/WebDriver/Tiny.pod
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,35 @@ Maximum number of attempts to try and find a match.

=head2 Cookies

=head3 cookies
=head3 cookie

my $cookies = $drv->cookies;
# Get cookie "foo".
my $cookie = $drv->cookie('foo');

# Set cookie "foo".
$drv->cookie( foo => 'value' );

# Or
$drv->cookie(
foo => 'value',
domain => 'example.com',
expires => 123,
httponly => 0,
secure => 1,
);

=head3 delete_cookie
=head3 cookie_delete

# Delete all cookies.
$drv->delete_cookie;
$drv->cookie_delete;

# Delete cookie "foo".
$drv->delete_cookie('foo');
$drv->cookie_delete('foo');

=head3 cookies

# Get a hashref of all cookies.
my $cookies = $drv->cookies;

=head2 Navigation

Expand Down
3 changes: 2 additions & 1 deletion t/00-namespace.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ is_deeply [ sort keys %got ], [ qw/
base_url
capabilities
close_page
cookie
cookie_delete
cookies
delete_cookie
dismiss_alert
execute
execute_phantom
Expand Down
6 changes: 3 additions & 3 deletions t/05-return-self.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use t scalar(
back
base_url
close_page
delete_cookie
cookie_delete
dismiss_alert
forward
get
refresh
switch_page
window_maximize
/
) + 2;
) + 3;

my $drv = WebDriver::Tiny->new( port => 1 );

is $drv->$_('foo'), $drv, "->$_ should return \$self" for @::methods;

is $drv->$_( 1, 1 ), $drv, "->$_ should return \$self"
for qw/window_position window_size/;
for qw/cookie window_position window_size/;
43 changes: 40 additions & 3 deletions xt/all.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use Cwd ();
use File::Temp;
use Test::Deep;
use Test::Fatal;
use Test::More tests => 31;
use Test::More tests => 35;
use URI;
use URI::QueryParam;
use WebDriver::Tiny;
Expand Down Expand Up @@ -69,8 +69,6 @@ cmp_deeply $drv->capabilities, {
webStorageEnabled => 0,
}, 'capabilities';

is_deeply $drv->cookies, {}, 'cookies';

cmp_deeply $drv->page_ids,
[ re qr/^[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12}$/ ], 'page_ids';

Expand All @@ -92,6 +90,45 @@ chomp( my $ver = `phantomjs -v` );

like $drv->user_agent, qr( PhantomJS/\Q$ver\E ), 'user_agent';

note 'Cookies';
###############

is_deeply $drv->cookies, {}, 'No cookies';

$drv->cookie( foo => 'bar' );
$drv->cookie( baz => 'qux', httponly => 1, path => Cwd::fastcwd );

my $cookie = {
domain => '',
httponly => bool(0),
name => 'foo',
path => Cwd::fastcwd . '/xt/',
secure => bool(0),
value => 'bar',
};

cmp_deeply $drv->cookie('foo'), $cookie, 'Cookie "foo" exists';

cmp_deeply $drv->cookies, {
foo => $cookie,
baz => {
domain => '',
httponly => bool(1),
name => 'baz',
path => Cwd::fastcwd,
secure => bool(0),
value => 'qux',
},
}, 'Cookies exists';

$drv->cookie_delete('foo');

is_deeply [ keys %{ $drv->cookies } ], ['baz'], 'Only "baz" left';

$drv->cookie_delete;

is keys %{ $drv->cookies }, 0, 'No cookies left';

note 'Ghost';
#############

Expand Down

0 comments on commit 3692f1a

Please sign in to comment.