On Github BenjaminCool / ui-testing-talk
class UITestWPExample extends PHPUnit_Framework_TestCase {
...
public function setUp(){
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->driver = RemoteWebDriver::create('http://127.0.0.1:4444/wd/hub',$capabilities);
$this->driver->get('http://local.wordpress.dev/wp-login.php');
}
...
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
protected $driver;
public function canLogInAndSeeHelloDolly() {
$driver = $this->driver;
$username = $driver->findElement(WebDriverBy::id('user_login'));
$password = $driver->findElement(WebDriverBy::id('user_pass'));
$rememberme = $driver->findElement(WebDriverBy::id('rememberme'));
$submit = $driver->findElement(WebDriverBy::id('wp-submit'));
$username->sendText('admin');
$password->sendText('password');
$rememberme->click();
$submit->click();
$admin_url = 'http://local.wordpress.dev/wp-admin/index.php';
$driver->wait(5,500)->until(
function () use ($driver) {
return $driver->getCurrentURL() == $admin_url;
},
'5 seconds passed page did not load. oh noes.'
);
$this->assertEquals($admin_url,$driver->getCurrentURL());
$this->assertTrue($driver->findElement(WebDriverBy::id('dolly'))->isDisplayed());
}
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
...
...
...
public function canLogIn() {
$driver = $this->driver;
$username = $driver->findElement(WebDriverBy::id('user_login'));
$password = $driver->findElement(WebDriverBy::id('user_pass'));
$rememberme = $driver->findElement(WebDriverBy::id('rememberme'));
$submit = $driver->findElement(WebDriverBy::id('wp-submit'));
$username->sendText('admin');
$password->sendText('password');
$rememberme->click();
$submit->click();
$admin_url = 'http://local.wordpress.dev/wp-admin/index.php';
$driver->wait(5,500)->until(
function () use ($driver) {
return $driver->getCurrentURL() == $admin_url;
},
'5 seconds passed page did not load. oh noes.'
);
$this->assertEquals($admin_url,$driver->getCurrentURL(),'not logged in');
}
}
class WPPage {
public $driver, $site_url, $login_url, $admin_url;
public function __construct($driver){
$this->driver = $driver;
$this->site_url = "http://local.wordpress.dev";
$this->login_url = "{$this->site_url}/wp-login.php";
$this->admin_url = "{$this->site_url}/wp-admin/";
}
public function get_admin_url(){
return $this->admin_url;
}
public function getCurrentURL(){
return $this->driver->getCurrentURL();
}
public function is_hello_dolly_visible(){
return $this->driver->findElement(WebDriverBy::id('dolly'))->isDisplayed();
}
public function login(){
try{
$this->driver->get($this->login_url);
$this->driver->findElement(WebDriverBy::id('user_login'))->sendKeys('admin');
$this->driver->findElement(WebDriverBy::id('user_pass'))->sendKeys('password');
$this->driver->findElement(WebDriverBy::id('rememberme'))->click();
$this->driver->findElement(WebDriverBy::id('wp-submit'))->click();
$this->driver->wait(5,500)->until(
function () {
return $this->driver->getCurrentURL() == $this->admin_url;
},
'5 seconds passed page did not load. oh noes.'
);
}
catch (Exception $e){
return false;
}
return true;
}
}
class WPPage {
public $driver, $site_url, $login_url, $admin_url;
public function __construct($driver){
$this->driver = $driver;
$this->site_url = "http://local.wordpress.dev";
$this->login_url = "{$this->site_url}/wp-login.php";
$this->admin_url = "{$this->site_url}/wp-admin/";
}
...
}
class WPPage {
...
public function get_admin_url(){
return $this->admin_url;
}
...
}
class WPPage {
...
public function is_hello_dolly_visible(){
return $this->driver->findElement(WebDriverBy::id('dolly'))->isDisplayed();
}
...
}
class WPPage {
...
public function login(){
try{
$this->driver->get($this->login_url);
$this->driver->findElement(WebDriverBy::id('user_login'))->sendKeys('admin');
$this->driver->findElement(WebDriverBy::id('user_pass'))->sendKeys('password');
$this->driver->findElement(WebDriverBy::id('rememberme'))->click();
$this->driver->findElement(WebDriverBy::id('wp-submit'))->click();
$this->driver->wait(5,500)->until(
function () {
return $this->driver->getCurrentURL() == $this->admin_url;
},
'5 seconds passed page did not load. oh noes.'
);
}
catch (Exception $e){
return false;
}
return true;
}
}
$this->driver->findElement(WebDriverBy::id('user_login'))->sendKeys('admin');
$this->driver->findElement(WebDriverBy::id('user_pass'))->sendKeys('password');
$this->driver->findElement(WebDriverBy::id('rememberme'))->click();
$this->driver->findElement(WebDriverBy::id('wp-submit'))->click();
public function login_enter_user_login($keys){
$this->driver->findElement(WebDriverBy::id('user_login'))->sendKeys($keys);
}
public function login_enter_user_pass($keys){
$this->driver->findElement(WebDriverBy::id('user_pass'))->sendKeys($keys);
}
public function login_click_rememberme(){
$this->driver->findElement(WebDriverBy::id('wp-submit'))->click();
}
public function login_click_submit(){
$this->driver->findElement(WebDriverBy::id('wp-submit'))->click();
}
$this->login_enter_user_login('admin');
$this->login_enter_user_pass('password');
$this->login_click_remember_me();
$this->login_click_submit();
$this->driver->wait(5,500)->until(
function () {
return $this->driver->getCurrentURL() == $this->admin_url;
},
'5 seconds passed page did not load. oh noes.'
);
class WPAdminPage {
protected $driver;
public __construct($driver){
$this->driver = $driver;
}
public function isHelloDollyVisible(){
try{
$dolly = $this->driver->findElement(WebDriverBy::id('dolly'));
return $dolly->isDisplayed();
}
catch(NoSuchElementException $e){
return false;
}
}
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
protected $driver;
public function setUp(){
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->driver = RemoteWebDriver::create('http://selenium_server',$capabilities);
$this->driver->get('http://local.wordpress.dev/wp-login.php');
}
public function testCanLogIn() {
$page = new WPLoginPage($this->driver);
$page->logIn();
$page = new WPAdminPage($this->driver);
$this->assertEquals($page->get_admin_url(),$page->getCurrentURL());
}
public function testCanLogInAndSeeHelloDolly() {
$page = new WPAdminPage($this->driver);
$page->logIn();
$page = new WPAdminPage($this->driver);
$this->assertEquals($page->get_admin_url(),$page->getCurrentURL());
$this->assertTrue($page->isHelloDollyVisible());
}
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
...
public function setUp(){
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->driver = RemoteWebDriver::create('http://selenium_server',$capabilities);
$this->driver->get('http://local.wordpress.dev/wp-login.php');
}
...
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
...
public function testCanLogIn() {
$page = new WPLoginPage($this->driver);
$page->logIn();
$page = new WPAdminPage($this->driver);
$this->assertEquals($page->get_admin_url(),$page->getCurrentURL());
}
...
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
...
public function testCanLogInAndSeeHelloDolly() {
$page = new WPAdminPage($this->driver);
$page->login();
$page = new WPAdminPage($this->driver);
$this->assertEquals($page->get_admin_url(),$page->getCurrentURL());
$this->assertTrue($page->isHelloDollyVisible());
}
...
}
class UITestWPExample extends PHPUnit_Framework_TestCase {
...
...
...
public function testButtonsHaveAriaRoles(){
$page = new HomePage($this->driver);
$buttons = $page->getButtons();
foreach($buttons as $button){
assertTrue($button->hasAttribute('aria-role'));
}
}
}
{
"require-dev" : {
"phpunit/phpunit" : "5.3.*",
"facebook/webdriver" : "dev-master"
}
}