>A Quick Guide to Automation Test in Magento 2

A Quick Guide to Automation Test in Magento 2

In Magento 2, testing is a mandatory part of various processes: upgrading Magento platform developing extensions, making customizations, and many more to detect introduced bugs early and improve the code quality.

Though testing can be performed manually or automatically, Magento 2 automation tests are highly recommended. Thanks to automation tests in Magento 2, testers can:

  • Simplify Magento 2 testing procedures
  • Save precious time and human resources
  • Take the full management over the test result
  • Adjust the test’s input data or preconditions freely
  • Re-run Magento 2 automation tests dynamically

In this post, we will have a brief introduction of Magento 2 automation test installation and how to create a test case in several simple settings. This guide is specially useful for Magento 2 developers as well as for PHP developers, QA specialists, and system integrators.

I. Installation:

A. Adjust configuration:

1. Edit phpunit.xml to configure PHPUnit

In <magento2_root_dir>/dev/tests/functional, please find phpunit.xml and open it in editor.

If you cannot find phpunit.xml, create it from phpunit.xml.dist.

Set URL for Magento storefront under test

Find <php> element.

For name=”app_frontend_url” set value that is your actual URL for Magento storefront under test.

Example:

http://127.0.0.1/magento223-bk/”/>

Set URL for Magento Admin URL under test

For name=”app_backend_url” set value that is your actual URL for Magento Admin URL under test.

Example:

http://127.0.0.1/magento223-bk/admin/”/>

Automation Test Magento 2
Automation Test Magento 2

2. Edit config.xml to configure Functional Testing Framework

In <magento2_root_dir>/dev/tests/functional/etc, find config.xml and open it in editor.

If you cannot find config.xml, create it from config.xml.dist.

Set credentials for Magento Admin under test

In <application> element, find elements <backendLogin> and <backendPassword>. Change content of these elements to your actual credentials for Magento Admin URL.

Set browser that the FTF will use for tests

Copy <server> element from <magento2_root_dir>/dev/tests/functional/vendor/magento/mtf/etc/config.xml> and paste it in <magento2_root_dir>/dev/tests/functional/etc/config.xml after <install> element.

Enter actual data in <browserName> and <browser> attributes.

If set in Google Chrome:

<server> <item name=”selenium” type=”default” browser=”Google Chrome” browserName=”chrome” host=”localhost” port=”4444″ seleniumServerRequestsTimeout=”90″ sessionStrategy=”shared”> <desiredCapabilities> <platform>ANY</platform> </desiredCapabilities> </item> </server>

Automation Test Magento 2
Automation Test Magento 2

B. Prepare Magento application:

1. Change WYSIWYG settings

    • Please follow Stores ⇒ Configuration ⇒ General ⇒ Content Management ⇒ WYSIWYG Options

    • Set Enable WYSIWYG Editor to Disabled Completely

  • Remember to Save Config

2. Change security settings

    • Please follow Stores ⇒ Configuration ⇒ Advanced ⇒  Admin ⇒ Security

    • Set Admin Account Sharing to Yes

  • Set Add Secret Key to URLs to No

3. Refresh page cache

4. Apache

Copy /dev/tests/functional/.htaccess.sample to /dev/tests/functional/.htaccess.

C. Prepare environment for test run:

1. Install Java

https://java.com/en/download/

2. Download Selenium Standalone Server

https://www.seleniumhq.org/download/

Automation Test
Automation Test

Enter in terminal:

java -jar <path_to_selenium_directory>/<name-of-selenium-server>.jar

3. Download geckodriver

https://github.com/mozilla/geckodriver/releases

4. Run generator

Enter in terminal:

cd <magento2_root_dir>/dev/tests/functional/utils

php generate.php

D. Test run:

1. Run all tests

Enter in terminal:

cd <magento2_root_dir>/dev/tests/functional

vendor/bin/phpunit

2. Run particular test

Enter in terminal:

cd <magento2_root_dir>/dev/tests/functional vendor/bin/phpunit –filter <name of test>

Example:

Let’s check the creation of new category. For this we should run <magento2_root_dir>/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/CreateCategoryEntityTest.php. To run this test enter the following script in your terminal:

cd <magento2_root_dir>/dev/tests/functional

vendor/bin/phpunit –filter CreateCategoryEntityTest

E. See logs for failed tests:

All failed tests are logged in <magento2_root_dir>/dev/tests/functional/var/log.

All Magento errors are logged in <magento2_root_dir>/var/log.

II.Create Test Case:

A. Example test case:

Variation 1:

    1. Log in to Admin

    1. Browse to Marketing ⇒ SEO & Search ⇒ Search Synonyms

    1. Click the New Synonyms Group button

    1. Enter data in the Synonyms field

    1. Click the Save Synonym Group button

  1. Verify the synonym group saved successfully

Variation 2:

    1. Log in to Admin

    1. Browse to Marketing ⇒ SEO & Search ⇒ Search Synonyms

    1. Click the New Synonyms Group button

    1. Select All Store Views in the Scope field

    1. Enter data in the Synonyms field

    1. Click the Save Synonym Group button

  1. Verify the synonym group saved successfully

Variation 3:

    1. Log in to Admin

    1. Browse to Marketing ⇒ SEO & Search ⇒ Search Synonyms

    1. Click the New Synonyms Group button

    1. Select Default Store View in the Scope field

    1. Enter data in the Synonyms field

    1. Click the Save Synonym Group button

  1. Verify the synonym group saved successfully

B. Test:

Enter in your terminal:

cd <magento2_root_dir>/dev/tests/functional/utils php -f generateFixtureXml.php — –name synonym –entity_type search_synonyms –collection Magento\\Search\\Model\\ResourceModel\\Query\\Collection

Copy folder code to magento2_root_dir

https://github.com/BssGroup/custom-test-case-create-synonym
Detail: https://devdocs.magento.com/guides/v2.2/mtf/create_test/create_new_test.html

Enter in your terminal:

php <magento2_root_dir>/dev/tests/functional/utils/generate.php

cd <magento2_root_dir>/dev/tests/functional

vendor/bin/phpunit –filter CreateSynonymEntityTest

See log:

-Terminal:

 

– Folder <magento2_root_dir>/dev/tests/functional/var/log.

  (include image).

Above is the step-by-step guide to install Magento 2 automation test with a detailed test case.

If you find this sharing is useful, please Like, Share, and leave Comment below.

Let us know at support@bsscommerce.com if you have any question about Magento 2 automation test or Magento 2 extensions.

< Previous Post
Next Post >