Skip to content

Commit

Permalink
feat: incorporate traits into e2e ci testing (openemr#7757)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradymiller authored Oct 6, 2024
1 parent 968ba9c commit 402c607
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 357 deletions.
46 changes: 46 additions & 0 deletions tests/Tests/E2e/Base/BaseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* BaseTrait trait
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Brady Miller <[email protected]>
* @copyright Copyright (c) 2024 Brady Miller <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

declare(strict_types=1);

namespace OpenEMR\Tests\E2e\Base;

use OpenEMR\Tests\E2e\Xpaths\XpathsConstants;

trait BaseTrait
{
protected function base(): void
{
$e2eBaseUrl = getenv("OPENEMR_BASE_URL_E2E", true) ?: "http://localhost";
$this->client = static::createPantherClient(['external_base_uri' => $e2eBaseUrl]);
$this->client->manage()->window()->maximize();
}

protected function switchToIFrame($selector): void
{
$iframe = $this->client->findElement($selector);
$this->client->switchTo()->frame($iframe);
$this->crawler = $this->client->refreshCrawler();
}

protected function assertActiveTab($text): void
{
$startTime = (int) (microtime(true) * 1000);
while (strpos($this->crawler->filterXPath(XpathsConstants::ACTIVE_TAB)->text(), "Loading") === 0) {
if (($startTime + 10000) < ((int) (microtime(true) * 1000))) {
$this->fail("Timeout waiting for tab [$text]");
}
usleep(100);
}
$this->assertSame($text, $this->crawler->filterXPath(XpathsConstants::ACTIVE_TAB)->text(), "[$text] tab load FAILED");
}
}
66 changes: 19 additions & 47 deletions tests/Tests/E2e/CheckMainMenuLinksTest.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
<?php

/**
* CheckMainMenuLinksTest class
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Brady Miller <[email protected]>
* @copyright Copyright (c) 2024 Brady Miller <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

declare(strict_types=1);

namespace OpenEMR\Tests\E2e;

use OpenEMR\Tests\E2e\Base\BaseTrait;
use OpenEMR\Tests\E2e\Login\LoginTrait;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\Client;

class CheckMainMenuLinksTest extends PantherTestCase
{
/**
* The base url used for e2e (end to end) browser testing.
*/
private $e2eBaseUrl;
use BaseTrait;
use LoginTrait;

private $client;
private $crawler;

protected function setUp(): void
{
$this->e2eBaseUrl = getenv("OPENEMR_BASE_URL_E2E", true) ?: "http://localhost";
}

public function testLogin(): void
{
$openEmrPage = $this->e2eBaseUrl;
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$this->client->manage()->window()->maximize();
try {
$this->login('admin', 'pass');
} catch (\Throwable $e) {
// Close client
$this->client->quit();
// re-throw the exception
throw $e;
}
// Close client
$this->client->quit();
}
protected $client;
protected $crawler;

/**
* @dataProvider menuLinkProvider
* @depends testLogin
* @depends testLoginAuthorized
*/
public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): void
{
Expand All @@ -50,9 +38,7 @@ public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): v
// a high enough version of nodejs)
$this->markTestSkipped('Test skipped because this environment does not support high enough nodejs version.');
}
$openEmrPage = $this->e2eBaseUrl;
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$this->client->manage()->window()->maximize();
$this->base();
try {
$this->login('admin', 'pass');
// check if the menu cog is showing. if so, then click it.
Expand Down Expand Up @@ -89,9 +75,7 @@ public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): v
$counter++;
}
// wait for the tab title to be shown
$this->client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
// Perform the final assertion
$this->assertSame($expectedTabTitle, $this->crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text(), 'Page load FAILED');
$this->assertActiveTab($expectedTabTitle);
} catch (\Throwable $e) {
// Close client
$this->client->quit();
Expand Down Expand Up @@ -200,16 +184,4 @@ public static function menuLinkProvider()
'Miscellaneous -> New Documents menu link' => ['Miscellaneous||New Documents', 'Documents']
];
}

private function login(string $name, string $password): void
{
// login
$this->crawler = $this->client->request('GET', '/interface/login/login.php?site=default');
$form = $this->crawler->filter('#login_form')->form();
$form['authUser'] = $name;
$form['clearPass'] = $password;
$this->crawler = $this->client->submit($form);
$title = $this->client->getTitle();
$this->assertSame('OpenEMR', $title, 'Login FAILED');
}
}
66 changes: 19 additions & 47 deletions tests/Tests/E2e/CheckUserMenuLinksTest.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,39 @@
<?php

/**
* CheckUserMenuLinksTest class
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Brady Miller <[email protected]>
* @copyright Copyright (c) 2024 Brady Miller <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

declare(strict_types=1);

namespace OpenEMR\Tests\E2e;

use OpenEMR\Tests\E2e\Base\BaseTrait;
use OpenEMR\Tests\E2e\Login\LoginTrait;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\Client;

class CheckUserMenuLinksTest extends PantherTestCase
{
/**
* The base url used for e2e (end to end) browser testing.
*/
private $e2eBaseUrl;
use BaseTrait;
use LoginTrait;

private $client;
private $crawler;

protected function setUp(): void
{
$this->e2eBaseUrl = getenv("OPENEMR_BASE_URL_E2E", true) ?: "http://localhost";
}

public function testLogin(): void
{
$openEmrPage = $this->e2eBaseUrl;
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$this->client->manage()->window()->maximize();
try {
$this->login('admin', 'pass');
} catch (\Throwable $e) {
// Close client
$this->client->quit();
// re-throw the exception
throw $e;
}
// Close client
$this->client->quit();
}
protected $client;
protected $crawler;

/**
* @dataProvider menuLinkProvider
* @depends testLogin
* @depends testLoginAuthorized
*/
public function testCheckUserMenuLink(string $menutreeicon, string $menuLinkItem, string $expectedTabTitle): void
{
$openEmrPage = $this->e2eBaseUrl;
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$this->client->manage()->window()->maximize();
$this->base();
try {
$this->login('admin', 'pass');
// got to and click the user menu link
Expand All @@ -67,9 +53,7 @@ public function testCheckUserMenuLink(string $menutreeicon, string $menuLinkItem
$title = $this->client->getTitle();
$this->assertSame('OpenEMR Login', $title, 'Logout FAILED');
} else {
$this->client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
// Perform the final assertion
$this->assertSame($expectedTabTitle, $this->crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text(), 'Page load FAILED');
$this->assertActiveTab($expectedTabTitle);
}
} catch (\Throwable $e) {
// Close client
Expand All @@ -91,16 +75,4 @@ public static function menuLinkProvider()
'Logout user menu link' => ['fa-sign-out-alt', 'Logout', 'OpenEMR Login']
];
}

private function login(string $name, string $password): void
{
// login
$this->crawler = $this->client->request('GET', '/interface/login/login.php?site=default');
$form = $this->crawler->filter('#login_form')->form();
$form['authUser'] = $name;
$form['clearPass'] = $password;
$this->crawler = $this->client->submit($form);
$title = $this->client->getTitle();
$this->assertSame('OpenEMR', $title, 'Login FAILED');
}
}
Loading

0 comments on commit 402c607

Please sign in to comment.