Skip to content

Commit b930fd5

Browse files
nsineokdf-nikitaAlexSkrypnyk
authored
[#413] Added assertion for the element after element. (#419)
Co-authored-by: Nikita Sineok <nikita@drupfan.com> Co-authored-by: Alex Skrypnyk <alex@drevops.com>
1 parent c39f3c1 commit b930fd5

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

STEPS.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,34 @@ When I scroll to the element "#footer"
319319

320320
</details>
321321

322+
<details>
323+
<summary><code>@Then the element :selector1 should appear after the element :selector2</code></summary>
324+
325+
<br/>
326+
Assert that one element appears after another on the page
327+
<br/><br/>
328+
329+
```gherkin
330+
Then the element "body" should appear after the element "head"
331+
332+
```
333+
334+
</details>
335+
336+
<details>
337+
<summary><code>@Then the text :text1 should appear after the text :text2</code></summary>
338+
339+
<br/>
340+
Assert that one text string appears after another on the page
341+
<br/><br/>
342+
343+
```gherkin
344+
Then the text "Welcome" should appear after the text "Home"
345+
346+
```
347+
348+
</details>
349+
322350
<details>
323351
<summary><code>@Then the element :selector with the attribute :attribute and the value :value should exist</code></summary>
324352

src/ElementTrait.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,75 @@
1313
*/
1414
trait ElementTrait {
1515

16+
/**
17+
* Assert that one element appears after another on the page.
18+
*
19+
* @code
20+
* Then the element "body" should appear after the element "head"
21+
* @endcode
22+
*
23+
* @Then the element :selector1 should appear after the element :selector2
24+
*/
25+
public function elementAssertAfterElement(string $selector1, string $selector2): void {
26+
$session = $this->getSession();
27+
$page = $session->getPage();
28+
29+
$element1 = $page->find('css', $selector1);
30+
$element2 = $page->find('css', $selector2);
31+
32+
if (!$element1) {
33+
throw new \Exception(sprintf("Element with selector '%s' not found.", $selector1));
34+
}
35+
if (!$element2) {
36+
throw new \Exception(sprintf("Element with selector '%s' not found.", $selector2));
37+
}
38+
39+
$text1 = $element1->getOuterHtml();
40+
$text2 = $element2->getOuterHtml();
41+
$content = $this->getSession()->getPage()->getOuterHtml();
42+
43+
$pos1 = strpos((string) $content, (string) $text1);
44+
$pos2 = strpos((string) $content, (string) $text2);
45+
46+
if ($pos1 === FALSE) {
47+
throw new \Exception(sprintf("Element with selector '%s' not found.", $selector1));
48+
}
49+
if ($pos2 === FALSE) {
50+
throw new \Exception(sprintf("Element with selector '%s' not found.", $selector2));
51+
}
52+
53+
if ($pos1 <= $pos2) {
54+
throw new \Exception(sprintf("Element '%s' appears before '%s'", $selector1, $selector2));
55+
}
56+
}
57+
58+
/**
59+
* Assert that one text string appears after another on the page.
60+
*
61+
* @code
62+
* Then the text "Welcome" should appear after the text "Home"
63+
* @endcode
64+
*
65+
* @Then the text :text1 should appear after the text :text2
66+
*/
67+
public function elementAssertTextAfterText(string $text1, string $text2): void {
68+
$content = $this->getSession()->getPage()->getText();
69+
70+
$pos1 = strpos((string) $content, $text1);
71+
$pos2 = strpos((string) $content, $text2);
72+
73+
if ($pos1 === FALSE) {
74+
throw new \Exception(sprintf("Text was not found: '%s'.", $text1));
75+
}
76+
if ($pos2 === FALSE) {
77+
throw new \Exception(sprintf("Text was not found: '%s'.", $text2));
78+
}
79+
80+
if ($pos1 <= $pos2) {
81+
throw new \Exception(sprintf("Text '%s' appears before '%s'", $text1, $text2));
82+
}
83+
}
84+
1685
/**
1786
* Assert an element with selector and attribute with a value exists.
1887
*

tests/behat/features/element.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,41 @@ Feature: Check that ElementTrait works
341341
"""
342342
Element(s) defined by "#top" selector is displayed within a viewport, but should not be.
343343
"""
344+
345+
@api
346+
Scenario: Text appears after another text
347+
When I go to the homepage
348+
Then the text "Powered by Drupal" should appear after the text "Welcome"
349+
350+
@api
351+
Scenario: Assert "Then the element :selector1 should appear after the element :selector2" works as expected
352+
When I go to the homepage
353+
Then the element "body" should appear after the element "head"
354+
355+
@trait:ElementTrait
356+
Scenario: Assert element order fails when first element is before second
357+
Given some behat configuration
358+
And scenario steps:
359+
"""
360+
When I go to the homepage
361+
Then the element "head" should appear after the element "body"
362+
"""
363+
When I run "behat --no-colors"
364+
Then it should fail with an error:
365+
"""
366+
Element 'head' appears before 'body'
367+
"""
368+
369+
@trait:ElementTrait
370+
Scenario: Assert text order fails when first text is before second
371+
Given some behat configuration
372+
And scenario steps:
373+
"""
374+
When I go to the homepage
375+
Then the text "Welcome" should appear after the text "Powered by Drupal"
376+
"""
377+
When I run "behat --no-colors"
378+
Then it should fail with an error:
379+
"""
380+
Text 'Welcome' appears before 'Powered by Drupal'
381+
"""

0 commit comments

Comments
 (0)