Wednesday, 22 June 2022

Selenium | Adding table data elements to List using findElements | Adding WebElement elements to ArrayList | Compare WebElement elements with ArrayList elements

Hi

Problem Statement: 

Compare the below table row data elements(expected results) with actual elements(actual results) in selenium ? 

Solution: 
1) Find the td elements of the tr using findElements method and add them to List<WebElement>. This would become the actual elements from the web page. 
2) Take a ListArray for the expected results and add the elements using add method
3) Now compare the elements by converting both web elements and array elements. 

In the code snippet below, additionally converted the WebElement to ArrayList and the size of these are displayed using size() method. 

Github:  https://github.com/sadakar/CucumberSeleniumTestNG.git
Project is based on : Cucumber , TestNG frameworks. 




Cucumber Gherkin script:
@tag
Feature: Login to HRM Application
  I want to use this template for my feature file

  Background: 
    Given User is on home page
    When User enters username as "Admin"
    And User enters password as "admin123"

  @List
  Scenario: Login with valid credentials
    Then User finds the list of quick launch elements

Step definitions:
package com.sadakar.stepdefinitions;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.sadakar.common.BasePage;

import io.cucumber.java.en.Then;

public class QuickLaunchWebElementsList extends BasePage {

	@Then("User finds the list of quick launch elements")
	public void User_finds_the_list_of_quick_launch_elements() {

		// Adding table data of a row to WebElement List
		List<WebElement> actualListOfQuickLaunchElements = driver
				.findElements(By.xpath("//*[@id=\"dashboard-quick-launch-panel-menu_holder\"]/table/tbody/tr/td"));

		// Display the table data of row from the WebElementList
		for (WebElement ele : actualListOfQuickLaunchElements) {
			System.out.println(ele.getText());
		}

		// Display the size of WebElement List
		System.out.println("Size of Quick launch elements : " + actualListOfQuickLaunchElements.size());

		// Adding WebElements List  to a ArrayList
		List<String> quickLaunchElementsArrayList = new ArrayList<String>();
		for (WebElement ele : actualListOfQuickLaunchElements) {
			quickLaunchElementsArrayList.add(ele.getText());
		}
		// Displaying the WebElements from the ArrayList
		for (WebElement s : actualListOfQuickLaunchElements) {
			System.out.println(s.getText());
		}
		// Size of the ArrayList
		System.out.println("Size of Array list : " + quickLaunchElementsArrayList.size());
		
		
		//Preparing expected list of elements
		@SuppressWarnings("serial")
		List<String> expecteListOfQuickLaunchElements= new ArrayList<String>() {
			{
			add("Assign Leave");
			add("Leave List");
			add("Timesheets");
			add("Apply Leave");
			add("My Leave");
			add("My Timesheet");
			}
		};
		
		// comparing actual list with expected list 
		for(int i=0;i<actualListOfQuickLaunchElements.size();i++) {
			String actualLabels = actualListOfQuickLaunchElements.get(i).getText();
			String expectedLabels = expecteListOfQuickLaunchElements.get(i);
			Assert.assertEquals(actualLabels, expectedLabels);
		}
	}
}

I hope this helps some in the community!

No comments:

Post a Comment