Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit dd1313f

Browse files
committed
Use W3C command to get named cookie
1 parent 1dfdeae commit dd1313f

File tree

4 files changed

+123
-2
lines changed

4 files changed

+123
-2
lines changed

lib/Remote/DriverCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class DriverCommand
149149
// W3C specific
150150
const ACTIONS = 'actions';
151151
const GET_ELEMENT_PROPERTY = 'getElementProperty';
152+
const GET_NAMED_COOKIE = 'getNamedCookie';
152153
const TAKE_ELEMENT_SCREENSHOT = 'takeElementScreenshot';
153154

154155
private function __construct()

lib/Remote/HttpCommandExecutor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class HttpCommandExecutor implements WebDriverCommandExecutor
5656
DriverCommand::GET_ACTIVE_ELEMENT => ['method' => 'POST', 'url' => '/session/:sessionId/element/active'],
5757
DriverCommand::GET_ALERT_TEXT => ['method' => 'GET', 'url' => '/session/:sessionId/alert_text'],
5858
DriverCommand::GET_ALL_COOKIES => ['method' => 'GET', 'url' => '/session/:sessionId/cookie'],
59+
DriverCommand::GET_NAMED_COOKIE => ['method' => 'GET', 'url' => '/session/:sessionId/cookie/:name'],
5960
DriverCommand::GET_ALL_SESSIONS => ['method' => 'GET', 'url' => '/sessions'],
6061
DriverCommand::GET_AVAILABLE_LOG_TYPES => ['method' => 'GET', 'url' => '/session/:sessionId/log/types'],
6162
DriverCommand::GET_CURRENT_URL => ['method' => 'GET', 'url' => '/session/:sessionId/url'],

lib/WebDriverOptions.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Facebook\WebDriver;
1717

18+
use Facebook\WebDriver\Exception\NoSuchCookieException;
1819
use Facebook\WebDriver\Remote\DriverCommand;
1920
use Facebook\WebDriver\Remote\ExecuteMethod;
2021
use InvalidArgumentException;
@@ -48,7 +49,7 @@ public function __construct(ExecuteMethod $executor, $isW3cCompliant = false)
4849
*/
4950
public function addCookie($cookie)
5051
{
51-
if (is_array($cookie)) {
52+
if (is_array($cookie)) { // @todo @deprecated remove in 2.0
5253
$cookie = Cookie::createFromArray($cookie);
5354
}
5455
if (!$cookie instanceof Cookie) {
@@ -95,10 +96,20 @@ public function deleteCookieNamed($name)
9596
* Get the cookie with a given name.
9697
*
9798
* @param string $name
98-
* @return Cookie|null The cookie, or null if no cookie with the given name is presented.
99+
* @throws NoSuchCookieException In W3C compliant mode if no cookie with the given name is present
100+
* @return Cookie|null The cookie, or null in JsonWire mode if no cookie with the given name is present
99101
*/
100102
public function getCookieNamed($name)
101103
{
104+
if ($this->isW3cCompliant) {
105+
$cookieArray = $this->executor->execute(
106+
DriverCommand::GET_NAMED_COOKIE,
107+
[':name' => $name]
108+
);
109+
110+
return Cookie::createFromArray($cookieArray);
111+
}
112+
102113
$cookies = $this->getCookies();
103114
foreach ($cookies as $cookie) {
104115
if ($cookie['name'] === $name) {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver;
17+
18+
use Facebook\WebDriver\Exception\NoSuchCookieException;
19+
20+
/**
21+
* @covers \Facebook\WebDriver\WebDriverOptions
22+
*/
23+
class WebDriverOptionsCookiesTest extends WebDriverTestCase
24+
{
25+
protected function setUp()
26+
{
27+
parent::setUp();
28+
29+
$this->driver->get($this->getTestPageUrl('index.html'));
30+
}
31+
32+
public function testShouldSetGetAndDeleteCookies()
33+
{
34+
$cookie1 = new Cookie('cookie1', 'cookie1Value');
35+
$cookie2 = new Cookie('cookie2', 'cookie2Value');
36+
$cookie3 = new Cookie('cookie3', 'cookie3Value');
37+
38+
// Verify initial state - no cookies are present
39+
$this->assertSame([], $this->driver->manage()->getCookies());
40+
41+
// Add cookie1
42+
$this->driver->manage()->addCookie($cookie1);
43+
44+
// get all cookies
45+
$cookiesWithOneCookie = $this->driver->manage()->getCookies();
46+
$this->assertCount(1, $cookiesWithOneCookie);
47+
$this->assertContainsOnlyInstancesOf(Cookie::class, $cookiesWithOneCookie);
48+
$this->assertSame('cookie1', $cookiesWithOneCookie[0]->getName());
49+
$this->assertSame('cookie1Value', $cookiesWithOneCookie[0]->getValue());
50+
$this->assertSame('/', $cookiesWithOneCookie[0]->getPath());
51+
$this->assertSame('localhost', $cookiesWithOneCookie[0]->getDomain());
52+
53+
// Add cookie2
54+
$this->driver->manage()->addCookie($cookie2);
55+
56+
// get all cookies
57+
$cookiesWithTwoCookies = $this->driver->manage()->getCookies();
58+
59+
$this->assertCount(2, $cookiesWithTwoCookies);
60+
$this->assertContainsOnlyInstancesOf(Cookie::class, $cookiesWithTwoCookies);
61+
62+
// normalize received cookies (their order is arbitrary)
63+
$normalizedCookies = [
64+
$cookiesWithTwoCookies[0]->getName() => $cookiesWithTwoCookies[0]->getValue(),
65+
$cookiesWithTwoCookies[1]->getName() => $cookiesWithTwoCookies[1]->getValue(),
66+
];
67+
ksort($normalizedCookies);
68+
$this->assertSame(['cookie1' => 'cookie1Value', 'cookie2' => 'cookie2Value'], $normalizedCookies);
69+
70+
// getCookieNamed()
71+
$onlyCookieOne = $this->driver->manage()->getCookieNamed('cookie1');
72+
$this->assertInstanceOf(Cookie::class, $onlyCookieOne);
73+
$this->assertSame('cookie1', $onlyCookieOne->getName());
74+
$this->assertSame('cookie1Value', $onlyCookieOne->getValue());
75+
76+
// deleteCookieNamed()
77+
$this->driver->manage()->deleteCookieNamed('cookie1');
78+
$cookiesWithOnlySecondCookie = $this->driver->manage()->getCookies();
79+
$this->assertCount(1, $cookiesWithOnlySecondCookie);
80+
$this->assertSame('cookie2', $cookiesWithOnlySecondCookie[0]->getName());
81+
82+
// getting non-existent cookie should throw an exception in W3C mode but return null in JsonWire mode
83+
if (self::isW3cProtocolBuild()) {
84+
try {
85+
$noSuchCookieExceptionThrown = false;
86+
$this->driver->manage()->getCookieNamed('cookie1');
87+
} catch (NoSuchCookieException $e) {
88+
$noSuchCookieExceptionThrown = true;
89+
} finally {
90+
$this->assertTrue($noSuchCookieExceptionThrown, 'NoSuchCookieException was not thrown');
91+
}
92+
} else {
93+
$this->assertNull($this->driver->manage()->getCookieNamed('cookie1'));
94+
}
95+
96+
// deleting non-existent cookie shod not throw an error
97+
$this->driver->manage()->deleteCookieNamed('cookie1');
98+
99+
// Add cookie3
100+
$this->driver->manage()->addCookie($cookie1);
101+
$this->assertCount(2, $this->driver->manage()->getCookies());
102+
103+
// Delete all cookies
104+
$this->driver->manage()->deleteAllCookies();
105+
106+
$this->assertSame([], $this->driver->manage()->getCookies());
107+
}
108+
}

0 commit comments

Comments
 (0)