diff --git a/tests/Browser/Components/Dialog.php b/tests/Browser/Components/Dialog.php new file mode 100644 index 000000000..7645a6314 --- /dev/null +++ b/tests/Browser/Components/Dialog.php @@ -0,0 +1,125 @@ +num = $num ?: 1; + } + + /** + * Get the root selector for the component. + * + * @return string + */ + public function selector() + { + // work with the specified dialog (in case there's more than one) + $suffix = $this->num > 1 ? str_repeat(' + div + .ui-dialog', $this->num - 1) : ''; + return '.ui-dialog' . $suffix; + } + + /** + * Assert that the browser page contains the component. + * + * @param Browser $browser + * + * @return void + */ + public function assert($browser) + { + $browser->waitFor($this->selector()); + } + + /** + * Get the element shortcuts for the component. + * + * @return array + */ + public function elements() + { + return [ + '@title' => '.ui-dialog-titlebar', + '@content' => '.ui-dialog-content', + '@footer' => '.ui-dialog-buttonset', + ]; + } + + /** + * Assert dialog title + */ + public function assertDialogTitle($browser, $title) + { + $browser->assertSeeIn('@title', $title); + } + + /** + * Assert dialog content (for simple text dialogs) + */ + public function assertDialogContent($browser, $text) + { + $browser->assertSeeIn('@content', $text); + } + + /** + * Assert dialog button + */ + public function assertButton($browser, $name, $label) + { + $selector = "@footer button.{$name}"; + $browser->assertVisible($selector, $title) + ->assertSeeIn($selector, $label); + } + + /** + * Click dialog button + */ + public function clickButton($browser, $name, $expect_close = true) + { + $browser->click('@footer button.' . $name); + + if ($expect_close) { + $browser->waitUntilMissing($this->selector()); + } + } + + /** + * Close dialog + */ + public function closeDialog($browser) + { + $browser->click('@footer button.cancel, @footer button.close') + ->waitUntilMissing($this->selector()); + } + + /** + * Close dialog with ESC key + */ + public function pressESC($browser) + { + $browser->driver->getKeyboard()->sendKeys(\Facebook\WebDriver\WebDriverKeys::ESCAPE); + $browser->waitUntilMissing($this->selector()); + } + + /** + * Execute code within dialog's iframe + */ + public function withinDialogFrame($browser, $callback) + { + $browser->withinFrame('@content iframe', function ($browser) use ($callback) { + $browser->withinBody(function ($browser) use ($callback) { + $callback($browser); + }); + }); + } +} diff --git a/tests/Browser/Contacts/GroupsTest.php b/tests/Browser/Contacts/GroupsTest.php index 4d7dc7a7d..f4db05571 100644 --- a/tests/Browser/Contacts/GroupsTest.php +++ b/tests/Browser/Contacts/GroupsTest.php @@ -2,6 +2,7 @@ namespace Tests\Browser\Contacts; +use Tests\Browser\Components\Dialog; use Tests\Browser\Components\Popupmenu; class GroupsTest extends \Tests\Browser\TestCase @@ -61,17 +62,15 @@ class GroupsTest extends \Tests\Browser\TestCase $browser->clickMenuItem('create'); }); - $browser->waitFor('.ui-dialog'); - $browser->with('.ui-dialog', function ($browser) { - $browser - ->assertSeeIn('.ui-dialog-titlebar', 'Create new group') - ->assertFocused('input.form-control') - ->type('input.form-control', 'New Group') - ->click('button.mainaction'); + $browser->with(new Dialog(), function ($browser) { + $browser->assertDialogTitle('Create new group') + ->assertButton('save.mainaction', 'Save') + ->assertButton('cancel', 'Cancel') + ->assertFocused('@content input.form-control') + ->type('@content input.form-control', 'New Group') + ->clickButton('save'); }); - $browser->waitUntilMissing('.ui-dialog'); - $browser->with('#directorylist', function ($browser) { $browser->waitFor('li:first-child ul.groups') ->assertVisible('.treetoggle.expanded') @@ -113,17 +112,17 @@ class GroupsTest extends \Tests\Browser\TestCase $browser->clickMenuItem('group.rename'); }); - $browser->waitFor('.ui-dialog'); - $browser->with('.ui-dialog', function ($browser) { - $browser->assertSeeIn('.ui-dialog-titlebar', 'Rename group') - ->assertFocused('input.form-control') - ->assertValue('input.form-control', 'New Group') - ->type('input.form-control', 'Renamed') - ->click('button.mainaction'); + $browser->with(new Dialog(), function ($browser) { + $browser + ->assertDialogTitle('Rename group') + ->assertButton('save.mainaction', 'Save') + ->assertButton('cancel', 'Cancel') + ->assertFocused('@content input.form-control') + ->assertValue('@content input.form-control', 'New Group') + ->type('@content input.form-control', 'Renamed') + ->clickButton('save'); }); - $browser->waitUntilMissing('.ui-dialog'); - $browser->with('#directorylist', function ($browser) { $browser->waitFor('li:first-child ul.groups') ->assertVisible('.treetoggle.expanded') @@ -165,16 +164,15 @@ class GroupsTest extends \Tests\Browser\TestCase $browser->clickMenuItem('group.delete'); }); - $browser->waitFor('.ui-dialog'); - $browser->with('.ui-dialog', function ($browser) { - $browser->assertSeeIn('.ui-dialog-titlebar', 'Are you sure...') - ->assertSeeIn('.ui-dialog-content', 'Do you really want to delete selected group?') - ->assertFocused('button.mainaction.delete') - ->click('button.mainaction.delete'); + $browser->with(new Dialog(), function ($browser) { + $browser + ->assertDialogTitle('Are you sure...') + ->assertDialogContent('Do you really want to delete selected group?') + ->assertButton('delete.mainaction', 'Delete') + ->assertButton('cancel', 'Cancel') + ->clickButton('delete'); }); - $browser->waitUntilMissing('.ui-dialog'); - $browser->with('#directorylist', function ($browser) { $browser->assertMissing('.treetoggle.expanded') ->assertMissing('ul.groups'); diff --git a/tests/Browser/Contacts/ImportTest.php b/tests/Browser/Contacts/ImportTest.php index 48a2e0b24..2d1994497 100644 --- a/tests/Browser/Contacts/ImportTest.php +++ b/tests/Browser/Contacts/ImportTest.php @@ -3,6 +3,7 @@ namespace Tests\Browser\Contacts; use Tests\Browser\Components\App; +use Tests\Browser\Components\Dialog; class ImportTest extends \Tests\Browser\TestCase { @@ -21,9 +22,11 @@ class ImportTest extends \Tests\Browser\TestCase $browser->clickToolbarMenuItem('import'); - $browser->assertSeeIn('.ui-dialog-title', 'Import contacts'); - $browser->assertVisible('.ui-dialog button.mainaction.import'); - $browser->assertVisible('.ui-dialog button.cancel'); + $browser->with(new Dialog(), function ($browser) { + $browser->assertDialogTitle('Import contacts') + ->assertButton('mainaction.import', 'Import') + ->assertButton('cancel', 'Cancel'); + }); $browser->withinFrame('.ui-dialog iframe', function ($browser) { // check task and action @@ -44,8 +47,9 @@ class ImportTest extends \Tests\Browser\TestCase }); // Close the dialog - $browser->click('.ui-dialog button.cancel'); - $browser->assertMissing('.ui-dialog'); + $browser->with(new Dialog(), function ($browser) { + $browser->clickButton('cancel'); + }); }); } @@ -59,27 +63,30 @@ class ImportTest extends \Tests\Browser\TestCase $this->browse(function ($browser) { // Open the dialog again $browser->clickToolbarMenuItem('import'); - $browser->assertSeeIn('.ui-dialog-title', 'Import contacts'); - // Submit the form with no file attached - $browser->click('.ui-dialog button.mainaction'); - $browser->waitForText('Attention'); - $browser->assertSee('Please select a file'); - $browser->driver->getKeyboard()->sendKeys(\Facebook\WebDriver\WebDriverKeys::ESCAPE); - $browser->assertElementsCount('.ui-dialog', 1); - - $browser->withinFrame('.ui-dialog iframe', function ($browser) { - $browser->attach('.custom-file input', TESTS_DIR . 'data/contacts.vcf'); + $browser->with(new Dialog(), function ($browser) { + $browser->assertDialogTitle('Import contacts') + ->clickButton('import'); }); - $browser->click('.ui-dialog button.mainaction'); - - $browser->withinFrame('.ui-dialog iframe', function ($browser) { - $browser->waitForText('Successfully imported 2 contacts:'); + // Submit the form with no file attached + $browser->with(new Dialog(2), function ($browser) { + $browser->assertDialogTitle('Attention') + ->assertDialogContent('Please select a file') + ->assertButton('save.mainaction', 'OK') + ->pressESC(); }); - // Close the dialog - $browser->click('.ui-dialog button.cancel'); + $browser->with(new Dialog(), function ($browser) { + $browser->withinDialogFrame(function ($browser) { + $browser->attach('.custom-file input', TESTS_DIR . 'data/contacts.vcf'); + }) + ->clickButton('import') + ->withinDialogFrame(function ($browser) { + $browser->waitForText('Successfully imported 2 contacts:'); + }) + ->closeDialog(); + }); // Expected existing contacts + imported $browser->waitFor('#contacts-table tr') diff --git a/tests/Browser/Mail/PreviewTest.php b/tests/Browser/Mail/PreviewTest.php index aa33f46da..6b6bbc021 100644 --- a/tests/Browser/Mail/PreviewTest.php +++ b/tests/Browser/Mail/PreviewTest.php @@ -3,6 +3,7 @@ namespace Tests\Browser\Mail; use Tests\Browser\Components\App; +use Tests\Browser\Components\Dialog; use Tests\Browser\Components\Popupmenu; class PreviewTest extends \Tests\Browser\TestCase @@ -128,16 +129,14 @@ class PreviewTest extends \Tests\Browser\TestCase ->click('.header.cc a.morelink'); }); - $browser->waitFor('.ui-dialog') - ->with('.ui-dialog', function ($browser) { - $browser->assertSeeIn('.ui-dialog-titlebar', 'Cc') - ->assertSeeIn('.ui-dialog-content', 'test1@domain.tld') - ->assertSeeIn('.ui-dialog-content', 'test12@domain.tld') - ->assertElementsCount('span.adr', 12) - ->click('.ui-dialog-buttonset button.cancel'); - }); - - $browser->waitUntilMissing('.ui-dialog'); + $browser->with(new Dialog(), function ($browser) { + $browser->assertDialogTitle('Cc') + ->assertDialogContent('test1@domain.tld') + ->assertDialogContent('test12@domain.tld') + ->assertElementsCount('@content span.adr', 12) + ->assertButton('cancel', 'Close') + ->clickButton('cancel'); + }); // Attachments list $browser->withinFrame('#messagecontframe', function ($browser) { diff --git a/tests/Browser/Settings/AboutTest.php b/tests/Browser/Settings/AboutTest.php index c2b9932af..00c3eb561 100644 --- a/tests/Browser/Settings/AboutTest.php +++ b/tests/Browser/Settings/AboutTest.php @@ -3,6 +3,7 @@ namespace Tests\Browser\Settings; use Tests\Browser\Components\App; +use Tests\Browser\Components\Dialog; class AboutTest extends \Tests\Browser\TestCase { @@ -13,8 +14,15 @@ class AboutTest extends \Tests\Browser\TestCase $browser->clickTaskMenuItem('about'); - $browser->assertSeeIn('.ui-dialog-title', 'About'); - $browser->assertVisible('.ui-dialog #aboutframe'); + $browser->with(new Dialog(), function ($browser) { + $browser->assertDialogTitle('About') + ->assertButton('cancel', 'Close') + ->assertVisible('@content #aboutframe'); + + if ($url = \rcmail::get_instance()->config->get('support_url')) { + $browser->assertButton('mainaction.help', 'Get support'); + } + }); $browser->withinFrame('#aboutframe', function ($browser) { // check task and action @@ -26,6 +34,10 @@ class AboutTest extends \Tests\Browser\TestCase $browser->assertSee($this->app->config->get('product_name')); $browser->assertVisible('#pluginlist'); }); + + $browser->with(new Dialog(), function ($browser) { + $browser->closeDialog(); + }); }); } } diff --git a/tests/Browser/Settings/ResponsesTest.php b/tests/Browser/Settings/ResponsesTest.php index ef006f59d..0a13dd8dd 100644 --- a/tests/Browser/Settings/ResponsesTest.php +++ b/tests/Browser/Settings/ResponsesTest.php @@ -3,6 +3,7 @@ namespace Tests\Browser\Settings; use Tests\Browser\Components\App; +use Tests\Browser\Components\Dialog; use Tests\Browser\Components\Popupmenu; class ResponsesTest extends \Tests\Browser\TestCase @@ -124,15 +125,15 @@ class ResponsesTest extends \Tests\Browser\TestCase $this->browse(function ($browser) { $browser->clickToolbarMenuItem('delete'); - $browser->waitFor('.ui-dialog') - ->with('.ui-dialog', function ($browser) { - $browser->assertSeeIn('.ui-dialog-title', 'Are you sure...') - ->assertSeeIn('.ui-dialog-content', 'Do you really want to delete this response text?') - ->click('.ui-dialog-buttonpane button.mainaction.delete'); - }); + $browser->with(new Dialog(), function ($browser) { + $browser->assertDialogTitle('Are you sure...') + ->assertDialogContent('Do you really want to delete this response text?') + ->assertButton('mainaction.delete', 'Delete') + ->assertButton('cancel', 'Cancel') + ->clickButton('mainaction.delete'); + }); $browser->waitForMessage('confirmation', 'Successfully deleted.') - ->assertMissing('.ui-dialog') ->closeMessage('confirmation'); // Preview frame should reset to the watermark page diff --git a/tests/Browser/bootstrap.php b/tests/Browser/bootstrap.php index a7b0ccdb5..80e89452a 100644 --- a/tests/Browser/bootstrap.php +++ b/tests/Browser/bootstrap.php @@ -37,6 +37,7 @@ define('TESTS_PASS', $rcmail->config->get('tests_password')); require_once(__DIR__ . '/Browser.php'); require_once(__DIR__ . '/TestCase.php'); require_once(__DIR__ . '/Components/App.php'); +require_once(__DIR__ . '/Components/Dialog.php'); require_once(__DIR__ . '/Components/Popupmenu.php'); require_once(__DIR__ . '/Components/Taskmenu.php'); require_once(__DIR__ . '/Components/Toolbarmenu.php');