Page MenuHomePhorge

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/tests/Browser/Browser.php b/tests/Browser/Browser.php
index 91ff17b15..03fe8d195 100644
--- a/tests/Browser/Browser.php
+++ b/tests/Browser/Browser.php
@@ -1,248 +1,227 @@
<?php
namespace Tests\Browser;
use Facebook\WebDriver\WebDriverKeys;
use PHPUnit\Framework\Assert;
use Tests\Browser\Components;
/**
* Laravel Dusk Browser extensions
*/
class Browser extends \Laravel\Dusk\Browser
{
/**
* Assert specified rcmail.env value
*/
public function assertEnvEquals($key, $expected)
{
$this->assertEquals($expected, $this->getEnv($key));
return $this;
}
/**
* Assert specified checkbox state
*/
public function assertCheckboxState($selector, $state)
{
if ($state) {
$this->assertChecked($selector);
}
else {
$this->assertNotChecked($selector);
}
return $this;
}
/**
* Assert Task menu state
*/
public function assertTaskMenu($selected)
{
$this->with(new Components\Taskmenu(), function ($browser) use ($selected) {
$browser->assertMenuState($selected);
});
return $this;
}
/**
* Assert toolbar menu state
*/
public function assertToolbarMenu($active, $disabled)
{
$this->with(new Components\Toolbarmenu(), function ($browser) use ($active, $disabled) {
$browser->assertMenuState($active, $disabled);
});
return $this;
}
/**
* Close toolbar menu (on phones)
*/
public function closeToolbarMenu()
{
$this->with(new Components\Toolbarmenu(), function ($browser) {
$browser->closeMenu();
});
return $this;
}
/**
* Select taskmenu item
*/
public function clickTaskMenuItem($name)
{
$this->with(new Components\Taskmenu(), function ($browser) use ($name) {
$browser->clickMenuItem($name);
});
return $this;
}
/**
* Select toolbar menu item
*/
public function clickToolbarMenuItem($name, $dropdown_action = null)
{
$this->with(new Components\Toolbarmenu(), function ($browser) use ($name, $dropdown_action) {
$browser->clickMenuItem($name, $dropdown_action);
});
return $this;
}
/**
* Shortcut to click an element while holding CTRL key
*/
public function ctrlClick($selector)
{
$this->driver->getKeyboard()->pressKey(WebDriverKeys::LEFT_CONTROL);
$this->element($selector)->click();
$this->driver->getKeyboard()->releaseKey(WebDriverKeys::LEFT_CONTROL);
}
- /**
- * Log in the test user
- */
- public function doLogin()
- {
- $this->type('_user', TESTS_USER);
- $this->type('_pass', TESTS_PASS);
- $this->click('button[type="submit"]');
-
- // wait after successful login
- $this->waitUntil('!rcmail.busy');
-
- return $this;
- }
-
/**
* Visit specified task/action with logon if needed
*/
public function go($task = 'mail', $action = null, $login = true)
{
- $this->visit("/?_task=$task&_action=$action");
-
- // check if we have a valid session
- if ($login) {
- $app = new Components\App();
- if ($app->getEnv($this, 'task') == 'login') {
- $this->doLogin();
- }
- }
+ $this->with(new Components\App(), function ($browser) use ($task, $action, $login) {
+ $browser->gotoAction($task, $action, $login);
+ });
return $this;
}
/**
* Check if in Phone mode
*/
public static function isPhone()
{
return getenv('TESTS_MODE') == 'phone';
}
/**
* Check if in Tablet mode
*/
public static function isTablet()
{
return getenv('TESTS_MODE') == 'tablet';
}
/**
* Check if in Desktop mode
*/
public static function isDesktop()
{
return !self::isPhone() && !self::isTablet();
}
/**
* Change state of the Elastic's pretty checkbox
*/
public function setCheckboxState($selector, $state)
{
// Because you can't operate on the original checkbox directly
$this->ensurejQueryIsAvailable();
if ($state) {
$run = "if (!element.prev().is(':checked')) element.click()";
}
else {
$run = "if (element.prev().is(':checked')) element.click()";
}
$this->script(
"var element = jQuery('$selector')[0] || jQuery('input[name=$selector]')[0];"
."element = jQuery(element).next('.custom-control-label'); $run;"
);
return $this;
}
/**
* Returns content of a downloaded file
*/
public function readDownloadedFile($filename)
{
$filename = TESTS_DIR . "downloads/$filename";
// Give the browser a chance to finish download
if (!file_exists($filename)) {
sleep(2);
}
Assert::assertFileExists($filename);
return file_get_contents($filename);
}
/**
* Removes downloaded file
*/
public function removeDownloadedFile($filename)
{
@unlink(TESTS_DIR . "downloads/$filename");
return $this;
}
/**
* Wait for UI (notice/confirmation/loading/error/warning) message
* and assert it's text
*/
public function waitForMessage($type, $text)
{
$selector = '#messagestack > div.' . $type;
$this->waitFor($selector)->assertSeeIn($selector, $text);
return $this;
}
/**
* Execute code within body context.
* Useful to execute code that selects elements outside of a component context
*/
public function withinBody($callback)
{
if ($this->resolver->prefix != 'body') {
$orig_prefix = $this->resolver->prefix;
$this->resolver->prefix = 'body';
}
call_user_func($callback, $this);
if (isset($orig_prefix)) {
$this->resolver->prefix = $orig_prefix;
}
return $this;
}
}
diff --git a/tests/Browser/Components/App.php b/tests/Browser/Components/App.php
index fbc2744de..76ce633f9 100644
--- a/tests/Browser/Components/App.php
+++ b/tests/Browser/Components/App.php
@@ -1,87 +1,114 @@
<?php
namespace Tests\Browser\Components;
use Laravel\Dusk\Component as BaseComponent;
use PHPUnit\Framework\Assert;
use Tests\Browser\Browser;
class App extends BaseComponent
{
/**
* Get the root selector for the component.
*
* @return string
*/
public function selector()
{
return '';
}
/**
* Assert that the browser page contains the component.
*
* @param Browser $browser
*
* @return void
*/
public function assert($browser)
{
- $result = $browser->script("return typeof(window.rcmail)");
-
- Assert::assertEquals('object', $result[0]);
+ // Assume the app (window.rcmail) is always available
+ // we can't assert that before we visit the page
+ // i.e. you will not be able to use gotoAction()
+ // method if you try to assert that fact.
}
/**
* Get the element shortcuts for the component.
*
* @return array
*/
public function elements()
{
return [
];
}
/**
* Assert value of rcmail.env entry
*/
public function assertEnv($browser, string $key, $expected)
{
Assert::assertEquals($expected, $this->getEnv($browser, $key));
}
/**
* Assert existence of defined gui_objects
*/
public function assertObjects($browser, array $names)
{
$objects = $this->getObjects($browser);
foreach ($names as $object_name) {
Assert::assertContains($object_name, $objects);
}
}
/**
* Return rcmail.env entry
*/
public function getEnv($browser, $key)
{
$result = $browser->script("return rcmail.env['$key']");
$result = $result[0];
return $result;
}
/**
* Return names of defined gui_objects
*/
public function getObjects($browser)
{
$objects = $browser->script("var i, r = []; for (i in rcmail.gui_objects) r.push(i); return r");
$objects = $objects[0];
return (array) $objects;
}
+
+ /**
+ * Visit specified task/action with logon if needed
+ */
+ public function gotoAction($browser, $task = 'mail', $action = null, $login = true)
+ {
+ $browser->visit("/?_task={$task}&_action={$action}");
+
+ // check if we have a valid session
+ if ($login && $this->getEnv($browser, 'task') == 'login') {
+ $this->doLogin($browser);
+ }
+ }
+
+ /**
+ * Log in the test user
+ */
+ protected function doLogin($browser)
+ {
+ $browser->type('_user', TESTS_USER);
+ $browser->type('_pass', TESTS_PASS);
+ $browser->click('button[type="submit"]');
+
+ // wait after successful login
+ $browser->waitUntil('!rcmail.busy');
+ }
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Mar 19, 8:41 AM (18 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
456953
Default Alt Text
(9 KB)

Event Timeline