MFTF Test Creation
Test Structure
<!-- app/code/Vendor/Module/Test/Mftf/Tests/LoginTest.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento\\TestFramework:xsd/mftf.xsd">
<test name="AdminLoginTest">
<annotations>
<features value="Admin Login"/>
<title value="Admin user can login with valid credentials"/>
<severity value="critical"/>
<group value="login"/>
</annotations>
<before>
<magentoCLI command="config:set web/secure/base_url {{StoreConfig.BASE_URL}}" stepKey="setBaseUrl"/>
<amOnPage url="{{AdminLoginPage.url}}" stepKey="openAdminLogin"/>
</before>
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin">
<argument name="username" value="{{AdminUser.username}}"/>
<argument name="password" value="{{AdminUser.password}}"/>
</actionGroup>
<see url="{{AdminDashboardPage.url}}" stepKey="seeDashboardUrl"/>
<see element="{{AdminDashboardPage.mainTitle}}" stepKey="seeDashboardTitle"/>
<after>
<logout stepKey="logout"/>
</after>
</test>
</tests>
Running MFTF Tests
# Run specific test
vendor/bin/mftf run:test AdminLoginTest -r
# Run test suite
vendor/bin/mftf run:suite adminLogin
# Run with specific browser
vendor/bin/mftf run:test AdminLoginTest --browser=chrome
# Generate report
vendor/bin/mftf generate:report
Key Points
- Tests are XML-based, not PHP
- Use action groups for reusable steps
- Before/after hooks for setup/cleanup
- Annotations define test metadata
MFTF Actions
Action Groups
<!-- app/code/Vendor/Module/Test/Mftf/ActionGroup/LoginAsAdmin.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento\\TestFramework:xsd/mftf.xsd">
<actionGroup name="LoginAsAdmin">
<arguments>
<argument name="username" type="string"/>
<argument name="password" type="string"/>
</arguments>
<fillField selector="{{AdminLoginForm.usernameField}}" userInput="{{username}}" stepKey="enterUsername"/>
<fillField selector="{{AdminLoginForm.passwordField}}" userInput="{{password}}" stepKey="enterPassword"/>
<click selector="{{AdminLoginForm.loginButton}}" stepKey="clickLogin"/>
<waitForPageLoad time="5" stepKey="waitForDashboard"/>
</actionGroup>
</actionGroups>
Common Actions
<!-- Click action -->
<click selector="{{ProductForm.saveButton}}" stepKey="clickSave"/>
<!-- Fill field -->
<fillField selector="{{ProductForm.nameField}}" userInput="Test Product" stepKey="enterName"/>
<!-- Select option -->
<selectOption selector="{{ProductForm.statusField}}" userInput="Enabled" stepKey="selectStatus"/>
<!-- Check box -->
<checkOption selector="{{ProductForm.featuredField}}" stepKey="checkFeatured"/>
<!-- Wait -->
<waitForPageLoad time="10" stepKey="waitForLoad"/>
<!-- See element -->
<see selector="{{ProductForm.successMessage}}" userInput="Product saved" stepKey="seeSuccess"/>
<!-- Grab text -->
<grabTextFrom selector="{{ProductForm.nameField}}" stepKey="grabName"/>
Key Points
- Actions are atomic operations
- Each action has a unique stepKey
- Actions can take arguments for reusability
- Use waitForPageLoad for async operations
MFTF Assertions
Assertion Types
<!-- See - verify element contains text -->
<see selector="{{ProductPage.title}}" userInput="{{Product.name}}" stepKey="seeTitle"/>
<!-- SeeInField - verify input field value -->
<seeInField selector="{{ProductForm.priceField}}" userInput="99.99" stepKey="seePrice"/>
<!-- SeeCheckboxIsChecked -->
<seeCheckboxIsChecked selector="{{ProductForm.featuredField}}" stepKey="seeFeatured"/>
<!-- SeeElement -->
<seeElement selector="{{ProductForm.saveButton}}" stepKey="seeSaveButton"/>
<!-- DontSee -->
<dontSee selector="{{ProductForm.errorMessage}}" stepKey="noError"/>
<!-- DontSeeElement -->
<dontSeeElement selector="{{ProductForm.deleteButton}}" stepKey="noDeleteButton"/>
Custom Assertions
<actionGroup name="AssertProductPrice">
<arguments>
<argument name="expectedPrice" type="string"/>
</arguments>
<seeInField selector="{{ProductForm.priceField}}" userInput="{{expectedPrice}}" stepKey="assertPrice"/>
<seeInField selector="{{ProductForm.specialPriceField}}" userInput="{{expectedPrice}}" stepKey="assertSpecialPrice"/>
</actionGroup>
Key Points
- Use see for visible text verification
- Use seeInField for form field values
- Use dontSee for negative assertions
- Chain assertions in action groups
MFTF Pages and Elements
Page Definitions
<!-- app/code/Vendor/Module/Test/Mftf/Pages/AdminLoginPage.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento\\TestFramework:xsd/mftf.xsd">
<page name="AdminLoginPage" url="{{AdminLoginURL}}" module="Magento_Backend">
<section name="AdminLoginForm">
<element name="usernameField" type="input" selector="input[name='login[username]']"/>
<element name="passwordField" type="input" selector="input[name='login[password]']"/>
<element name="loginButton" type="button" selector=".action.login"/>
</section>
</page>
</pages>
Section Definitions
<!-- app/code/Vendor/Module/Test/Mftf/Section/AdminLoginFormSection.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento\\TestFramework:xsd/mftf.xsd">
<section name="AdminLoginForm">
<element name="usernameField" type="input" selector="input[name='login[username]']"/>
<element name="passwordField" type="input" selector="input[name='login[password]']"/>
<element name="loginButton" type="button" selector=".action.login"/>
<element name="errorMessage" type="text" selector=".message-error"/>
</section>
</sections>
Data Definitions
<!-- app/code/Vendor/Module/Test/Mftf/Data/AdminUserData.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<datasets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento\\TestFramework:xsd/mftf.xsd">
<dataset name="AdminUser">
<data key="username">admin</data>
<data key="password">admin123</data>
</dataset>
<dataset name="AdminUserCustom">
<data key="username">custom_admin</data>
<data key="password">custom_pass123</data>
</dataset>
</datasets>
Key Points
- Pages define URLs and element selectors
- Sections group related elements
- Data provides test data values
- Use XPath or CSS selectors for elements
Practice Problems
0 / 1 solved
Create MFTF Test
Create an MFTF test for product creation in the admin panel.
Solution
<?xml version="1.0" encoding="UTF-8"?>
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento\\TestFramework:xsd/mftf.xsd">
<test name="CreateSimpleProductTest">
<annotations>
<features value="Product Management"/>
<title value="Create a simple product"/>
<severity value="critical"/>
</annotations>
<before>
<actionGroup ref="LoginAsAdmin" stepKey="login"/>
</before>
<amOnPage url="{{AdminProductPage.url}}" stepKey="openProductPage"/>
<click selector="{{AdminProductPage.addProductButton}}" stepKey="clickAddProduct"/>
<fillField selector="{{AdminProductForm.nameField}}" userInput="MFTF Test Product" stepKey="enterName"/>
<fillField selector="{{AdminProductForm.skuField}}" userInput="MFTF-TEST-001" stepKey="enterSku"/>
<fillField selector="{{AdminProductForm.priceField}}" userInput="29.99" stepKey="enterPrice"/>
<click selector="{{AdminProductForm.saveButton}}" stepKey="clickSave"/>
<see selector="{{AdminProductPage.successMessage}}" userInput="Product saved" stepKey="seeSuccess"/>
<after>
<logout stepKey="logout"/>
</after>
</test>
</tests> Quiz
1. What format are MFTF tests written in?
2. What is an action group?
3. How do you verify an element contains text?
4. Where are MFTF page definitions stored?
Flashcards
Question
MFTF test format?
Click to reveal answer
Answer
XML with specific schema
Question
Action group purpose?
Click to reveal answer
Answer
Reusable sets of actions for common workflows
Question
Verify element text?
Click to reveal answer
Answer
Use 'see' action with selector and expected text
Question
MFTF data files?
Click to reveal answer
Answer
Provide test data values for tests
Revision Notes
Key Takeaways
- 1. MFTF tests are XML-based, not PHP
- 2. Action groups provide reusable test steps
- 3. Pages and sections define element selectors
- 4. Use before/after for setup and cleanup
Interview Tips
- • Explain MFTF vs Codeception differences
- • Discuss action group reusability
- • Know MFTF test execution flow
Cheat Sheet
MFTF
- Format: XML
- Run: vendor/bin/mftf run:test
- Actions: click, fillField, see
- Pages: element selectors
- Data: test data definitions