Skip to content

Commit ec3f086

Browse files
committed
MQE-2034: Document Custom Helper functionality
1 parent 0758ca0 commit ec3f086

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

docs/custom-helpers.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Custom Helpers
2+
3+
<div class="bs-callout bs-callout-warning" markdown="1">
4+
Due to complexity, you should only write new Custom Helpers as a last resort after trying to implement your test using built-in actions.
5+
</div>
6+
7+
Custom Helpers allow test writers to write custom test actions to solve for advanced requirements beyond what MFTF offers out of the box.
8+
9+
In MFTF version 3.0.0, we removed the following test actions:
10+
11+
* `<executeInSelenium>`
12+
* `<performOn>`
13+
14+
These actions were removed because they allowed custom PHP code to be written inline inside of XML files. This code was difficult to read. It had no proper syntax highlighting and no linting. It was difficult to maintain, troubleshoot, and modify.
15+
16+
However, sometimes custom logic beyond what MFTF offers is necessary so we have provided an alternative solution: the `<helper>` action.
17+
18+
## Example
19+
20+
Custom Helpers are implemented in PHP files that must be placed in this directory:
21+
```
22+
<ModuleName>/Test/Mftf/Helper
23+
```
24+
25+
Let's take a look at one. This Custom Helper selects text on the page by this approach:
26+
27+
1. Move to a very specific X,Y starting position
28+
2. Click and hold the mouse button down
29+
3. Move to another specific X,Y position
30+
4. Release the mouse button
31+
32+
This functionality is used to select text on the page and cannot be accomplished using built-in test actions.
33+
34+
### PHP File
35+
36+
```php
37+
<?php
38+
/**
39+
* Copyright © Magento, Inc. All rights reserved.
40+
* See COPYING.txt for license details.
41+
*/
42+
43+
namespace Magento\PageBuilder\Test\Mftf\Helper;
44+
45+
use Magento\FunctionalTestingFramework\Helper\Helper;
46+
47+
/**
48+
* Class SelectText provides an ability to select needed text.
49+
*/
50+
class SelectText extends Helper
51+
{
52+
/**
53+
* Select needed text.
54+
*
55+
* @param string $context
56+
* @param int $startX
57+
* @param int $startY
58+
* @param int $endX
59+
* @param int $endY
60+
* @return void
61+
*/
62+
public function selectText(string $context, int $startX, int $startY, int $endX, int $endY)
63+
{
64+
try {
65+
/** @var \Magento\FunctionalTestingFramework\Module\MagentoWebDriver $webDriver */
66+
$webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
67+
68+
$contextElement = $webDriver->webDriver->findElement(\Facebook\WebDriver\WebDriverBy::xpath($context));
69+
$actions = new \Facebook\WebDriver\Interactions\WebDriverActions($webDriver->webDriver);
70+
$actions->moveToElement($contextElement, $startX, $startY)
71+
->clickAndHold()
72+
->moveToElement($contextElement, $endX, $endY)
73+
->release()
74+
->perform();
75+
} catch (\Exception $e) {
76+
$this->fail($e->getMessage());
77+
}
78+
}
79+
}
80+
```
81+
82+
### Notes About The PHP File
83+
84+
The following details are important about the file above:
85+
1. The `namespace` must match the file location like `namespace Magento\PageBuilder\Test\Mftf\Helper;`
86+
2. The class must `extends Helper` and have the corresponding `use` statement to match
87+
3. You can get access to the WebDriver object via `$this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver')`
88+
4. You can implement multiple related methods in the same class.
89+
90+
You should follow the same patterns in any Custom Helpers that you write yourself. But you can implement any logic or iteration that you need to solve for your use case.
91+
92+
### Referencing In A Test
93+
94+
Once you have implemented something like the above PHP file. You can then reference it in a test like this:
95+
96+
```xml
97+
<helper class="\Magento\PageBuilder\Test\Mftf\Helper\SelectText" method="selectText" stepKey="selectHeadingTextInTinyMCE">
98+
<argument name="context">//div[contains(@class, 'inline-wysiwyg')]//h2</argument>
99+
<argument name="startX">{{TinyMCEPartialHeadingSelection.startX}}</argument>
100+
<argument name="startY">{{TinyMCEPartialHeadingSelection.startY}}</argument>
101+
<argument name="endX">{{TinyMCEPartialHeadingSelection.endX}}</argument>
102+
<argument name="endY">{{TinyMCEPartialHeadingSelection.endY}}</argument>
103+
</helper>
104+
```
105+
106+
### Notes About The XML
107+
108+
1. Specify an argument value for every argument that matches our PHP implementation. This allows us to pass other test data to the Custom Helper.
109+
2. The `class` attribute matches the namespace we specified in the PHP file
110+
3. You can specify the method from the class via the `method` attribute
111+
4. If the function has a return value, it will be assigned to the stepKey variable. In this case `$selectHeadingTextInTinyMCE` would hold the return value.
112+
113+
## Key Takeaways
114+
115+
Custom Helpers allow you to solve for complex use cases such as conditional logic, iteration, or complex WebDriver usage.
116+
117+
With access to the WebDriver object, you have a lot of flexibility available to you. See the [Codeception WebDriver](https://github.com/Codeception/module-webdriver/blob/master/src/Codeception/Module/WebDriver.php) for technical details and functionality available for use.
118+
119+
A Custom Helper is written in a PHP file and then referenced in test XML like other actions.
120+
121+
Due to complexity, you should only use these as a last resort after trying to implement your test using built-in actions.
122+
123+
## References
124+
125+
[Codeception WebDriver source code](https://github.com/Codeception/module-webdriver/blob/master/src/Codeception/Module/WebDriver.php) - Reference for using the WebDriver Object

0 commit comments

Comments
 (0)