Skip to content

Commit 402c607

Browse files
authoredOct 6, 2024
feat: incorporate traits into e2e ci testing (openemr#7757)
1 parent 968ba9c commit 402c607

10 files changed

+334
-357
lines changed
 

‎tests/Tests/E2e/Base/BaseTrait.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* BaseTrait trait
5+
*
6+
* @package OpenEMR
7+
* @link https://www.open-emr.org
8+
* @author Brady Miller <brady.g.miller@gmail.com>
9+
* @copyright Copyright (c) 2024 Brady Miller <brady.g.miller@gmail.com>
10+
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace OpenEMR\Tests\E2e\Base;
16+
17+
use OpenEMR\Tests\E2e\Xpaths\XpathsConstants;
18+
19+
trait BaseTrait
20+
{
21+
protected function base(): void
22+
{
23+
$e2eBaseUrl = getenv("OPENEMR_BASE_URL_E2E", true) ?: "http://localhost";
24+
$this->client = static::createPantherClient(['external_base_uri' => $e2eBaseUrl]);
25+
$this->client->manage()->window()->maximize();
26+
}
27+
28+
protected function switchToIFrame($selector): void
29+
{
30+
$iframe = $this->client->findElement($selector);
31+
$this->client->switchTo()->frame($iframe);
32+
$this->crawler = $this->client->refreshCrawler();
33+
}
34+
35+
protected function assertActiveTab($text): void
36+
{
37+
$startTime = (int) (microtime(true) * 1000);
38+
while (strpos($this->crawler->filterXPath(XpathsConstants::ACTIVE_TAB)->text(), "Loading") === 0) {
39+
if (($startTime + 10000) < ((int) (microtime(true) * 1000))) {
40+
$this->fail("Timeout waiting for tab [$text]");
41+
}
42+
usleep(100);
43+
}
44+
$this->assertSame($text, $this->crawler->filterXPath(XpathsConstants::ACTIVE_TAB)->text(), "[$text] tab load FAILED");
45+
}
46+
}

‎tests/Tests/E2e/CheckMainMenuLinksTest.php

+19-47
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
<?php
22

3+
/**
4+
* CheckMainMenuLinksTest class
5+
*
6+
* @package OpenEMR
7+
* @link https://www.open-emr.org
8+
* @author Brady Miller <brady.g.miller@gmail.com>
9+
* @copyright Copyright (c) 2024 Brady Miller <brady.g.miller@gmail.com>
10+
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
11+
*/
12+
313
declare(strict_types=1);
414

515
namespace OpenEMR\Tests\E2e;
616

17+
use OpenEMR\Tests\E2e\Base\BaseTrait;
18+
use OpenEMR\Tests\E2e\Login\LoginTrait;
719
use Symfony\Component\Panther\PantherTestCase;
820
use Symfony\Component\Panther\Client;
921

1022
class CheckMainMenuLinksTest extends PantherTestCase
1123
{
12-
/**
13-
* The base url used for e2e (end to end) browser testing.
14-
*/
15-
private $e2eBaseUrl;
24+
use BaseTrait;
25+
use LoginTrait;
1626

17-
private $client;
18-
private $crawler;
19-
20-
protected function setUp(): void
21-
{
22-
$this->e2eBaseUrl = getenv("OPENEMR_BASE_URL_E2E", true) ?: "http://localhost";
23-
}
24-
25-
public function testLogin(): void
26-
{
27-
$openEmrPage = $this->e2eBaseUrl;
28-
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
29-
$this->client->manage()->window()->maximize();
30-
try {
31-
$this->login('admin', 'pass');
32-
} catch (\Throwable $e) {
33-
// Close client
34-
$this->client->quit();
35-
// re-throw the exception
36-
throw $e;
37-
}
38-
// Close client
39-
$this->client->quit();
40-
}
27+
protected $client;
28+
protected $crawler;
4129

4230
/**
4331
* @dataProvider menuLinkProvider
44-
* @depends testLogin
32+
* @depends testLoginAuthorized
4533
*/
4634
public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): void
4735
{
@@ -50,9 +38,7 @@ public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): v
5038
// a high enough version of nodejs)
5139
$this->markTestSkipped('Test skipped because this environment does not support high enough nodejs version.');
5240
}
53-
$openEmrPage = $this->e2eBaseUrl;
54-
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
55-
$this->client->manage()->window()->maximize();
41+
$this->base();
5642
try {
5743
$this->login('admin', 'pass');
5844
// check if the menu cog is showing. if so, then click it.
@@ -89,9 +75,7 @@ public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): v
8975
$counter++;
9076
}
9177
// wait for the tab title to be shown
92-
$this->client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
93-
// Perform the final assertion
94-
$this->assertSame($expectedTabTitle, $this->crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text(), 'Page load FAILED');
78+
$this->assertActiveTab($expectedTabTitle);
9579
} catch (\Throwable $e) {
9680
// Close client
9781
$this->client->quit();
@@ -200,16 +184,4 @@ public static function menuLinkProvider()
200184
'Miscellaneous -> New Documents menu link' => ['Miscellaneous||New Documents', 'Documents']
201185
];
202186
}
203-
204-
private function login(string $name, string $password): void
205-
{
206-
// login
207-
$this->crawler = $this->client->request('GET', '/interface/login/login.php?site=default');
208-
$form = $this->crawler->filter('#login_form')->form();
209-
$form['authUser'] = $name;
210-
$form['clearPass'] = $password;
211-
$this->crawler = $this->client->submit($form);
212-
$title = $this->client->getTitle();
213-
$this->assertSame('OpenEMR', $title, 'Login FAILED');
214-
}
215187
}
+19-47
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
<?php
22

3+
/**
4+
* CheckUserMenuLinksTest class
5+
*
6+
* @package OpenEMR
7+
* @link https://www.open-emr.org
8+
* @author Brady Miller <brady.g.miller@gmail.com>
9+
* @copyright Copyright (c) 2024 Brady Miller <brady.g.miller@gmail.com>
10+
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
11+
*/
12+
313
declare(strict_types=1);
414

515
namespace OpenEMR\Tests\E2e;
616

17+
use OpenEMR\Tests\E2e\Base\BaseTrait;
18+
use OpenEMR\Tests\E2e\Login\LoginTrait;
719
use Symfony\Component\Panther\PantherTestCase;
820
use Symfony\Component\Panther\Client;
921

1022
class CheckUserMenuLinksTest extends PantherTestCase
1123
{
12-
/**
13-
* The base url used for e2e (end to end) browser testing.
14-
*/
15-
private $e2eBaseUrl;
24+
use BaseTrait;
25+
use LoginTrait;
1626

17-
private $client;
18-
private $crawler;
19-
20-
protected function setUp(): void
21-
{
22-
$this->e2eBaseUrl = getenv("OPENEMR_BASE_URL_E2E", true) ?: "http://localhost";
23-
}
24-
25-
public function testLogin(): void
26-
{
27-
$openEmrPage = $this->e2eBaseUrl;
28-
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
29-
$this->client->manage()->window()->maximize();
30-
try {
31-
$this->login('admin', 'pass');
32-
} catch (\Throwable $e) {
33-
// Close client
34-
$this->client->quit();
35-
// re-throw the exception
36-
throw $e;
37-
}
38-
// Close client
39-
$this->client->quit();
40-
}
27+
protected $client;
28+
protected $crawler;
4129

4230
/**
4331
* @dataProvider menuLinkProvider
44-
* @depends testLogin
32+
* @depends testLoginAuthorized
4533
*/
4634
public function testCheckUserMenuLink(string $menutreeicon, string $menuLinkItem, string $expectedTabTitle): void
4735
{
48-
$openEmrPage = $this->e2eBaseUrl;
49-
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
50-
$this->client->manage()->window()->maximize();
36+
$this->base();
5137
try {
5238
$this->login('admin', 'pass');
5339
// got to and click the user menu link
@@ -67,9 +53,7 @@ public function testCheckUserMenuLink(string $menutreeicon, string $menuLinkItem
6753
$title = $this->client->getTitle();
6854
$this->assertSame('OpenEMR Login', $title, 'Logout FAILED');
6955
} else {
70-
$this->client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
71-
// Perform the final assertion
72-
$this->assertSame($expectedTabTitle, $this->crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text(), 'Page load FAILED');
56+
$this->assertActiveTab($expectedTabTitle);
7357
}
7458
} catch (\Throwable $e) {
7559
// Close client
@@ -91,16 +75,4 @@ public static function menuLinkProvider()
9175
'Logout user menu link' => ['fa-sign-out-alt', 'Logout', 'OpenEMR Login']
9276
];
9377
}
94-
95-
private function login(string $name, string $password): void
96-
{
97-
// login
98-
$this->crawler = $this->client->request('GET', '/interface/login/login.php?site=default');
99-
$form = $this->crawler->filter('#login_form')->form();
100-
$form['authUser'] = $name;
101-
$form['clearPass'] = $password;
102-
$this->crawler = $this->client->submit($form);
103-
$title = $this->client->getTitle();
104-
$this->assertSame('OpenEMR', $title, 'Login FAILED');
105-
}
10678
}

0 commit comments

Comments
 (0)