1

I have already used the data driven approach and executed the test cases using excel file. I mean the data is extracted from excel file. Now i want to extract the data from xml file and execute the test case. Could someone please help me out. Below is the code i have written for reading the excel file and executing the same.

Excel:
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/5v3bz.png

Code:

package Test_package;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class datadriven_coop {
    WebDriver driver;
    ExtentReports extent;
    ExtentTest logger;
    String baseUrl, nodeUrl;

    @BeforeTest
    public void startReport(){

        extent = new ExtentReports (System.getProperty("user.dir") +"/test-output/STMExtentReport.html", true);

        extent
                .addSystemInfo("Host Name", "SoftwareTestingMaterial")
                .addSystemInfo("Environment", "Automation Testing")
                .addSystemInfo("User Name", "Pratiksha Salunkhe");


                extent.loadConfig(new File(System.getProperty("user.dir")+"/extent-config.xml"));
    }
    @BeforeMethod
      public void beforeMethod() throws MalformedURLException, InterruptedException {



    System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
    driver = new ChromeDriver();
      }

    @DataProvider(name="Cooplogin")
    public Object[][] loginData() throws BiffException, IOException {
        Object[][] arrayObject = getExcelData("C:\\Standalone\\ABC.xls","Sheet2");
        return arrayObject;
    }


    public String[][] getExcelData(String fileName, String sheetName) throws BiffException, IOException {
        String[][] arrayExcelData = null;
        try
        {
            FileInputStream fs = new FileInputStream(fileName);
            Workbook wb = Workbook.getWorkbook(fs);
            Sheet sh = wb.getSheet(sheetName);

            int totalNoOfCols = sh.getColumns();
            int totalNoOfRows = sh.getRows();
            System.out.println(totalNoOfCols);
            System.out.println(totalNoOfRows);

            arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];

            for (int i= 1 ; i<totalNoOfRows; i++) {

                for (int j=0; j<totalNoOfCols; j++) {
                    arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
                }

            }


        }


    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        e.printStackTrace();
    } catch (BiffException e) {
        e.printStackTrace();
    }
            return arrayExcelData;

    }

    @Test(dataProvider="Cooplogin")
    public void Coop_produkt(String Username,String password,String Category,String Subcategory,String Category1,String Subcategory1) throws InterruptedException {

        logger = extent.startTest("Coop_produkt");
     driver.get("https://www.coopathome.ch/de/");

           Thread.sleep(5000);


           //fruits
           driver.findElement(By.xpath(".//*[@id='nav']/ul/li[1]/a/span")).click();
           Thread.sleep(2000);
           driver.findElement(By.xpath(".//*[@id='subnav--item-m_0001']/li[1]/ul[1]/li[1]/a")).click();
           Thread.sleep(2000);
           JavascriptExecutor jse8= (JavascriptExecutor)driver;
           jse8.executeScript("window.scrollBy(0,400)", "");
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();
           Thread.sleep(2000);
           driver.findElement(By.id("j_username")).sendKeys(Username);
           driver.findElement(By.id("j_password")).sendKeys(password);
           driver.findElement(By.id("loginOverlaySubmitLoginFormButton")).click();
           Thread.sleep(2000);

           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();

           //Shampoo & Salad
           Thread.sleep(4000);
          driver.findElement(By.xpath(".//*[contains(text(),'" +Category+ "')]")).click();

           Thread.sleep(4000);
           driver.findElement(By.xpath(".//*[contains(text(),'" +Subcategory+ "')]")).click();
           Thread.sleep(4000);
           driver.findElement(By.xpath(".//*[@id='sortOptions']")).click();
           Select dropdown1=new Select(driver.findElement(By.name("sort")));
           dropdown1.selectByVisibleText("Preis (absteigend)");
           JavascriptExecutor jse7= (JavascriptExecutor)driver;
           jse7.executeScript("window.scrollBy(0,400)", "");
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();
           Thread.sleep(4000);
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();

           //Drinks & Beer
           Thread.sleep(4000);
           driver.findElement(By.xpath(".//*[contains(text(),'" +Category1+ "')]")).click();
           Thread.sleep(4000);
           driver.findElement(By.xpath(".//*[contains(text(),'" +Subcategory1+ "')]")).click();
           Thread.sleep(4000);
           driver.findElement(By.xpath(".//*[@id='sortOptions']")).click();
           Select dropdown2=new Select(driver.findElement(By.name("sort")));
           dropdown2.selectByVisibleText("Preis (absteigend)");
           JavascriptExecutor jse6= (JavascriptExecutor)driver;
           jse6.executeScript("window.scrollBy(0,500)", "");
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/button")).click();;
           Thread.sleep(4000);
           driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[2]/div/div[4]/ul/li[1]/div/div/div[3]/div/div/button[2]")).click();
           Thread.sleep(4000);

           //add to cart
           driver.findElement(By.id("miniCartProceedToCheckoutButton")).click();
           Thread.sleep(4000);
           driver.findElement(By.id("miniCartProceedToCheckoutButton")).click();
           Thread.sleep(4000);

           //Date for delivery
           driver.findElement(By.xpath(".//*[@id='heimlieferung']/div[4]/dl/dd[3]/a")).click();
           driver.findElement(By.id("chooseDeliveryDate_continue_button")).click();
           WebElement t=driver.findElement(By.xpath("html/body/div[3]/div/div[3]/div/dd"));
           if (t.getText().equals("120.40"))
           {
                  System.out.println("Gesamtkosten ist korrekt");
                  logger.log(LogStatus.INFO, "Validierung erfolgreich : Gesamtkosten ist korrekt");

                  } 
           else {
           System.out.println("Gesamtkosten ist nicht korrekt, Erwarteten Gesamtkosten ist "+ t.getText()); 
           }
           JavascriptExecutor jse18= (JavascriptExecutor)driver;
            jse18.executeScript("window.scrollBy(0,500)", "");
            driver.findElement(By.xpath("html/body/div[3]/div/div[1]/div/div/div[5]/form/div[2]/p/a")).click();

            driver.findElement(By.xpath("html/body/div[3]/div/div[2]/div[1]/div[2]/div[2]/span[2]/a[1]")).click();


            //Alert alert=driver.switchTo().alert();
            Thread.sleep(4000);
            if(driver.findElement(By.id("removeAllItemsBtn")).isDisplayed())
            {
                driver.findElement(By.id("removeAllItemsBtn")).click();
            }
            driver.findElement(By.xpath("html/body/div[2]/div/div[2]/nav[2]/div/ul/li[7]/a")).click();
System.out.println("Jetzt ist den Warenkorb leer und Sie können neues Produkt wieder hinzufügen");
logger.log(LogStatus.PASS, "Jetzt ist den Warenkorb leer und Sie koennen neues Produkt wieder hinzufuegen");
            logger.log(LogStatus.PASS, "Coop_produkt Testfall ist erfolgreicht");
         }


    @AfterMethod
    public void getResult(ITestResult result){

        if(result.getStatus() == ITestResult.FAILURE){
            logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
            logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
        }else if(result.getStatus() == ITestResult.SKIP){
            logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
        }

        extent.endTest(logger);
    }


    @AfterTest
    public void endReport(){

                extent.flush();

}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.