DWQA Ask QuestionCategory: SeleniumData provider to be used for Searching Multiple Products in E Commerce
admin Staff asked 2 years ago

Problem Statement:

  • Create a Test case using test ng annotation for searching( chose any e-commerce web site amazon, flipkart or demo cscart)
  • Validate the search result i.e. what ever product has been search there has to be a validation that correct search result is displayed.
  • Make this test iteratable using Data provider concept of Test ng.
  • Data provider should fetch the list from a external file(properties file, or XML file or Excel file. avoid hard coded values in Data provider methods) and provide the result to the test.
  • Test should be built in such a manner so that no matter how many products are added later on there should not be any code changes in the test. That is test should be generic in nature.

Demo URL: https://demo.cs-cart.com . Also, amzon.com or flipkart can also be used.

Make use of Page Object Model, Test NG and Maven.

5 Answers
Pallavi Gadale Staff answered 2 years ago

DataProvider to be used for searching multiple products on amazon web site
 
PO_SearchProduct class file

public class PO_searchItem {

WebDriver driver;


//initialize constructor
public PO_searchItem(WebDriver d)
{
this.driver=d;
}

//locators
@FindBy(how=How.XPATH,using="//*[contains(@id,'twotabsearchtextbox')]")
private WebElement search_box;

@FindBy(how=How.XPATH,using="//div[@class='nav-search-submit nav-sprite']")
private WebElement click_on_search;


//Methods

public void setSearchbox( String searchProduct)
{
search_box.sendKeys(searchProduct);
}
public void ClickOnSearch()
{
click_on_search.click();
}

}


TC_SearchProduct TestCase


public class TC_searchItem {

WebDriver driver;
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
@Test(dataProvider="searchData")
public void T_01_searchMultipleProducts(String searchProduct)

{
//Create driver object
System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
Reporter.log("Browser Invoked",true);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

//navigate to link
driver.get("https://www.amazon.in/");

PO_searchItem searchproduct=PageFactory.initElements(driver, PO_searchItem.class);
searchproduct.setSearchbox(searchProduct);
searchproduct.ClickOnSearch();

//checkpoints
String Expected_title="Amazon.in:"+ " "+searchProduct;
String Actual_title=driver.getTitle();
Assert.assertEquals( Actual_title,Expected_title,"Product search title is correct");
}

@DataProvider(name="searchData")
public static Object[][] getData () throws Exception{

//create an object of FileInputStream class to read excel file
FileInputStream fis=new FileInputStream("C:\\Users\\MAYUR\\eclipse-workspace\\SearchProductTV.xlsx");

//load Workbook
wb=new XSSFWorkbook(fis);

//load sheet-here i load first sheet only
sheet=wb.getSheet("sheet1");

//gives no. of rows present in excel sheet
int row=sheet.getLastRowNum()+1;
System.out.println(row);

//no.of columns present in sheet
int column=sheet.getRow(0).getLastCellNum();
System.out.println(column);

//store no. of rows and columns present in excel
Object[][] data=new Object[row][column];

//create loop for rows of excel excel file to read it
for(int i=0;i<row;i++){

Row r =sheet.getRow(i);

//create loop to print cell values in a row
for(int j = 0;j<column;j++){
Cell c = r.getCell(j);

//test data storing in data variable
data[i][j] = c.getStringCellValue();}}
return data;

}

}
Bindiya Patil Staff answered 2 years ago

 
 
 
 
 
 
 
 
 
//***Bindiya Patil***//
//****XL file read code****//
package com.product.cscart.Reusable;
import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel_file_read
{
public static XSSFWorkbook wb; //XSSFWorkbook is simply class coming from Apache POI
public static XSSFSheet sheet; //XSSFSheet is simply class coming from Apache POI
public static Object[][] getData(String sheetName) throws Exception
{
File fil=new File(“../Framework/xl_file_data.xlsx”); //excel sheet path from where we can fetch the data
//Create an object of FileInputStream class to read excel file
FileInputStream fis=new FileInputStream(fil);
// complete excel sheet will be loaded by XSSFWorkbook
wb=new XSSFWorkbook(fis);
//
sheet=wb.getSheet(sheetName); //loaded the specific excel sheet
int row=sheet.getLastRowNum(); // give number of rows present in excel sheet
System.out.println(row);
int cellNum=sheet.getRow(0).getLastCellNum();//give number of column present in excel sheet
System.out.println(cellNum);
Object[][] data=new Object[row][cellNum]; // store number of rows and column present in excel sheet
//Create a loop over all the rows of excel file to read it
for(int i=0;i<row;i++)
{
//Create a loop to print cell values in a row
for(int k=0;k<cellNum;k++)
{
data[i][k]=sheet.getRow(i+1).getCell(k).toString(); //Test data storing in data[][] variable
}
}
return data;
}
}
 
// ***POM for search product****//
package com.product.cscart.PO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class Search_cscart_PO {
WebDriver driver;
/*public Search_cscart_PO(WebDriver search)
{
this.driver=search;
}*/
@FindBy(how=How.ID,using=”search_input”)
WebElement search1;
@FindBy(how=How.XPATH,using=”//button[@title=’Search’]”)
WebElement searchclick;
public void search(String arg)
{
search1.clear();
search1.sendKeys(arg);
}
public void click()
{
searchclick.click();
}
}
 
// *****test case for search by using dataprovider*****//
package com.product.cscart.TC;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.Base_test.Browser_invoke;
import com.Base_test.propertyfile1;
import com.product.cscart.PO.Search_cscart_PO;
import com.product.cscart.Reusable.Excel_file_read;
import com.product.cscart.Reusable.Excel_file_write;
import com.product.cscart.Reusable.Screenshot;
public class TC_Cscart_Search extends Browser_invoke {
@Test(dataProvider=”cscart_search”)
public void validate_search(String product)
{
Search_cscart_PO cartSearch=PageFactory.initElements(driver, Search_cscart_PO.class);
cartSearch.search(product);
cartSearch.click();
}
@DataProvider(name=”cscart_search”)
public Object[][] cscart_login() throws Exception
{
Object[][] data=Excel_file_read.getData(“Sheet4”);
return data;
}

}

VaibhavD Staff answered 2 years ago

Using Property file—————
search.propertires
product = tv, laptop, mobile

 
TC_PropertyFile_DataProvider.java
public class TC_PropertyFile_DataProvider {

WebDriver driver;
@BeforeMethod
public void setup() throws Exception {
driver = BrowserManager.GetBrowser(“chrome”);
driver.get(“https://demo.cs-cart.com&#8221;);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterMethod
public void tearDown() {
driver.close();
}

@Test(dataProvider=”Data”)
public void searchTheProduct(String product)
{
PO_SearchPage oSearch =PageFactory.initElements(driver, PO_SearchPage.class);
oSearch.enterProductToSearch(product);
oSearch.clickSearchButton();
}

@DataProvider(name=”Data”)
public String[] sendDataToTest() throws Exception
{
String[] data= PropertyFileReader.getProperty(“product”).split(“,”);
return data;
}
}
PropertyFileReader .java
public class PropertyFileReader {
public static String getProperty(String data)
{
String file_path = “search.properties”;
Properties p=new Properties(); //creating object of properties class
try
{
FileInputStream file = new FileInputStream(file_path);//read the propertyfile
p.load(file);
}
catch (Exception e)
{
e.printStackTrace();
}
return p.getProperty(data);
}
}
 
PO_SearchPage.java
public class PO_SearchPage {

WebDriver driver;

public PO_SearchPage(WebDriver search)
{
this.driver=search;
}

@FindBy(how=How.ID, using=”search_input”) WebElement txtbx_search;
@FindBy(how=How.XPATH, using=”//button[@title=’Search’]”) WebElement btn_search;
public void enterProductToSearch(String arg)
{
txtbx_search.sendKeys(arg.toLowerCase());
Reporter.log(“Product Entered to search is ” +arg, true);
}
public void clickSearchButton()
{
btn_search.click();
}
}

Tejal Gadewar Staff answered 2 years ago

//PO_DataProvider
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.testng.Assert;
import org.testng.Reporter;
public class PO_DataProvider {
WebDriver driver;
public PO_DataProvider(WebDriver d)
{
this.driver=d;
}

//elements

@FindBy(how=How.ID, using=”search_input”)
WebElement txtbox_srchbox;
@FindBy(how=How.XPATH, using=”//button[@title=’Search’]”)
WebElement btn_search;
@FindBy(how=How.XPATH, using=”//span[@class=’ty-mainbox-title__left’]”)
WebElement vald_searchreslt;

//methods

public void searchProducts(String product) throws Exception
{

txtbox_srchbox.sendKeys(product);
btn_search.click();
Reporter.log(“search product with ” +product+”<br>”);
WebElement searchprd=driver.findElement(By.xpath(“//span[@class=’ty-mainbox-title__left’]”));
String actual=searchprd.getText();
String expected=”Search results”;
Assert.assertEquals(actual,expected);
Reporter.log(“search result found”);
}

}
 
Test Case for DataProvider
 
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import po.PO_DataProvider;
import utility.ExcelManager;
public class TC_DataProvider {

WebDriver driver;

@BeforeMethod
public void webdrivermanager() throws Exception
{
System.setProperty(“webdriver.chrome.driver”, “F:\\automation\\chromedriver.exe”);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“https://demo.cs-cart.com&#8221;);
Reporter.log(“URL launched<br>”);
}

@Test(dataProvider = “dp”)
public void searchproducts(String product) throws Exception
{
PO_DataProvider po_dp=PageFactory.initElements(driver,PO_DataProvider.class);
po_dp.searchProducts(product);

}
@DataProvider
public Object[][] dp() throws Exception {
Object[][] Data=ExcelManager.fetchexceldata();
return Data;
}
}
 
//Excel Manager For Fetch Data From Excel File
 
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelManager {
public static Object[][] fetchexceldata() throws Exception {
File f= new File(“C:\\Users\\Sumeet Moralwar\\Desktop\\demo_cs_cart.xlsx”); //excel file path with name
FileInputStream fs=new FileInputStream(f);
XSSFWorkbook wb=new XSSFWorkbook(fs);
Sheet sheet=wb.getSheet(“Sheet1”); //sheet name
Row r1=sheet.getRow(0);
int lastClmNum=r1.getLastCellNum();
int lastRowNum=sheet.getLastRowNum();
Object[][] fullData=new Object[lastRowNum][lastClmNum];

for(int i=0;i<lastRowNum;i++)
{
Row r=sheet.getRow(i);
int rowClmNum=r.getLastCellNum();
for(int j=0; j<rowClmNum; j++)
{
fullData[i][j]=r.getCell(j).getStringCellValue();
}
}

return fullData;
}
}

Supriya Kitukale Staff answered 2 years ago

//DataProvider for cs-cart
public class DataProviderforSearch
{
WebDriver driver;
@Test(dataProvider=”Provider”)
public void fetch_excel_data_using_dataprovider(String input) throws IOException
{

System.setProperty(“webdriver.chrome.driver”, “E:\\Vision IT\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“https://demo.cs-cart.com&#8221;);
WebElement element=driver.findElement(By.id(“search_input”));
element.sendKeys(input);
driver.findElement(By.xpath(“//button[@title=’Search’]”)).click();
}
@DataProvider(name=”Provider”)
public Object[][]GetExcelData() throws IOException
{
File f=new File(“E:\\Study material\\CART.xlsx”); //file path
FileInputStream fs=new FileInputStream(f);
XSSFWorkbook wb=new XSSFWorkbook(fs);
XSSFSheet sheet =wb.getSheet(“product”);
Row r1= sheet.getRow(0);
int col=r1.getLastCellNum();
int lastRowNumber=sheet.getLastRowNum();

Object[][] fullData=new Object[lastRowNumber][col];
for(int i=0;i<lastRowNumber;i++)
{
Row r= sheet.getRow(i);
int Rowcol=r.getLastCellNum();
for(int j=0;j<Rowcol;j++)
{
fullData[i][j]=r.getCell(j).getStringCellValue();
}
}
Reporter.log(“Product fetch successfully “);
return fullData;
}
}