Reading list Switch to dark mode

    Magento 2 Functional Testing Framework (MFTF) Merging

    Updated 12 December 2018

    Merging is the most beneficial feature of MFTF. It helps to avoid code duplicacy. Magento already provides it’s prewritten test cases for its basic functionalities. I’ll explain Merging here with an example of the Customer Signup form. Magento already provides its test cases for the customer signup form but let’s say I have installed a custom module and some new form attributes added on this page.

    Now, we don’t need to write the complete test case of this particular page. Here we can use merging. For merging, we need to create a new file inside the custom module’s Test folder and add all details related to the newly added fields only.

    Magento Customer Signup Form-

    mage2sign

    Magento customer signup form after module installation-

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    CustomFields

    Let’s see the technical implementation-

    Navigate to MagentoRoot/dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Customer/Test here you’ll see a file  StorefrontCreateCustomerTest.xml. This is the test written by Magento for the customer signup form from the frontend.

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
     /**
      * Copyright © Magento, Inc. All rights reserved.
      * See COPYING.txt for license details.
      */
    -->
    
    <tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd">
        <test name="StorefrontCreateCustomerTest">
            <annotations>
                <features value="Customer Creation"/>
                <stories value="Create a Customer via the Storefront"/>
                <title value="You should be able to create a customer via the storefront"/>
                <description value="You should be able to create a customer via the storefront."/>
                <severity value="CRITICAL"/>
                <testCaseId value="MAGETWO-23546"/>
                <group value="customer"/>
                <group value="create"/>          
            </annotations>
            <!-- <after>
                <amOnPage url="admin/admin/auth/logout/" stepKey="amOnLogoutPage"/>
            </after> -->
    
            <amOnPage stepKey="amOnStorefrontPage"  url="/"/>
            <click stepKey="clickOnCreateAccountLink" selector="{{StorefrontPanelHeaderSection.createAnAccountLink}}"/>
            <fillField  stepKey="fillFirstName" userInput="{{CustomerEntityOne.firstname}}" selector="{{StorefrontCustomerCreateFormSection.firstnameField}}"/>
            <fillField  stepKey="fillLastName" userInput="{{CustomerEntityOne.lastname}}" selector="{{StorefrontCustomerCreateFormSection.lastnameField}}"/>
            <fillField  stepKey="fillEmail" userInput="{{CustomerEntityOne.email}}" selector="{{StorefrontCustomerCreateFormSection.emailField}}"/>
            <fillField  stepKey="fillPassword" userInput="{{CustomerEntityOne.password}}" selector="{{StorefrontCustomerCreateFormSection.passwordField}}"/>
            <fillField  stepKey="fillConfirmPassword" userInput="{{CustomerEntityOne.password}}" selector="{{StorefrontCustomerCreateFormSection.confirmPasswordField}}"/>
            <click stepKey="clickCreateAccountButton" selector="{{StorefrontCustomerCreateFormSection.createAccountButton}}"/>
            <see stepKey="seeThankYouMessage" userInput="Thank you for registering with Main Website Store."/>
            <see stepKey="seeFirstName" userInput="{{CustomerEntityOne.firstname}}" selector="{{StorefrontCustomerDashboardAccountInformationSection.ContactInformation}}" />
            <see stepKey="seeLastName" userInput="{{CustomerEntityOne.lastname}}" selector="{{StorefrontCustomerDashboardAccountInformationSection.ContactInformation}}" />
            <see stepKey="seeEmail" userInput="{{CustomerEntityOne.email}}" selector="{{StorefrontCustomerDashboardAccountInformationSection.ContactInformation}}" />
        </test>
    </tests>

    Now I’ll create a new folder of my custom module inside MagentoRoot/dev/tests/acceptance/tests/functional/Magento/FunctionalTest/MyModuleName/ and inside this folder, I’ll create a Test folder and inside this Test folder, I’ll create a file with the same name used by Magento which is StorefrontCreateCustomerTest.xml and will write the steps for newly added fields.

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
     /**
     * Webkul Software.
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    
    <tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd">
        <test name="StorefrontCreateCustomerTest">
            <annotations>
                <features value="Seller Creation"/>
                <stories value="Create a Seller via the Storefront"/>
                <title value="You should be able to create a seller via the storefront"/>
                <description value="You should be able to create a seller via the storefront."/>
                <severity value="CRITICAL"/>
                <testCaseId value="WEBKULMP-00001"/>
                <group value="sellercreate"/>
            </annotations>
            <click stepKey="clickBecomeSellerRadioButton" selector="{{StorefrontCustomerCreateFormSection.becomeSellerRadioButton}}" before="fillEmail"/>
            <waitForAjaxLoad time="5" stepKey="waitForAjaxResponse" before="clickBecomSellerRadioButton"/>
            <fillField stepKey="shopUrl" userInput="{{SellerEntityOne.shopurl}}" selector="{{StorefrontCustomerCreateFormSection.shopUrlField}}" after="fillPassword"/>
            <remove keyForRemoval="seeFirstName"/>
            <remove keyForRemoval="seeLastName"/>
            <remove keyForRemoval="seeEmail"/>
        </test>
    </tests>

    As you can see in my custom module file StorefrontCreateCustomerTest.xml I have not written the steps already written in Magento file. I have written the steps for my newly added fields only.

    Now the steps of test cases will work like – Fill the First Name, Last Name, Email, Yes(Radio Button), Shop URL (Text Field) and then Password and Confirm Password and then it will click on the submit button.

    before=”fillEmail” will make sure that after filling details in email field it will select the radio button. fillEmail is a stepKey of the step of filling the data in email field written in Magento’s  customer creation test file StorefrontCreateCustomerTest.xml 

    after=”fillPassword” will make sure that after filling the data in ShoP Url field only it will fill the data in the password field.fillPassword is a stepKey of the step of filling the data in password field written in Magento’s  customer creation test file StorefrontCreateCustomerTest.xml 

    <remove keyForRemoval=”seeFirstName”/> I have used it to remove this step because after my custom module installation it will be redirected to a new page where it will not get the email of customer. After registration Magento by default redirects on the customer dashboard page. Where we get the customer email, first name, last name.

    Customer dashboard after registration –

    M2Dashboard

    Seller dashboard after registration –

    seller

     

    For userInput and selector also I have created the files.

    Now merge both the files by running this command – vendor/bin/robo generate:tests

    It will generate StorefrontCreateCustomerTestCest.php inside _generated folder of the test file of your XML test file.

    That’s all about merging. If you have any doubt then please drop your comment below.

    Thanks! 🙂

     

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    4 comments

  • Jaya Venkat
    • Parth Sidana (Moderator)
  • Jesni
    • Shruti Baranwal (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home