Skip to content

Commit

Permalink
fix: for e2e testing ensure login working prior to doing subsequent t…
Browse files Browse the repository at this point in the history
…esting (openemr#7756)
  • Loading branch information
bradymiller authored Oct 5, 2024
1 parent 30a9ace commit 968ba9c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 40 deletions.
62 changes: 45 additions & 17 deletions tests/Tests/E2e/CheckMainMenuLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,34 @@ class CheckMainMenuLinksTest extends PantherTestCase
*/
private $e2eBaseUrl;

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();
}

/**
* @dataProvider menuLinkProvider
* @depends testLogin
*/
public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): void
{
Expand All @@ -30,18 +51,13 @@ public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): v
$this->markTestSkipped('Test skipped because this environment does not support high enough nodejs version.');
}
$openEmrPage = $this->e2eBaseUrl;
$client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$client->manage()->window()->maximize();
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$this->client->manage()->window()->maximize();
try {
// login
$crawler = $client->request('GET', '/interface/login/login.php?site=default');
$form = $crawler->filter('#login_form')->form();
$form['authUser'] = 'admin';
$form['clearPass'] = 'pass';
$crawler = $client->submit($form);
$this->login('admin', 'pass');
// check if the menu cog is showing. if so, then click it.
if ($crawler->filterXPath('//div[@id="mainBox"]/nav/button[@data-target="#mainMenu"]')->isDisplayed()) {
$crawler->filterXPath('//div[@id="mainBox"]/nav/button[@data-target="#mainMenu"]')->click();
if ($this->crawler->filterXPath('//div[@id="mainBox"]/nav/button[@data-target="#mainMenu"]')->isDisplayed()) {
$this->crawler->filterXPath('//div[@id="mainBox"]/nav/button[@data-target="#mainMenu"]')->click();
}
// got to and click the menu link
$menuLinkSequenceArray = explode('||', $menuLink);
Expand All @@ -67,23 +83,23 @@ public function testCheckMenuLink(string $menuLink, string $expectedTabTitle): v
// click the nested menu item
$menuLink = '//div[@id="mainMenu"]/div/div/div/div[text()="' . $menuLinkSequenceArray[0] . '"]/../ul/li/div/div[text()="' . $menuLinkSequenceArray[1] . '"]/../ul/li/div[text()="' . $menuLinkItem . '"]';
}
$client->waitFor($menuLink);
$crawler = $client->refreshCrawler();
$crawler->filterXPath($menuLink)->click();
$this->client->waitFor($menuLink);
$this->crawler = $this->client->refreshCrawler();
$this->crawler->filterXPath($menuLink)->click();
$counter++;
}
// wait for the tab title to be shown
$client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
$this->client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
// Perform the final assertion
$this->assertSame($expectedTabTitle, $crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text());
$this->assertSame($expectedTabTitle, $this->crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text(), 'Page load FAILED');
} catch (\Throwable $e) {
// Close client
$client->quit();
$this->client->quit();
// re-throw the exception
throw $e;
}
// Close client
$client->quit();
$this->client->quit();
}

public static function menuLinkProvider()
Expand Down Expand Up @@ -184,4 +200,16 @@ 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');
}
}
73 changes: 50 additions & 23 deletions tests/Tests/E2e/CheckUserMenuLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,71 @@ class CheckUserMenuLinksTest extends PantherTestCase
*/
private $e2eBaseUrl;

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();
}

/**
* @dataProvider menuLinkProvider
* @depends testLogin
*/
public function testCheckUserLink(string $menutreeicon, string $menuLinkItem, string $expectedTabTitle): void
public function testCheckUserMenuLink(string $menutreeicon, string $menuLinkItem, string $expectedTabTitle): void
{
$openEmrPage = $this->e2eBaseUrl;
$client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$client->manage()->window()->maximize();
$this->client = static::createPantherClient(['external_base_uri' => $openEmrPage]);
$this->client->manage()->window()->maximize();
try {
// login
$crawler = $client->request('GET', '/interface/login/login.php?site=default');
$form = $crawler->filter('#login_form')->form();
$form['authUser'] = 'admin';
$form['clearPass'] = 'pass';
$crawler = $client->submit($form);

$this->login('admin', 'pass');
// got to and click the user menu link
$menuLink = '//i[@id="user_icon"]';
$menuLink2 = '//ul[@id="userdropdown"]//i[contains(@class, "' . $menutreeicon . '")]';
$client->waitFor($menuLink);
$crawler = $client->refreshCrawler();
$crawler->filterXPath($menuLink)->click();
$client->waitFor($menuLink2);
$crawler = $client->refreshCrawler();
$crawler->filterXPath($menuLink2)->click();
$this->client->waitFor($menuLink);
$this->crawler = $this->client->refreshCrawler();
$this->crawler->filterXPath($menuLink)->click();
$this->client->waitFor($menuLink2);
$this->crawler = $this->client->refreshCrawler();
$this->crawler->filterXPath($menuLink2)->click();

// wait for the tab title to be shown
if ($menuLinkItem == 'Logout') {
// special case for Logout
$client->waitFor('//input[@id="authUser"]');
$title = $client->getTitle();
$this->assertSame('OpenEMR Login', $title);
$this->client->waitFor('//input[@id="authUser"]');
$title = $this->client->getTitle();
$this->assertSame('OpenEMR Login', $title, 'Logout FAILED');
} else {
$client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
$this->client->waitForElementToContain("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]", $expectedTabTitle);
// Perform the final assertion
$this->assertSame($expectedTabTitle, $crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text());
$this->assertSame($expectedTabTitle, $this->crawler->filterXPath("//div[@id='tabs_div']/div/div[not(contains(concat(' ',normalize-space(@class),' '),' tabsNoHover '))]")->text(), 'Page load FAILED');
}
} catch (\Throwable $e) {
// Close client
$client->quit();
$this->client->quit();
// re-throw the exception
throw $e;
}
// Close client
$client->quit();
$this->client->quit();
}

public static function menuLinkProvider()
Expand All @@ -76,4 +91,16 @@ 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');
}
}

0 comments on commit 968ba9c

Please sign in to comment.