Skip to content

Instantly share code, notes, and snippets.

@adamgoucher
Created August 22, 2012 21:36
Show Gist options
  • Save adamgoucher/3429609 to your computer and use it in GitHub Desktop.
Save adamgoucher/3429609 to your computer and use it in GitHub Desktop.

Revisions

  1. adamgoucher revised this gist Aug 24, 2012. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions samplepageobject.php
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,14 @@

    require_once('dashboard.php');
    require_once(dirname(__FILE__) . '/../../../PHPWebDriver/WebDriverWait.php');
    require_once(dirname(__FILE__) . '/../../../PHPWebDriver/WebDriverBy.php');

    class SauceLoginPage {
    private $locators = array(
    "username" => array("id", 'username'),
    "password" => array("id", 'password'),
    "submit button" => array("id", 'submit'),
    "errors" => array("css selector", '.error')
    "username" => array(\PHPWebDriver_WebDriverBy::ID, 'username'),
    "password" => array(\PHPWebDriver_WebDriverBy::ID, 'password'),
    "submit button" => array(\PHPWebDriver_WebDriverBy::ID, 'submit'),
    "errors" => array(\PHPWebDriver_WebDriverBy::CSS_SELECTOR, '.error')
    );

    function __construct($session) {
    @@ -59,7 +60,7 @@ function($session, $extra_arguments) {
    }

    function validate() {
    assert('$this->title == "Login - Sauce Labs" /* title should be "Login" */');
    assert('$this->title == "Login - Sauce Labs" /* title should be "Login - Sauce Labs" */');
    return $this;
    }

  2. adamgoucher revised this gist Aug 24, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion samplepageobject.php
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,8 @@ function __get($property) {
    list($type, $string) = $this->locators[$property];
    $e = $this->session->element($type, $string);
    return $e->text();
    case "title":
    return $this->session->title();
    default:
    return $this->$property;
    }
    @@ -57,7 +59,7 @@ function($session, $extra_arguments) {
    }

    function validate() {
    assert('$this->title == "Login" /* title should be "Login" */');
    assert('$this->title == "Login - Sauce Labs" /* title should be "Login" */');
    return $this;
    }

  3. adamgoucher revised this gist Aug 24, 2012. 1 changed file with 25 additions and 18 deletions.
    43 changes: 25 additions & 18 deletions samplepageobject.php
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,27 @@
    <?php
    namespace WebDriver;

    require_once('DashboardPage.php');
    require_once('PHPWebDriver/WebDriverWait.php');
    require_once('dashboard.php');
    require_once(dirname(__FILE__) . '/../../../PHPWebDriver/WebDriverWait.php');

    class LoginPage {
    class SauceLoginPage {
    private $locators = array(
    "username" => array("id", 'username'),
    "password" => array("id", 'password'),
    "submit button" => array("id", 'submit'),
    "errors" => array("css selector", 'errors')
    "errors" => array("css selector", '.error')
    );

    function __construct($session) {
    $this->session = $session
    $this->session = $session;
    }

    function __get($property) {
    switch($property) {
    case "errors":
    $e = $this->session->element($this->locators[$property][0], $this->locators[$property][1]);
    return $e->text;
    list($type, $string) = $this->locators[$property];
    $e = $this->session->element($type, $string);
    return $e->text();
    default:
    return $this->$property;
    }
    @@ -30,7 +31,8 @@ function __set($property, $value) {
    switch($property) {
    case "username":
    case "password":
    $e = $this->session->element($this->locators[$property][0], $this->locators[$property][1]);
    list($type, $string) = $this->locators[$property];
    $e = $this->session->element($type, $string);
    $e->sendKeys($value);
    break;
    default:
    @@ -39,15 +41,16 @@ function __set($property, $value) {
    }

    function open() {
    self::$session->open("http://your.site.com");
    $this->session->open("https://saucelabs.com/login");
    return $this;
    }

    function wait_until_loaded() {
    $w = new PHPWebDriver_WebDriverWait($this->session);
    $w = new \PHPWebDriver_WebDriverWait($this->session, 30, 0.5, array("locator" => $this->locators['submit button']));
    $w->until(
    function($session) {
    return $session->element($this->locators["submit button"][0], $this->locators["submit button"][1]);
    function($session, $extra_arguments) {
    list($type, $string) = $extra_arguments['locator'];
    return $session->element($type, $string);
    }
    );
    return $this;
    @@ -61,17 +64,21 @@ function validate() {
    function login_as($username, $password, $success=true) {
    $this->username = $username;
    $this->password = $password;
    $e = $this->session->element($this->locators["submit button"][0], $this->locators["submit button"][1]);
    $e->click()

    list($type, $string) = $this->locators['submit button'];
    $e = $this->session->element($type, $string);
    $e->click();

    if ($success) {
    $p = new DashboardPage($this->session);
    $p = new \DashboardPage($this->session);
    $p->wait_until_loaded();
    return $p;
    } else {
    $w = new PHPWebDriver_WebDriverWait($this->session);
    $w = new \PHPWebDriver_WebDriverWait($this->session, 30, 0.5, array("locator" => $this->locators['errors']));
    $w->until(
    function($session) {
    $e = $session->element($this->locators["errors"][0], $this->locators["errors"][1]);
    function($session, $extra_arguments) {
    list($type, $string) = $extra_arguments['locator'];
    $e = $session->element($type, $string);
    return $e->displayed();
    }
    );
  4. adamgoucher created this gist Aug 22, 2012.
    81 changes: 81 additions & 0 deletions samplepageobject.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <?php
    namespace WebDriver;

    require_once('DashboardPage.php');
    require_once('PHPWebDriver/WebDriverWait.php');

    class LoginPage {
    private $locators = array(
    "username" => array("id", 'username'),
    "password" => array("id", 'password'),
    "submit button" => array("id", 'submit'),
    "errors" => array("css selector", 'errors')
    );

    function __construct($session) {
    $this->session = $session
    }

    function __get($property) {
    switch($property) {
    case "errors":
    $e = $this->session->element($this->locators[$property][0], $this->locators[$property][1]);
    return $e->text;
    default:
    return $this->$property;
    }
    }

    function __set($property, $value) {
    switch($property) {
    case "username":
    case "password":
    $e = $this->session->element($this->locators[$property][0], $this->locators[$property][1]);
    $e->sendKeys($value);
    break;
    default:
    $this->$property = $value;
    }
    }

    function open() {
    self::$session->open("http://your.site.com");
    return $this;
    }

    function wait_until_loaded() {
    $w = new PHPWebDriver_WebDriverWait($this->session);
    $w->until(
    function($session) {
    return $session->element($this->locators["submit button"][0], $this->locators["submit button"][1]);
    }
    );
    return $this;
    }

    function validate() {
    assert('$this->title == "Login" /* title should be "Login" */');
    return $this;
    }

    function login_as($username, $password, $success=true) {
    $this->username = $username;
    $this->password = $password;
    $e = $this->session->element($this->locators["submit button"][0], $this->locators["submit button"][1]);
    $e->click()
    if ($success) {
    $p = new DashboardPage($this->session);
    $p->wait_until_loaded();
    return $p;
    } else {
    $w = new PHPWebDriver_WebDriverWait($this->session);
    $w->until(
    function($session) {
    $e = $session->element($this->locators["errors"][0], $this->locators["errors"][1]);
    return $e->displayed();
    }
    );
    return $this;
    }
    }
    }