From 968ba9c5be881325f2aea2c758f97d1ed4b0e4ce Mon Sep 17 00:00:00 2001 From: Brady Miller Date: Sat, 5 Oct 2024 08:16:38 -0700 Subject: [PATCH] fix: for e2e testing ensure login working prior to doing subsequent testing (#7756) --- tests/Tests/E2e/CheckMainMenuLinksTest.php | 62 +++++++++++++----- tests/Tests/E2e/CheckUserMenuLinksTest.php | 73 +++++++++++++++------- 2 files changed, 95 insertions(+), 40 deletions(-) diff --git a/tests/Tests/E2e/CheckMainMenuLinksTest.php b/tests/Tests/E2e/CheckMainMenuLinksTest.php index 0e8f40da42d..3113d048d2f 100644 --- a/tests/Tests/E2e/CheckMainMenuLinksTest.php +++ b/tests/Tests/E2e/CheckMainMenuLinksTest.php @@ -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 { @@ -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); @@ -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() @@ -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'); + } } diff --git a/tests/Tests/E2e/CheckUserMenuLinksTest.php b/tests/Tests/E2e/CheckUserMenuLinksTest.php index 2c26e3f70e0..749346184de 100644 --- a/tests/Tests/E2e/CheckUserMenuLinksTest.php +++ b/tests/Tests/E2e/CheckUserMenuLinksTest.php @@ -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() @@ -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'); + } }