From 6760ce4619752c553552f964d5fa8abcb3edef0f Mon Sep 17 00:00:00 2001 From: An Phan Date: Wed, 23 Nov 2016 21:22:47 +0800 Subject: [PATCH] Add several handy WebDriver shortcuts --- tests/e2e/AlbumsScreenTest.php | 2 +- tests/e2e/ArtistsScreenTest.php | 2 +- tests/e2e/DefaultsTest.php | 19 +++++------------- tests/e2e/HomeScreenTest.php | 10 +--------- tests/e2e/PlaylistScreenTest.php | 18 ++++------------- tests/e2e/QueueScreenTest.php | 3 +-- tests/e2e/SettingsScreenTest.php | 22 +++++++++++++++++++++ tests/e2e/SideBarTest.php | 34 ++++++-------------------------- tests/e2e/SongListTest.php | 2 +- tests/e2e/TestCase.php | 17 ++++++++++------ tests/e2e/WebDriverShortcuts.php | 24 ++++++++++++++++++++++ 11 files changed, 77 insertions(+), 76 deletions(-) create mode 100644 tests/e2e/SettingsScreenTest.php diff --git a/tests/e2e/AlbumsScreenTest.php b/tests/e2e/AlbumsScreenTest.php index ad254eaf..3cb4716e 100644 --- a/tests/e2e/AlbumsScreenTest.php +++ b/tests/e2e/AlbumsScreenTest.php @@ -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)'); diff --git a/tests/e2e/ArtistsScreenTest.php b/tests/e2e/ArtistsScreenTest.php index 80ba2bf8..54313f55 100644 --- a/tests/e2e/ArtistsScreenTest.php +++ b/tests/e2e/ArtistsScreenTest.php @@ -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)'); diff --git a/tests/e2e/DefaultsTest.php b/tests/e2e/DefaultsTest.php index ad41ed06..7953bf4f 100644 --- a/tests/e2e/DefaultsTest.php +++ b/tests/e2e/DefaultsTest.php @@ -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); } } diff --git a/tests/e2e/HomeScreenTest.php b/tests/e2e/HomeScreenTest.php index 7914196f..1bdb049f 100644 --- a/tests/e2e/HomeScreenTest.php +++ b/tests/e2e/HomeScreenTest.php @@ -1,10 +1,4 @@ loginAndWait(); - - $this->click('#sidebar a.home'); + $this->loginAndGoTo('home'); // We must see some greetings static::assertTrue($this->el('#homeWrapper > h1')->isDisplayed()); diff --git a/tests/e2e/PlaylistScreenTest.php b/tests/e2e/PlaylistScreenTest.php index b0e8348e..e2e9758d 100644 --- a/tests/e2e/PlaylistScreenTest.php +++ b/tests/e2e/PlaylistScreenTest.php @@ -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'); } } diff --git a/tests/e2e/QueueScreenTest.php b/tests/e2e/QueueScreenTest.php index b559c586..fa78e6c0 100644 --- a/tests/e2e/QueueScreenTest.php +++ b/tests/e2e/QueueScreenTest.php @@ -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 diff --git a/tests/e2e/SettingsScreenTest.php b/tests/e2e/SettingsScreenTest.php new file mode 100644 index 00000000..bbc9e1b6 --- /dev/null +++ b/tests/e2e/SettingsScreenTest.php @@ -0,0 +1,22 @@ +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'); + } +} diff --git a/tests/e2e/SideBarTest.php b/tests/e2e/SideBarTest.php index f985a441..09625f46 100644 --- a/tests/e2e/SideBarTest.php +++ b/tests/e2e/SideBarTest.php @@ -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)'); } } diff --git a/tests/e2e/SongListTest.php b/tests/e2e/SongListTest.php index be83a5c8..f93377b9 100644 --- a/tests/e2e/SongListTest.php +++ b/tests/e2e/SongListTest.php @@ -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'); diff --git a/tests/e2e/TestCase.php b/tests/e2e/TestCase.php index 62c56501..447daf30 100644 --- a/tests/e2e/TestCase.php +++ b/tests/e2e/TestCase.php @@ -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)) { diff --git a/tests/e2e/WebDriverShortcuts.php b/tests/e2e/WebDriverShortcuts.php index 7fbe838c..66c22f1e 100644 --- a/tests/e2e/WebDriverShortcuts.php +++ b/tests/e2e/WebDriverShortcuts.php @@ -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(); } + + }