Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

"require": {
"php": ">=8.0",
"behat/behat": "^3.0.13",
"friends-of-behat/mink-extension": "^2.3.1",
"behat/behat": "^4.0",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's clearly a BC break. This extension couldn't be used with Behat 3.

"friends-of-behat/mink-extension": "^3.0",
"justinrainbow/json-schema": "^5.0|^6.0",
"symfony/property-access": "^2.3|^3.0|^4.0|^5.0|^6.0|^7.0",
"symfony/http-foundation": "^2.3|^3.0|^4.0|^5.0|^6.0|^7.0",
"symfony/dom-crawler": "^2.4|^3.0|^4.0|^5.0|^6.0|^7.0"
"symfony/property-access": "^2.3|^3.0|^4.0|^5.0|^6.0|^7.0|^8.0",
"symfony/http-foundation": "^2.3|^3.0|^4.0|^5.0|^6.0|^7.0|^8.0",
"symfony/dom-crawler": "^2.4|^3.0|^4.0|^5.0|^6.0|^7.0|^8.0"
},

"require-dev": {
Expand Down
111 changes: 39 additions & 72 deletions src/Context/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Gherkin\Node\TableNode;
use Behat\Hook\AfterScenario;
use Behat\Hook\BeforeScenario;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Exception\ResponseTextException;
use Behat\Step\Given;
use Behat\Step\Then;
use Behat\Step\When;
use WebDriver\Exception\StaleElementReference;

class BrowserContext extends BaseContext
Expand All @@ -20,41 +25,34 @@ public function __construct($timeout = 1)
$this->timeout = $timeout;
}

/**
* @AfterScenario
*/
#[AfterScenario]
public function closeBrowser(): void
{
if ($this->getMink()->isSessionStarted()) {
$this->getSession()->stop();
}
}

/**
* @BeforeScenario
*
* @When (I )start timing now
*/
#[BeforeScenario]
#[When('(I )start timing now')]
public function startTimer(): void
{
$this->timerStartedAt = time();
}

/**
* Set login / password for next HTTP authentication.
*
* @When (I )set basic authentication with :user and :password
*/
#[When('(I )set basic authentication with :user and :password')]
public function iSetBasicAuthenticationWithAnd($user, $password): void
{
$this->getSession()->setBasicAuth($user, $password);
}

/**
* Open url with various parameters.
*
* @Given (I )am on url composed by:
*/
#[Given('(I )am on url composed by:')]
public function iAmOnUrlComposedBy(TableNode $tableNode)
{
$url = '';
Expand All @@ -68,9 +66,8 @@ public function iAmOnUrlComposedBy(TableNode $tableNode)

/**
* Clicks on the nth CSS element.
*
* @When (I )click on the :index :element element
*/
#[When('(I )click on the :index :element element')]
public function iClickOnTheNthElement($index, $element): void
{
$node = $this->findElement('css', $element, $index);
Expand All @@ -79,9 +76,8 @@ public function iClickOnTheNthElement($index, $element): void

/**
* Click on the nth specified link.
*
* @When (I )follow the :index :link link
*/
#[When('(I )follow the :index :link link')]
public function iFollowTheNthLink($index, $link): void
{
$node = $this->findElement('named', ['link', $link], $index);
Expand All @@ -90,9 +86,8 @@ public function iFollowTheNthLink($index, $link): void

/**
* Presses the nth specified button.
*
* @When (I )press the :index :button button
*/
#[When('(I )press the :index :button button')]
public function pressTheNthButton($index, $button): void
{
$node = $this->findElement('named', ['button', $button], $index);
Expand All @@ -101,19 +96,17 @@ public function pressTheNthButton($index, $button): void

/**
* Fills in form field with current date.
*
* @When (I )fill in :field with the current date
*/
#[When('(I )fill in :field with the current date')]
public function iFillInWithTheCurrentDate($field)
{
return $this->iFillInWithTheCurrentDateAndModifier($field, 'now');
}

/**
* Fills in form field with current date and strtotime modifier.
*
* @When (I )fill in :field with the current date and modifier :modifier
*/
#[When('(I )fill in :field with the current date and modifier :modifier')]
public function iFillInWithTheCurrentDateAndModifier($field, $modifier)
{
return $this->getMinkContext()
Expand All @@ -122,9 +115,8 @@ public function iFillInWithTheCurrentDateAndModifier($field, $modifier)

/**
* Mouse over a CSS element.
*
* @When (I )hover :element
*/
#[When('(I )hover :element')]
public function iHoverIShouldSeeIn($element): void
{
$node = $this->getSession()->getPage()->find('css', $element);
Expand All @@ -136,9 +128,8 @@ public function iHoverIShouldSeeIn($element): void

/**
* Save value of the field in parameters array.
*
* @When (I )save the value of :field in the :parameter parameter
*/
#[When('(I )save the value of :field in the :parameter parameter')]
public function iSaveTheValueOfInTheParameter($field, $parameter): void
{
$field = str_replace('\\"', '"', $field);
Expand All @@ -152,19 +143,17 @@ public function iSaveTheValueOfInTheParameter($field, $parameter): void

/**
* Checks, that the page should contains specified text after given timeout.
*
* @Then (I )wait :count second(s) until I see :text
*/
#[Then('(I )wait :count second(s) until I see :text')]
public function iWaitSecondsUntilISee($count, $text): void
{
$this->iWaitSecondsUntilISeeInTheElement($count, $text, 'html');
}

/**
* Checks, that the page should not contain specified text before given timeout.
*
* @Then (I )should not see :text within :count second(s)
*/
#[Then('(I )should not see :text within :count second(s)')]
public function iDontSeeInSeconds($count, $text): void
{
$caught = false;
Expand All @@ -179,19 +168,17 @@ public function iDontSeeInSeconds($count, $text): void

/**
* Checks, that the page should contains specified text after timeout.
*
* @Then (I )wait until I see :text
*/
#[Then('(I )wait until I see :text')]
public function iWaitUntilISee($text): void
{
$this->iWaitSecondsUntilISee($this->timeout, $text);
}

/**
* Checks, that the element contains specified text after timeout.
*
* @Then (I )wait :count second(s) until I see :text in the :element element
*/
#[Then('(I )wait :count second(s) until I see :text in the :element element')]
public function iWaitSecondsUntilISeeInTheElement($count, $text, $element): void
{
$startTime = time();
Expand Down Expand Up @@ -219,39 +206,34 @@ public function iWaitSecondsUntilISeeInTheElement($count, $text, $element): void
$this->assertContains($expected, $node->getText(), $message);
}

/**
* @Then (I )wait :count second(s)
*/
#[Then('(I )wait :count second(s)')]
public function iWaitSeconds($count): void
{
usleep((int) ($count * 1000000));
}

/**
* Checks, that the element contains specified text after timeout.
*
* @Then (I )wait until I see :text in the :element element
*/
#[Then('(I )wait until I see :text in the :element element')]
public function iWaitUntilISeeInTheElement($text, $element): void
{
$this->iWaitSecondsUntilISeeInTheElement($this->timeout, $text, $element);
}

/**
* Checks, that the page should contains specified element after timeout.
*
* @Then (I )wait for :element element
*/
#[Then('(I )wait for :element element')]
public function iWaitForElement($element): void
{
$this->iWaitSecondsForElement($this->timeout, $element);
}

/**
* Wait for a element.
*
* @Then (I )wait :count second(s) for :element element
*/
#[Then('(I )wait :count second(s) for :element element')]
public function iWaitSecondsForElement($count, $element): void
{
$found = false;
Expand All @@ -275,9 +257,7 @@ public function iWaitSecondsForElement($count, $element): void
}
}

/**
* @Then /^(?:|I )should see (?P<count>\d+) "(?P<element>[^"]*)" in the (?P<index>\d+)(?:st|nd|rd|th) "(?P<parent>[^"]*)"$/
*/
#[Then('/^(?:|I )should see (?P<count>\d+) "(?P<element>[^"]*)" in the (?P<index>\d+)(?:st|nd|rd|th) "(?P<parent>[^"]*)"$/')]
public function iShouldSeeNElementInTheNthParent($count, $element, $index, $parent): void
{
$actual = $this->countElements($element, $index, $parent);
Expand All @@ -286,9 +266,7 @@ public function iShouldSeeNElementInTheNthParent($count, $element, $index, $pare
}
}

/**
* @Then (I )should see less than :count :element in the :index :parent
*/
#[Then('(I )should see less than :count :element in the :index :parent')]
public function iShouldSeeLessThanNElementInTheNthParent($count, $element, $index, $parent): void
{
$actual = $this->countElements($element, $index, $parent);
Expand All @@ -297,9 +275,7 @@ public function iShouldSeeLessThanNElementInTheNthParent($count, $element, $inde
}
}

/**
* @Then (I )should see more than :count :element in the :index :parent
*/
#[Then('(I )should see more than :count :element in the :index :parent')]
public function iShouldSeeMoreThanNElementInTheNthParent($count, $element, $index, $parent): void
{
$actual = $this->countElements($element, $index, $parent);
Expand All @@ -310,9 +286,8 @@ public function iShouldSeeMoreThanNElementInTheNthParent($count, $element, $inde

/**
* Checks, that element with given CSS is enabled.
*
* @Then the element :element should be enabled
*/
#[Then('the element :element should be enabled')]
public function theElementShouldBeEnabled($element): void
{
$node = $this->getSession()->getPage()->find('css', $element);
Expand All @@ -327,9 +302,8 @@ public function theElementShouldBeEnabled($element): void

/**
* Checks, that element with given CSS is disabled.
*
* @Then the element :element should be disabled
*/
#[Then('the element :element should be disabled')]
public function theElementShouldBeDisabled($element): void
{
$this->not(function () use ($element): void {
Expand All @@ -339,9 +313,8 @@ public function theElementShouldBeDisabled($element): void

/**
* Checks, that given select box contains the specified option.
*
* @Then the :select select box should contain :option
*/
#[Then('the :select select box should contain :option')]
public function theSelectBoxShouldContain($select, $option): void
{
$select = str_replace('\\"', '"', $select);
Expand All @@ -359,9 +332,8 @@ public function theSelectBoxShouldContain($select, $option): void

/**
* Checks, that given select box does not contain the specified option.
*
* @Then the :select select box should not contain :option
*/
#[Then('the :select select box should not contain :option')]
public function theSelectBoxShouldNotContain($select, $option): void
{
$this->not(function () use ($select, $option): void {
Expand All @@ -371,9 +343,8 @@ public function theSelectBoxShouldNotContain($select, $option): void

/**
* Checks, that the specified CSS element is visible.
*
* @Then the :element element should be visible
*/
#[Then('the :element element should be visible')]
public function theElementShouldBeVisible($element): void
{
$displayedNode = $this->getSession()->getPage()->find('css', $element);
Expand All @@ -387,9 +358,8 @@ public function theElementShouldBeVisible($element): void

/**
* Checks, that the specified CSS element is not visible.
*
* @Then the :element element should not be visible
*/
#[Then('the :element element should not be visible')]
public function theElementShouldNotBeVisible($element): void
{
$exception = new \Exception("The element '$element' is visible");
Expand All @@ -401,31 +371,28 @@ public function theElementShouldNotBeVisible($element): void

/**
* Select a frame by its name or ID.
*
* @When (I )switch to iframe :name
* @When (I )switch to frame :name
*/
#[When('(I )switch to iframe :name')]
#[When('(I )switch to frame :name')]
public function switchToIFrame($name): void
{
$this->getSession()->switchToIFrame($name);
}

/**
* Go back to main document frame.
*
* @When (I )switch to main frame
*/
#[When('(I )switch to main frame')]
public function switchToMainFrame(): void
{
$this->getSession()->switchToIFrame();
}

/**
* test time from when the scenario started.
*
* @Then (the )total elapsed time should be :comparison than :expected seconds
* @Then (the )total elapsed time should be :comparison to :expected seconds
*/
#[Then('(the )total elapsed time should be :comparison than :expected seconds')]
#[Then('(the )total elapsed time should be :comparison to :expected seconds')]
public function elapsedTime($comparison, $expected): void
{
$elapsed = time() - $this->timerStartedAt;
Expand Down
4 changes: 2 additions & 2 deletions src/Context/ContextClass/ClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class ClassResolver implements BaseClassResolver
{
public function supportsClass($contextClass)
public function supportsClass($contextClass): bool
{
return str_starts_with($contextClass, 'behatch:context:');
}

public function resolveClass($contextClass)
public function resolveClass($contextClass): string
{
$className = preg_replace_callback('/(^\w|:\w)/', function ($matches) {
return str_replace(':', '\\', strtoupper($matches[0]));
Expand Down
Loading
Loading