Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit e6775f1

Browse files
committed
E2E: Added waiting for backend, clearing the user session between scenarios
1 parent 9e98754 commit e6775f1

File tree

4 files changed

+103
-14
lines changed

4 files changed

+103
-14
lines changed

e2e/.rkd/makefile.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ version: org.riotkit.rkd/yaml/v1
22
imports: []
33

44
environment:
5-
BROWSER_BIN: "opera"
5+
BROWSER_BIN: "chromium"
66

77
tasks:
88
:e2e:browser:
99
description: Run Chromium browser
10-
steps: $BROWSER_BIN --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222
10+
steps:
11+
- rm -rf .rkd/browser
12+
- $BROWSER_BIN --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 --user-data-dir=.rkd/browser
1113

1214
:e2e:test:
1315
description: Run End-To-End tests with Behat

e2e/features/UsersManagement.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature: Create users, edit basic fields in existing user profiles, searching the user list
2+
3+
Scenario: Create a example user from web panel
4+
Given I am authenticated as administrator
5+
And I visit users search page
6+
And I press "Add user" button

e2e/features/bootstrap/FeatureContext.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php declare(strict_types=1);
22

3-
use Behat\Behat\Context\Context;
4-
use Behat\Gherkin\Node\PyStringNode;
5-
use Behat\Gherkin\Node\TableNode;
6-
use Behat\MinkExtension\Context\MinkContext;
3+
use Behat\Behat\Hook\Scope\AfterScenarioScope;
4+
use Behat\Mink\Exception\ElementNotFoundException;
75
use PHPUnit\Framework\Assert as Assertions;
86

97
define('SERVER_PATH', __DIR__ . '/../../../server');
@@ -16,14 +14,20 @@
1614
class FeatureContext extends TechnicalContext
1715
{
1816
/**
19-
* Initializes context.
17+
* After each scenario clear the browser session - logout user
2018
*
21-
* Every scenario gets its own context instance.
22-
* You can also pass arbitrary arguments to the
23-
* context constructor through behat.yml.
19+
* @param AfterScenarioScope $event
20+
*
21+
* @AfterScenario
2422
*/
25-
public function __construct()
23+
public function resetCurrentPage(AfterScenarioScope $event)
2624
{
25+
try {
26+
$script = 'sessionStorage.clear(); localStorage.clear();';
27+
$this->getSession()->executeScript($script);
28+
} catch (\Exception $e) {
29+
//
30+
}
2731
}
2832

2933

@@ -64,11 +68,12 @@ public function iCreateAdminAccountAdvanced(string $commandline): void
6468
*
6569
* @param string $user
6670
* @param string $password
71+
* @throws ElementNotFoundException
6772
*/
68-
public function iLoginAs(string $user, string $password)
73+
public function iLoginAs(string $user, string $password): void
6974
{
7075
$this->visit('/');
71-
$this->getSession()->getPage()->find('css', 'a[to="/admin/login"]')->click();
76+
$this->iClickMenuLink('/admin/login');
7277

7378
$this->fillFieldByCSS('input[placeholder="Email*"]', $user);
7479
$this->fillFieldByCSS('input[placeholder="Enter your password*"]', $password);
@@ -78,6 +83,18 @@ public function iLoginAs(string $user, string $password)
7883
sleep(1);
7984
}
8085

86+
/**
87+
* @When I click menu link leading to :path
88+
*
89+
* @param string $path
90+
* @throws ElementNotFoundException
91+
*/
92+
public function iClickMenuLink(string $path): void
93+
{
94+
$this->findByCSS('a[to="' . $path . '"]')->click();
95+
$this->iWait();
96+
}
97+
8198
/**
8299
* @Then I expect to be logged in
83100
*/
@@ -96,4 +113,22 @@ public function expectToSeeApplicationFooter(): void
96113
Assertions::assertMatchesRegularExpression('/Backup Repository ([0-9\.\-a-z]+) running on ([a-z]+)/', $footer ? $footer->getText() : '');
97114
Assertions::assertTrue($footer && $footer->isVisible());
98115
}
116+
117+
/**
118+
* @Given I am authenticated as administrator
119+
*/
120+
public function iAuthAsAdmin(): void
121+
{
122+
$this->iCreateAdminAccountWithEMail('[email protected]', 'you-cant-break-it');
123+
$this->iLoginAs('[email protected]', 'you-cant-break-it');
124+
}
125+
126+
/**
127+
* @When I visit users search page
128+
* @throws ElementNotFoundException
129+
*/
130+
public function iVisitUsersSearchPage(): void
131+
{
132+
$this->iClickMenuLink('/admin/users');
133+
}
99134
}

e2e/features/bootstrap/TechnicalContext.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php declare(strict_types=1);
22

3+
use Behat\Mink\Element\NodeElement;
4+
use Behat\Mink\Exception\ElementNotFoundException;
35
use Behat\MinkExtension\Context\MinkContext;
46
use PHPUnit\Framework\Assert as Assertions;
57

@@ -46,7 +48,19 @@ public function iAssertShellCommandExitedWith(int $exitCode): void
4648
// Browser
4749
//
4850

49-
protected function fillFieldByCSS(string $selector, string $value)
51+
protected function iWait(): void
52+
{
53+
$this->getSession()->getPage()->waitFor(
54+
30,
55+
function () {
56+
$element = $this->getSession()->getPage()->find('css', 'div[class="loader-overlay"]');
57+
58+
return !$element || !$element->isVisible();
59+
}
60+
);
61+
}
62+
63+
protected function fillFieldByCSS(string $selector, string $value): void
5064
{
5165
$input = $this->getSession()->getPage()->find('css', $selector);
5266
$input->click();
@@ -55,4 +69,36 @@ protected function fillFieldByCSS(string $selector, string $value)
5569
$input->blur();
5670
$input->click();
5771
}
72+
73+
/**
74+
* @When I press :buttonTitle button
75+
*
76+
* @param string $buttonTitle
77+
*/
78+
public function iPressButton(string $buttonTitle): void
79+
{
80+
$this->pressButton($buttonTitle);
81+
}
82+
83+
protected function findByCSS(string $selector, int $timeout = 10): NodeElement
84+
{
85+
$callback = function () use ($selector) {
86+
return $this->getSession()->getPage()->find('css', $selector);
87+
};
88+
89+
do {
90+
$element = $callback();
91+
92+
if ($element instanceof NodeElement) {
93+
return $element;
94+
}
95+
96+
$timeout--;
97+
sleep(1);
98+
99+
} while (!$element instanceof NodeElement && $timeout > 0);
100+
101+
sleep(5);
102+
throw new ElementNotFoundException($this->getSession()->getDriver(), null, 'css', $selector);
103+
}
58104
}

0 commit comments

Comments
 (0)