Add several handy WebDriver shortcuts

This commit is contained in:
An Phan 2016-11-23 21:22:47 +08:00
parent 7b4908f85d
commit 6760ce4619
No known key found for this signature in database
GPG key ID: 05536BB4BCDC02A2
11 changed files with 77 additions and 76 deletions

View file

@ -8,7 +8,7 @@ class AlbumsScreenTest extends TestCase
{
public function testAlbumsScreen()
{
$this->loginAndWait()->goto('albums');
$this->loginAndGoTo('albums');
static::assertNotEmpty($this->els('#albumsWrapper .albums article.item'));
$firstAlbum = $this->el('#albumsWrapper .albums article.item:nth-child(1)');

View file

@ -8,7 +8,7 @@ class ArtistsScreenTest extends TestCase
{
public function testArtistsScreen()
{
$this->loginAndWait()->goto('artists');
$this->loginAndGoTo('artists');
static::assertNotEmpty($this->els('#artistsWrapper .artists article.item'));
$firstArtist = $this->el('#artistsWrapper .artists article.item:nth-child(1)');

View file

@ -2,9 +2,6 @@
namespace E2E;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
class KoelTest extends TestCase
{
public function testDefaults()
@ -17,24 +14,18 @@ class KoelTest extends TestCase
static::assertCount(1, $this->els($formSelector));
// We submit rubbish and expect an error class on the form.
$this->login('foo@bar.com', 'ThisIsWongOnSoManyLevels');
$this->waitUntil(WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector("$formSelector.error")
));
$this->login('foo@bar.com', 'ThisIsWongOnSoManyLevels')
->waitUntilSeen("$formSelector.error");
// Now we submit good stuff and make sure we're in.
$this->login();
$this->waitUntil(WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('#userBadge > a.view-profile.control > span'), 'Koel Admin'
));
$this->login()
->waitUntilTextSeenIn('Koel Admin', '#userBadge > a.view-profile.control > span');
// Default URL must be Home
static::assertEquals($this->url.'/#!/home', $this->driver->getCurrentURL());
// While we're at this, test logging out as well.
$this->click('#userBadge > a.logout');
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::cssSelector($formSelector)
));
$this->waitUntilSeen($formSelector);
}
}

View file

@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: an
* Date: 15/11/16
* Time: 4:14 PM
*/
namespace E2E;
@ -14,9 +8,7 @@ class HomeScreenTest extends TestCase
{
public function testHomeScreen()
{
$this->loginAndWait();
$this->click('#sidebar a.home');
$this->loginAndGoTo('home');
// We must see some greetings
static::assertTrue($this->el('#homeWrapper > h1')->isDisplayed());

View file

@ -2,33 +2,23 @@
namespace E2E;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
class PlaylistScreenTest extends TestCase
{
use SongListActions;
public function testPlaylistScreen()
{
$this->loginAndWait()
->goto('songs')
$this->loginAndGoTo('songs')
->selectRange()
->createPlaylist('Bar');
$this->waitUntil(WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('#playlists > ul'), 'Bar'
));
$this->waitUntilTextSeenIn('Bar', '#playlists > ul');
$this->click('#sidebar .playlist:nth-last-child(1)');
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::id('playlistWrapper')
));
$this->waitUntilSeen('#playlistWrapper');
$this->click('#playlistWrapper .btn-delete-playlist');
// expect a confirmation
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::cssSelector('.sweet-alert')
));
$this->waitUntilSeen('.sweet-alert');
}
}

View file

@ -6,8 +6,7 @@ class QueueScreenTest extends TestCase
{
public function test()
{
$this->loginAndWait();
$this->goto('queue');
$this->loginAndGoTo('queue');
static::assertContains('Current Queue', $this->el('#queueWrapper > h1 > span')->getText());
// As the queue is currently empty, the "Shuffling all song" link should be there

View file

@ -0,0 +1,22 @@
<?php
namespace E2E;
class SettingsScreenTest extends TestCase
{
public function testSettingsScreen()
{
$this->loginAndGoTo('settings');
$this->typeIn('#inputSettingsPath', dirname(__DIR__.'/../songs'));
$this->enter();
// Wait for the page to reload
$this->waitUntil(function () {
return $this->driver->executeScript('return document.readyState') === 'complete';
});
// And for the loading screen to disappear
$this->waitUntilNotSeen('#overlay');
$this->goto('albums');
// and make sure the scanning is good.
$this->waitUntilTextSeenIn('Koel Testing Vol', '#albumsWrapper');
}
}

View file

@ -2,10 +2,6 @@
namespace E2E;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverExpectedCondition;
class SideBarTest extends TestCase
{
public function testSideBar()
@ -22,44 +18,26 @@ class SideBarTest extends TestCase
// Add a playlist
$this->click('#playlists > h1 > i.create');
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::cssSelector('#playlists > form.create')
));
$this->waitUntilSeen('#playlists > form.create');
$this->typeIn('#playlists > form > input[type="text"]', 'Bar');
$this->enter();
/** @var WebDriverElement $mostRecentPlaylist */
$mostRecentPlaylist = null;
$this->waitUntil(function () use (&$mostRecentPlaylist) {
/* @var WebDriverElement $mostRecentPlaylist */
$this->waitUntil(function () {
$list = $this->els('#playlists .playlist');
$mostRecentPlaylist = end($list);
return $mostRecentPlaylist->getText() === 'Bar';
return end($list)->getText() === 'Bar';
});
// Double click to edit/rename a playlist
$this->doubleClick('#playlists .playlist:nth-child(2)');
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::cssSelector('#playlists .playlist:nth-child(2) input[type="text"]')
));
$this->waitUntilSeen('#playlists .playlist:nth-child(2) input[type="text"]');
$this->typeIn('#playlists .playlist:nth-child(2) input[type="text"]', 'Qux');
$this->enter();
$this->waitUntil(
WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('#playlists .playlist:nth-child(2)'),
'Qux'
)
);
$this->waitUntilTextSeenIn('Qux', '#playlists .playlist:nth-child(2)');
// Edit with an empty name shouldn't do anything.
$this->doubleClick('#playlists .playlist:nth-child(2)');
$this->click('#playlists .playlist:nth-child(2) input[type="text"]')->clear();
$this->enter();
$this->waitUntil(
WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('#playlists .playlist:nth-child(2)'),
'Qux'
)
);
$this->waitUntilTextSeenIn('Qux', '#playlists .playlist:nth-child(2)');
}
}

View file

@ -125,7 +125,7 @@ class SongListTest extends TestCase
public function testContextMenu()
{
$this->loginAndWait()->goto('songs');
$this->loginAndGoTo('songs');
$this->rightClickOnSong();
$by = WebDriverBy::cssSelector('#songsWrapper .song-menu');

View file

@ -76,20 +76,22 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
*
* @param string $username
* @param string $password
*
* @return $this
*/
protected function login($username = 'koel@example.com', $password = 'SoSecureK0el')
{
$this->typeIn("#app > div.login-wrapper > form > [type='email']", $username);
$this->typeIn("#app > div.login-wrapper > form > [type='password']", $password);
$this->enter();
return $this;
}
protected function loginAndWait()
{
$this->login();
$this->waitUntil(WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('#userBadge > a.view-profile.control > span'), 'Koel Admin'
));
$this->waitUntilTextSeenIn('Koel Admin', '#userBadge > a.view-profile.control > span');
return $this;
}
@ -113,13 +115,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
$this->click("#sidebar a.$screen");
}
$this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::cssSelector($this->wrapperId)
));
$this->waitUntilSeen($this->wrapperId);
return $this;
}
protected function loginAndGoTo($screen)
{
return $this->loginAndWait()->goto($screen);
}
protected function waitForUserInput()
{
if (trim(fgets(fopen('php://stdin', 'rb'))) !== chr(13)) {

View file

@ -6,6 +6,7 @@ use Facebook\WebDriver\Interactions\Internal\WebDriverDoubleClickAction;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverKeys;
trait WebDriverShortcuts
@ -97,6 +98,27 @@ trait WebDriverShortcuts
return $this->driver->wait($timeout)->until($func);
}
public function waitUntilSeen($selector)
{
return $this->waitUntil(WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::cssSelector($selector)
));
}
public function waitUntilNotSeen($selector)
{
return $this->waitUntil(WebDriverExpectedCondition::invisibilityOfElementLocated(
WebDriverBy::cssSelector($selector)
));
}
public function waitUntilTextSeenIn($text, $selector)
{
$this->waitUntil(WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector($selector), $text
));
}
protected function back()
{
return $this->driver->navigate()->back();
@ -111,4 +133,6 @@ trait WebDriverShortcuts
{
return $this->driver->navigate()->refresh();
}
}