Skip to content

Instantly share code, notes, and snippets.

@jakzal
Created February 8, 2019 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakzal/fa1a1000ff8be48964478cffd43144bc to your computer and use it in GitHub Desktop.
Save jakzal/fa1a1000ff8be48964478cffd43144bc to your computer and use it in GitHub Desktop.
Page objects
<?php declare(strict_types=1);
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Session;
use Zalas\PageObject\Annotation\FindBy;
use Zalas\PageObject\Annotation\MinkSession;
class SearchPage
{
/**
* @MinkSession()
* @var Session
*/
protected $session;
/**
* @FindBy(xpath="//form[@name='f']")
* @var NodeElement
*/
protected $searchForm;
/**
* @FindBy(xpath="//title")
* @var NodeElement
*/
protected $title;
public function visit(): self
{
$this->session->visit('https://www.google.co.uk/');
return new self();
}
public function search(string $keywords): self
{
$this->searchForm->fillField('q', $keywords);
$this->searchForm->pressButton('Google Search');
return new self();
}
public function getTitle(): string
{
return $this->title->getText();
}
}
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class PageObjectTest extends TestCase
{
public function testSearchPage()
{
$searchPage = (new SearchPage())
->visit()
->search('foo');
$this->assertSame('foo - Google Search', $searchPage->getTitle());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment