Skip to content

Commit 5497d21

Browse files
committed
MQE-1845: Convert Writing tests with actiongroups
1 parent 640c2c9 commit 5497d21

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

docs/guides/action-groups.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Action Group Best Practices
2+
3+
We should strive to write tests using only action groups. Fortunately we have built up a large set of action groups to get started. We can make use of them and extend them for our own specific needs. In some cases, we may never even need to write action groups of our own. We may be able to simply chain together calls to existing action groups to implement our new test case.
4+
5+
## Why use Action Groups?
6+
7+
Action groups simplify maintainability by reducing duplication. Because they are re-usable building blocks, odds are that they are already made use of by existing tests in the Magento codebase. This proves their stability through real-world use. Take for example, the action group named `LoginAsAdmin`:
8+
9+
```xml
10+
<actionGroup name="LoginAsAdmin">
11+
<annotations>
12+
<description>Login to Backend Admin using provided User Data. PLEASE NOTE: This Action Group does NOT validate that you are Logged In.</description>
13+
</annotations>
14+
<arguments>
15+
<argument name="adminUser" type="entity" defaultValue="DefaultAdminUser"/>
16+
</arguments>
17+
18+
<amOnPage url="{{AdminLoginPage.url}}" stepKey="navigateToAdmin"/>
19+
<fillField selector="{{AdminLoginFormSection.username}}" userInput="{{adminUser.username}}" stepKey="fillUsername"/>
20+
<fillField selector="{{AdminLoginFormSection.password}}" userInput="{{adminUser.password}}" stepKey="fillPassword"/>
21+
<click selector="{{AdminLoginFormSection.signIn}}" stepKey="clickLogin"/>
22+
<closeAdminNotification stepKey="closeAdminNotification"/>
23+
</actionGroup>
24+
```
25+
26+
As you may be able to guess, logging in to the admin panel is one of the most used action groups. It is used around 1,500 times at the time of this writing.
27+
28+
Imagine if this wasn't an action group and instead we were to copy and paste these 5 actions so many times. In that scenario, if we need to make a small change it would require a lot of work. But with the action group, we can make the change in one place.
29+
30+
## How can I extend Action Groups?
31+
32+
Let's continue using `LoginAsAdmin` as our example. I have trimmed away metadata to clearly reveal that this action group performs 5 actions:
33+
34+
```
35+
<actionGroup name="LoginAsAdmin">
36+
...
37+
<amOnPage url="{{AdminLoginPage.url}}" .../>
38+
<fillField selector="{{AdminLoginFormSection.username}}" .../>
39+
<fillField selector="{{AdminLoginFormSection.password}}" .../>
40+
<click selector="{{AdminLoginFormSection.signIn}}" .../>
41+
<closeAdminNotification .../>
42+
</actionGroup>
43+
```
44+
45+
This works against the standard Magento admin panel login page. But let's imagine we're working on a Magento extension that adds a CAPTCHA field to the login page. If we create and activate this extension and then we try to run all existing tests, we can expect almost everything to fail because now we are unable to log in because we did not completely fill out all of the login form. The CAPTCHA field was left unfilled.
46+
47+
We can overcome this by making use of MFTF's extensibility. All we need to do is to provide a "merge" that modifies the existing `LoginAsAdmin` action group. Our simple merge file will look like this:
48+
49+
```
50+
<actionGroup name="LoginAsAdmin">
51+
<fillField selector="{{CaptchaSection.captchaInput}}" before="signIn" .../>
52+
</actionGroup>
53+
```
54+
55+
Because the name of this merge is also `LoginAsAdmin`, the two get merged together and an additional step happens everytime this action group is made use of.
56+
57+
To continue this demonstration, let's imagine someone else is working on a Two Factor Authentication extension and they also provide a merge for the `LoginAsAdmin` action group. Their merge looks similar to what we've already seen. The only difference is that this time we fill a different field:
58+
59+
```
60+
<actionGroup name="LoginAsAdmin">
61+
<fillField selector="{{TwoFactorSection.twoFactorInput}}" before="signIn" .../>
62+
</actionGroup>
63+
```
64+
65+
Bringing it all together, our resulting `LoginAsAdmin` action group becomes this:
66+
67+
```
68+
<actionGroup name="LoginAsAdmin">
69+
...
70+
<amOnPage url="{{AdminLoginPage.url}}" .../>
71+
<fillField selector="{{AdminLoginFormSection.username}}" .../>
72+
<fillField selector="{{AdminLoginFormSection.password}}" .../>
73+
<fillField selector="{{CaptchaSection.captchaInput}}" .../>
74+
<fillField selector="{{TwoFactorSection.twoFactorInput}}" .../>
75+
<click selector="{{AdminLoginFormSection.signIn}}" .../>
76+
<closeAdminNotification .../>
77+
</actionGroup>
78+
```
79+
80+
Note that no file actually contains these exact contents above, but instead all three files come together to form this action group.
81+
82+
One final thing to be aware of is that this extensibility can be applied in many ways. Obviously we need to use it if we want to affect existing Magento entities like tests, action groups, and data. But something not so obvious is that this can be used within the walls of your own entities in order to make them more maintainable too.

0 commit comments

Comments
 (0)