|
| 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