Skip to content

Instantly share code, notes, and snippets.

@linguinee
Last active December 28, 2015 19:58
Show Gist options
  • Save linguinee/7553707 to your computer and use it in GitHub Desktop.
Save linguinee/7553707 to your computer and use it in GitHub Desktop.
Login screen object and test object example.
// With the page object model, what I have is something like this:
class LoginScreen extends MainScreen {
protected static SCREEN_NAME = 'LoginScreen';
public function __construct($driver)
{
parent::__construct($driver);
// Various login element selectors.
$this->EMAIL_FIELD = WebDriverBy::id('email_address');
$this->PASS_FIELD = WebDriverBy::id('password');
// etc...
}
public function onScreen()
{
try {
$elem = $this->driver->wait($this->WAIT_UNTIL, $this->WAIT_PAUSE)->
until(WebDriverExpectedCondition::
visibilityOfElementLocated($this->EMAIL_FIELD));
return (is_null($elem) ? FALSE : TRUE);
} catch (NoSuchElementWebDriverError $e) {
return FALSE;
}
}
/**
* @return MenuScreen
*/
public function login($username, $password)
{
$this->driver->findElement($this->EMAIL_FIELD)->clear();
$this->driver->findElement($this->EMAIL_FIELD)->sendKeys($username);
$this->driver->findElement($this->PASS_FIELD)->sendKeys($password);
$this->driver->findElement($this->LOGIN_BUTTON)->click('');
// Check that the user is logged in.
$menuScreen = new MenuScreen($this->driver);
if (!$menuScreen->onScreen()) {
throw new Exception(self::SCREEN_NAME . ': User unable to log in.');
}
return $menuScreen;
}
// etc...
}
// And my test looks very simple, like this:
class LoginTest extends TestCase
{
protected $driver;
public function setUp()
{
$this->driver = MainScreen::newDriver();
}
public function testLogin()
{
$loginScreen = new LoginScreen($this->driver);
$this->assertTrue($loginScreen->onScreen());
$loginScreen->login('foo', 'bar');
}
public function testLogout()
{
$loginScreen = new LoginScreen($this->driver);
$menuScreen = $loginScreen->login('foo', 'bar');
$loginScreen = $menuScreen->logout();
$this->assertTrue($loginScreen->onScreen());
}
public function tearDown()
{
if (!is_null($this->driver)) $this->driver->quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment