|
13 | 13 | */
|
14 | 14 | trait ElementTrait {
|
15 | 15 |
|
| 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 | + |
16 | 85 | /**
|
17 | 86 | * Assert an element with selector and attribute with a value exists.
|
18 | 87 | *
|
|
0 commit comments