How to use codeception in MFTF( Magento Functional Testing Framework)
Note:- If you are using latest magento then Use “mftf” instead of “robo” command
First Install the Magento
First Go to your magento root directory.
- Install Composer
2. Go to dev/tests/acceptance folder
install composer
After installed Composer then Run this command
3.) vendor/bin/mftf build:project :- this command create Functional.suite.yml file and .env file
Go to .env file update the 4 data.
- MAGENTO_BASE_URL
For example, MAGENTO_BASE_URL=http://test.com
2.) MAGENTO_BACKEND_NAME:- Your site admin URL
For Example, MAGENTO_BACKEND_NAME=admin
3.) MAGENTO_ADMIN_USERNAME:- Site admin Username
For Example, MAGENTO_ADMIN_USERNAME=admin
4.) MAGENTO_ADMIN_PASSWORD:- Site admin password
For Example, MAGENTO_ADMIN_PASSWORD=adminpassword
If you do not have the Selenium server yet:
- Go to this site and download the selenium server:- https://www.seleniumhq.org/download/
- Download chrome driver also from this site:- https://chromedriver.storage.googleapis.com/index.html?path=2.41/
Keep both files selenium server and chromedriver at one place.
For example, you can keep both files in the htdocs folder or the Magento root directory.
Open command prompt where you kept selenium server and chromedriver files and run this command:
java -jar selenium-server-standalone-3.12.0.jar
Last part is Magento configuration
WYSIWYG settings
A Selenium web driver cannot enter data to fields with WYSIWYG.
To disable the WYSIWYG and enable the web driver to process these fields as simple text areas:
- Log in to the Magento Admin as an administrator.
- Navigate to Stores > Configuration > General > Content Management.
- In the WYSIWYG Options section set the Enable WYSIWYG Editor option to Disabled Completely.
- Click Save Config.
Security settings
To enable the Admin Account Sharing setting, to avoid unpredictable log out during a testing session, and disable the Add Secret Key in URLs setting, to open pages using direct URLs:
- Navigate to Stores > Configuration > Advanced > Admin > Security.
- Set Admin Account Sharing to Yes.
- Set Add Secret Key to URLs to No.
- Click Save Config.
5. Flush the Magento cache
Write Your First Test Case for your any Module
First, Open any IDE script editor -> open this folder
Magento root directory->dev/tests/acceptance/tests/functional/Magento/FunctionalTest
1.) Give the module name for which you want to write a test case.
For example, Module name:- Sample
2.) Inside Module, Create 4 folder:-
- Data
- Page
- Section
- Test
For Example, Sample->Data,Page,Section,Test
Now Start a writing a test case
First, we will start with Page Folder
1.) Go to Page Folder->Create a TestPage.xml File
Depends on You can give any page name. But at end of the name please put Page.xml
Sample/Page/TestPage.xml
The format of the TestPage.xml
<?xml version="1.0" encoding="UTF-8"?>
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../dev/tests/acceptance/vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Page/etc/PageObject.xsd">
<page name="" url="" module="" area="">
<section name=""/>
<section name=""/>
</page>
</pages>Now I’ll explain one by one.
- <page name=”name is the same as the file name”
For Example, <page name=”Testpage”
2. url= “here you can give Page URL where you can test”
For Example, I want to test on the product details page.
url=”/mona-pullover-hoodlie.html”
3.) Module=” Here you can give the module name(Sample)”
For Example, I took Sample module name so I put the module name Sample
Module=”Sample”
4.) Now area=”Here if your page related to frontend then give the name “storefront” Or if your page related to Backend then give the name “admin”. ”
For Example, I gave the page name (product details page) it is related to frontend
area=”storefront”
5.)Last Part Section name. Page File may contain several Section name.
For Example, <section name=”TestSection”/>
In Section file we write Elemet selector of that page.
Now We will create Section file inside the Section Folder.
Sample(Module name)/Section/TestSection.xml
Depends on You can give any section name. But at end of the name please put Section.xml
The Format of the Section file.
<?xml version="1.0" encoding="UTF-8"?>
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../dev/tests/acceptance/vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd">
<section name="">
<element name="" type="" selector="" />
<element name="" type="" selector=""/>
</section>
</sections>Now we will explain one by one inside the section file.
1 <section name=”Same as file name”>
For example, <section name=”TestSection”>
2. <element name=”Here give the name where you want to action of the element”
For example, Suppose I want to click on “ADD TO CART” button on produt details page in the frontend.
<element name=”AddtoCart”
3. type=” here it depends on elements.some elements are like button, input, text, select and etc.”
For Example, I want to click on add to cart button that means it is button So
type=”button”
4.)Last part is:- selector=” Here give CSS selector of that element where you want to action.
For example, I want to click on add to cart button on the product details page so I will give the add to cart button CSS selector
selector=”button.action.tocart.primary”
Now we will create the Test file.
Sample(Module Name)/Test/ProductDetailTest.xml
Depends on You can give any test name. But at end of the name please put Test.xml
The Format of the Test file.
<?xml version="1.0" encoding="UTF-8"?>
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../dev/tests/acceptance/vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd">
<test name="ProductDetailTest">
<annotations>
<features value="Add product to the cart on product details page"/>
<stories value="Add product to the cart on product details page"/>
<description value="Add product to the cart on product details page"/>
<severity value="MAJOR"/>
<testCaseId value="MAGETWO-1312"/>
<group value="Sample"/> </annotations> <before>
<!-- ACTIONS AND ACTION GROUPS PERFORMED BEFORE THE TEST --></before>
<after>
<!-- ACTIONS AND ACTION GROUPS PERFORMED AFTER THE TEST -->
<amOnPage url="admin/admin/auth/logout/" stepKey="amOnLogoutPage"/> </after> <!-- TEST ACTIONS, ACTION GROUPS, AND ASSERTIONS--><amOnPage url="{{TestPage.url}}" stepKey="NavigateToProductDetalsPage"/><click selector="{{TestSection.AddtoCart}}" stepKey="clickonAddtoCartButton”"/>
</test>
</tests>
</config>
Now I will explain one by one
1 . As a beginner, please ignore <before> </before> this part.
Because in <before> Here you can an action before the test executed like create a product, create a category </before>
2. In <after>Here you can any action after the test executed like Log out from admin </after>
3 . <amOnPage url=”Here give the Page name.url”
example, <amOnPage url=”{{TestPage.url}}”
4. stepKey=” here give the title what are you doing ? in this case I want to go on the product details page. So
stepKey=”NavigateToProductDetalsPage”
5. <click selector=”Here give the Section name.element name”
Example, <click selector=”{{TestSection.AddtoCart}}”
6. stepKey=” here give the title what are you doing ? in this case I want to click on add to cart button.So
stepKey=”clickonAddtoCartButton”
Go to dev/tests/acceptance folder
Run this command:-
“your PHP path” vendor/bin/robo generate: tests
Then Run
(Your PHP path) vendor/bin/codecept run tests/functional/Magento/FunctionalTest/_generated/ProductDetailTestCest.php
vendor/bin/codecept run :- for all test cases.List of commands to use in Magento Functional Testing Framework.
Some Commands in Codeception.
How to use these all Commands in Magento functional testing framework.
1.) click:- Clicks an element according to given selector or selectorArray
<click selector=”Selector of the element” stepKey=”Name of the steps”/>
For Example:
<click selector=”{{SectionName. ElementName}}” stepKey=”ClickOnLoginButton”/>
2.) clickWithLeftButton:- Performs a Left Click on an element according to given selector or selectorArray, or on given x/y coordinate.
3.)clickWithRightButton:- Performs a Right Click on an element according to given selector or selectorArray, or on given x/y coordinate.
4.)stepKey:- Unique identifier for test action.
For Example:
<click selector=”{{SectionName. ElementName}}” stepKey=”ClickOnLoginButton”/>
5.)after :- stepKey of preceding action.
For example:
<after>
<amOnPage url=”admin/admin/auth/logout/” stepKey=”amOnLogoutPage”/>
</after>
6.)userInput :- Data or value for Test Action.
<fillField userInput=”Test Data” selector=”Selector of the element” stepKey=”Name of the steps”/>
Suppose if you want to take data from element and that data you want to use for assertion .
Then You can use following Syntax for this.
<grabMultiple selector=”Here write CSS Selctor of the element” stepKey=”grabProducts”/>
Here “grabProducts” used as a variable. That can be Used in Actual Result.
<assertContains stepKey=”assertContains”>
<expectedResult type=”string”>Here Write your expected Result</expectedResult>
<actualResult type=”variable”>grabProducts</actualResult>
</assertContains>
<expectedResult type it depends on variable which type of data are you using.
Suppose you are using DIGIT or Count then you can use like
<expectedResult type=”int”>12</expectedResult>
2.) How to fetch the value from the element used for DATA.
If you want to grab or fetch the value from the element and then want use for Fill the Form.
There is a simple syntax for that
<grabValueFrom selector=”Here write CSS Selctor of the element” stepKey=”grabProducts”/>
Here “grabProducts” used as a variable.
<fillField userInput=”{$grabProducts}” selector=”{{SectionName.elementName}}” stepKey=”FillgrabProductVariableData”/>
