fProceed with the Next button to finish the installation process.

Reading Time: 7 mins

Umpteen businesses banks on Magento, an e-commerce platform which is named as one of the ‘Leaders’ of B2B e-commerce. Effective measures must be undertaken to test a complex platform like Magento along with its extended functionality. Though the word, ‘testing’ carries similar procedures across the industries, testing an e-commerce platform is not as easy as it sounds.

Why Use Automation Testing For Magento?

When it comes to testing, the term ‘automation’ seems to be a part of every simple and complex testing. Automation is something wherein, the test cases will be executed using automation testing tools instead of manual execution. Further, the automation testing is faster, reliable and less error-prone as humans often commit mistakes despite their conscious efforts. This helps the QA (or developers) team to eliminate some of the time and resource-consuming manual works such as triggering marketing mails.

By default, Magento 2 has the automation features such as a Cron job to run scheduled tasks like re-indexing and generate newsletters, emails and sitemaps. But to overcome the real-time challenges faced by an e-commerce site, a mere automation feature doesn’t suffice, indeed an Automation Testing has to be integrated for Multi-vendor Marketplace. Here, an end-to-end guide of running an Automated Testing for all regression scenarios in Magento using Selenium, MFTF and TestNG Framework have been discussed.

Why Selenium:

Selenium is an open-source automation testing tool and designed exclusively for web-based applications. One of the main reasons for choosing Selenium over other automation tools is that it works on multiple operating systems, platforms (Windows, Linux, Solaris, Os X ), programming languages like (Java, C#, Ruby, PHP, Python, Pearl) and web browsers like Chrome, Safari, Firefox, Internet Explorer, etc.

Product Tools of Selenium:

Being known as the prominent standalone automation testing tool, selenium doesn’t offer automation services as a single tool. Instead, it offers the automation services via its two major products such as,

  • Selenium WebDriver and Remote Control (RC)
  • Selenium IDE

Selenium WebDriver and Selenium RC

selenium-rc-logoselenium-webdriver-logo
The Selenium WebDriver superseded it’s precursor Selenium RC (Remote Control) which is now have been officially deprecated. The WebDriver which is regarded as the Selenium 2 (and RC as 1.x bindings)  incorporates all its precursor’s functionalities and overcomes its limitations too, such as the single host origin policy.

Selenium 1.0 +WebDriver = Selenium 2

Selenium IDE

selenium-ide-logo
The Selenium IDE (Integrated Development Environment) is a simple Chrome and Firefox add on and known for its record and playback functionalities. For those who want to automate the exploratory testing, Selenium IDE is the best option to go with and also facilitates the testers in creating quick bug reproduction scripts.

We are now got to know about the perks and productive uses of automation testing and testing tool, Selenium. Let us delve deeper into the how-to process of configuring Maven, Cucumber and Jenkins to run with Selenium on a Magento websites/application.

Installing Maven and Configuring it to run with Cucumber & Selenium:

Step 1: Installing Maven

Go to the Eclipse IDE and choose Install New Software by tapping the Help option from the Eclipse main menu.

Enter the http://download.eclipse.org/technology/m2e/releases/url on the install dialog and subsequently select the Work With and m2e plugin.

installing-maven-eclipse-ide

Img. 1.1

Proceed with the Next button to finish the installation process.

Step 2: Configuring Maven Project into Eclipse

To create a new (Maven) project, go to the Eclipse menu and choose File>>New>>Other.

On the Select a Wizard dialog, tap Maven>>Maven Project.

selecting-wizard-maven-project-eclipse

Img. 2.1

 

Upon completion, click Next.

Now, you will be directed to a New Maven Project page. Select the Create a Simple Maven Project checkbox and tap Next.

creating-simple-maven-project-eclipse

Img. 2.2

 

For example let’s enter Group ID and Artifact ID tabs as MageProjCont in and MageProj.

configuring-maven-project

Img. 2.3

Upon completion, click Next.

Post which you will be able to see a below-shown MageProj arrangement created by Eclipse.

mageproj-arrangement-eclipse

Img. 2.4

Again from the MageProj arrangement, select the pom.xml file.

As you can see, the pom.xml will be opened in the Editor section.

pom.xml-file-mageproj-arrangement-eclipse

Img. 2.5

Step 3: Using Cucumber

Scenario 1: Print Text in the Console

a) Create Project in Eclipse:

Now, create a new Java project by the name MavenBlog1 as shown in the image 3.1.

creating-java-project-eclipse-cucumber

Img. 3.1

Img. 3.2

mavenblog-arrangement-java-project-eclipse

b) Adding jar Files:

Now, to add the jar files, select Properties (using right-click) and choose Java Build Path, wherein add all the necessary libraries.

adding-jar-files-java-build-path-cucumber-with-selenium

Img. 3.3

 

c) To Create a Feature File:

Next, create a features folder to open a Feature File and name the folder as ‘features’.

creating-feature-folder-mavenblog-cucumber-with-selenium

Img. 3.4

Tap the Finish button.

Now create a file in the Features folder and name it as ‘login.feature’.

creating-file-in-feature-folder-cucumber-with-selenium

Img. 3.5

feature-file-arrangement-maveenblog-cucumber-with-selenium

 

Img. 3.6

d) To Write Scenarios:

Next, write scenarios. The below-shown image is an example of the login.feature file written Gherkin language (of Cucumber).

Feature: Validating on login page of Application
Scenario Outline: Validating test with valid login credentials
Given Initialize the browser with chrome and launch the application
And Click on the Login link in home page to land on Secure sign in Page
When User enters Username and Password and logs in
Then Close the browser

The line-by-line explanation of the code can be seen below.

  1. In the first line, the business functionality has been written.
  2. In the second line, outline of the test scenario is written.
  3. In the third line, pre-condition has been defined.
  4. In the fourth line, the action which is needed to execute has been defined.
  5. In the fifth line, the next action to be executed has been re-defined.
  6. And the last line defines the expected outcome, or result.

e) To Write Selenium Testrunner Script.

To write a Selenium testrunner script, first, create a TestRunner package and then create a class file Runner.java under it.

package TestRunner;		

import org.junit.runner.RunWith;		
import cucumber.api.CucumberOptions;		
import cucumber.api.junit.Cucumber;		

@RunWith(Cucumber.class)				
@CucumberOptions(features="Features",glue={"StepDefinition"})						
public class Runner 				
{		

}

The explanation of the annotations can be seen below.

RunWith () – It assigns the test runner class to start performing the concerned tests.
CucmberOptions () – It helps to fix the properties such as definition, feature, file for the Cucumber test.

The image 3.7 is an example of how the TestRunner file looks like.

testrunner-file-maven-blog-java-cucumber-with-selenium

Img. 3.7

f) To Create Step Definition Script:

Similarly, to create the Step Definition Script, first create a StepDefinition package and then the script file Steps.java under it.

public class StepDefinition extends base {

   @Given("^Initialize the browser with chrome$")
   public void initialize_the_browser_with_chrome() throws Throwable {

      driver=initializeDriver();
   }

   @And("^Navigate to \"([^\"]*)\" Site$")
   public void navigate_to_Site(String arg1) throws Throwable {

      driver.get(arg1);
   }

   @Then("^Click on the Login link in home page to land on Secure sign in Page$")
   public void click_on_Login_link_in_home_page_to_land_on_Secure_sign_in_Page() throws Throwable {

      LandingPage l=new LandingPage(driver);
      l.getLogin().click();
   }

   @When("^User enters (.+) and (.+) and logs in$")
   public void user_enters_and_and_logs_in(String username, String password) throws Throwable {

      LoginPage lp=new LoginPage(driver);
      lp.getEmail().sendKeys(username);
      lp.getPassword().sendKeys(password);
      lp.getLogin().click();
   }
   
   @And("^Close the browser$")
    public void close_the_browser() throws Throwable {
        throw new PendingException();
    }

}

Defining the annotations:

@Given – it defines the prerequisite for test
Eg:- to Initialize the browser with chrome

@When –  it defines the trigger point for any test scenario execution 
Eg:- When User enters Username and Password and logs in

@And –  it provides the logical AND condition between any two statements also to be used in conjunction with GIVEN, WHEN and THEN 
Eg:- to User enters (.+) And (.+) and logs in

@Then –  it defines  the expected result or complete the test to be executed 
Eg:- Then Close the browser

The below-shown image 3.8 is an example of how the Steps.java script looks like.

steps.java-script-maven--cucumber-with-selenium

Img. 3.8

 

g) To Execute the Script:

Now, you can execute the script via Runner.java of the TestRunner file.

executing-testrunner-script-cucumber-with-selenium-maven-installation

Img. 3.9

Scenario 2: Validate Test on Magemint with Valid Login Credentials. Carry Out this for Two Sets of Data

To update the Steps.java and the feature file, follow the below steps.

a) To Update the Feature File:

Here we update the feature file with 'Scenario Outline' and 'examples' syntax.

Feature: Login into application

Scenario Outline: Validating test with valid login credentials
Given Initialize the browser with chrome
And Navigate to "https://elastic-demo.magemint.com/" Site
Then Click on the Login link in home page to land on Secure sign in Page
When User enters <Username> And <Password> and logs in
And Close the browser

Examples:
|Username |Password |
|test99@gmail.com |123456 |
|testing@test.com |Testing@#123 |

 

updated-feature-file-cucumber-with-selenium

Img. 4.1

b) To Update the Steps.java script:

Now, update the Step.java script.

These are the parameters which were passed to the below-updated script.

package stepDefinitions;

//import org.testng.Assert;

import cucumber.api.PendingException;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageObjects.LandingPage;
import pageObjects.LoginPage;
import resources.base;

public class StepDefinition extends base {

   @Given("^Initialize the browser with chrome$")
   public void initialize_the_browser_with_chrome() throws Throwable {

      driver=initializeDriver();
   }

   @And("^Navigate to \"([^\"]*)\" Site$")
   public void navigate_to_Site(String arg1) throws Throwable {

      driver.get(arg1);
   }

   @Then("^Click on the Login link in home page to land on Secure sign in Page$")
   public void click_on_Login_link_in_home_page_to_land_on_Secure_sign_in_Page() throws Throwable {

      LandingPage l=new LandingPage(driver);
      l.getLogin().click();
   }

   @When("^User enters (.+) and (.+) and logs in$")
   public void user_enters_and_and_logs_in(String username, String password) throws Throwable {

      LoginPage lp=new LoginPage(driver);
      lp.getEmail().sendKeys(username);
      lp.getPassword().sendKeys(password);
      lp.getLogin().click();
   }
   
   @And("^Close the browser$")
    public void close_the_browser() throws Throwable {
        throw new PendingException();
    }

}

updated-steps.java-script-cucumber-with-selenium

Img. 4.2

c) To Execute the Updated Scripts:

The below screens show the successful execution of the script by each set of data.

  1. Browser is launched
  2. Magemint site gets opened
  3. Username and password are placed in the respective tabs
  4. Validations are done

From the below-shown screenshot, you can see that the browser has been launched and the Magemint site got opened. The login credentials are typed in their respective tabs to sign into the interface.

default-customer-login-page-automation-testing

Img. 4.3

Overall Result

Here, the overall result of the executed scenarios can be seen.

executed-scenarios-automation-testing-cucumber-with-selenium

Img. 4.4

Individual Result of Each Scenario:

The individual result of each executed scenarios is shown in the below screenshot 4.3.

individual-scenario-report-automation-testing-cucumber-with-selenium

Img. 4.5