Skip to content

@adamgoucher /WebDriverCookies.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
WebDriver and Cookies
<?php
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPWebDriver/WebDriver.php';
require_once 'PHPWebDriver/WebDriverActionChains.php';
class CookieTest extends PHPUnit_Framework_TestCase {
public function setUp() {
$web_driver = new PHPWebDriver_WebDriver("http://localhost:4444/wd/hub");
$this->session = $web_driver->session();
}
public function tearDown() {
$this->session->close();
}
/**
* @test
*/
public function no_cookies_on_initial_load() {
$this->session->open("http://element34.ca/cookies/index.php");
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 0);
}
/**
* @test
*/
public function set_cookie() {
// cookie_crumbs: An array, with required keys - "name", "value"
// optional keys - "path", "domain", "expiry", "secure"
$cookie_crumbs = array("name" => "monkey",
"value" => "butt",
"path" => "/",
"secure" => False);
$this->session->open("http://element34.ca/cookies/index.php");
$this->session->setCookie($cookie_crumbs);
$this->session->open("http://element34.ca/cookies/index.php");
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 1);
$this->assertEquals($this->session->element("id", 'cookiecount')->text(), "You have 1 cookies set");
}
/**
* @test
*/
public function single_cookie() {
$this->session->open("http://element34.ca/cookies/index.php");
$this->session->element("css selector", 'select[name="colour"] option[value="White"]')->click();
$this->session->element("id", 'single')->submit();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 1);
$cookie = $this->session->getCookie("White");
$this->assertEquals($cookie["value"], 1);
}
/**
* @test
*/
public function multiple_cookies() {
$this->session->open("http://element34.ca/cookies/index.php");
$this->session->element("css selector", 'select[name^="colours"] option[value="Green"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Yellow"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Brown"]')->click();
$this->session->element("id", 'multiple')->submit();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 3);
$cookie = $this->session->getCookie("Green");
$this->assertEquals($cookie["value"], 1);
$cookie = $this->session->getCookie("Yellow");
$this->assertEquals($cookie["value"], 1);
$cookie = $this->session->getCookie("Brown");
$this->assertEquals($cookie["value"], 1);
}
/**
* @test
*/
public function deleting_cookies_on_the_server() {
$this->session->open("http://element34.ca/cookies/index.php");
$this->session->element("css selector", 'select[name^="colours"] option[value="Green"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Yellow"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Brown"]')->click();
$this->session->element("id", 'multiple')->submit();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 3);
$this->session->element("id", 'empty')->submit();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 0);
}
/**
* @test
*/
public function deleting_cookies_on_the_client() {
$this->session->open("http://element34.ca/cookies/index.php");
$this->session->element("css selector", 'select[name^="colours"] option[value="Green"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Yellow"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Brown"]')->click();
$this->session->element("id", 'multiple')->submit();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 3);
$this->session->deleteAllCookies();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 0);
$this->session->open("http://element34.ca/cookies/index.php");
$this->assertEquals($this->session->element("id", 'cookiecount')->text(), "You have 0 cookies set");
}
/**
* @test
*/
public function deleting_cookie_on_the_client() {
$this->session->open("http://element34.ca/cookies/index.php");
$this->session->element("css selector", 'select[name^="colours"] option[value="Green"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Yellow"]')->click();
$this->session->element("css selector", 'select[name^="colours"] option[value="Brown"]')->click();
$this->session->element("id", 'multiple')->submit();
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 3);
$this->session->deleteCookie("Yellow");
$cookies = $this->session->getAllCookies();
$this->assertEquals(count($cookies), 2);
foreach ($cookies as $cookie) {
$this->assertTrue(in_array($cookie["name"], array("Brown", "Green")));
}
$this->session->open("http://element34.ca/cookies/index.php");
$this->assertEquals($this->session->element("id", 'cookiecount')->text(), "You have 2 cookies set");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.