Search code examples
javaapache-poisikuli

Sort Excel and paste it on webpage using Java and sikuli


I have written the above code and every time it gives me "single" as output.

import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.sikuli.basics.Settings;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;

public class teste {

    public static void main (String args[]) throws InterruptedException {

        try {
            // Open the Excel file
            FileInputStream fis = new FileInputStream("C:\\test.xls");
            // Access the required test data sheet
            HSSFWorkbook wb = new HSSFWorkbook(fis);
            HSSFSheet sheet = wb.getSheetAt(3);
            // Loop through all rows in the sheet

            for(int count = 0;count<=sheet.getLastRowNum();count++) {

                HSSFRow row = sheet.getRow(4);
                HSSFCell cell = row.getCell(4);
                System.out.println("Running test case " +  cell.getStringCellValue().equalsIgnoreCase("uk"));

                // Run the test for the current test data row

                runTest(row.getCell(3).toString());
            }
            fis.close();
        } 
        catch (IOException e) {
            System.out.println("Test data file not found");
        }  
    }

    public static void runTest(String strSearchString) throws InterruptedException {

        // Start a browser driver and navigate to Google

        WebDriver driver = new FirefoxDriver(); 

        //it will always open private profile

        driver.get("http:\\www.google.com");

        //Using Sikuli

        System.out.println("SCreeeeeen is apppearinggg...wait");
        Thread.sleep(5000);
        Screen s = new Screen();
        Settings.OcrTextSearch = true;
        try {
            s.click("imgs/Search.png", 0);
            System.out.println(strSearchString + " ppp ");
            s.type(strSearchString);
            s.doubleClick("imgs/GSearch.png", 0);
            Thread.sleep(5000);

        }
        catch(FindFailed e){
            e.printStackTrace();
        }

        Thread.sleep(1000);

        driver.quit();
    }
}

This is the Excel sheet which is to be sorted and here I am stuck because I need to search "Type" if the country column from excel is uk or us.

This is the excel sheet which is to be sorted and here I am stuck because I need to search "Type" if the country column from excel is uk or us


Solution

  • In the code it seems that you are using same row each time...

    for(int count = 0;count<=sheet.getLastRowNum();count++){
    
    HSSFRow row = sheet.getRow(4);
    HSSFCell cell = row.getCell(4);
    System.out.println("Running test case " +  cell.getStringCellValue().equalsIgnoreCase("uk"));
    

    for each loop, you are keeping row value as 4 hard coded, it remains same throughout the loop.

    i.e. for count = 0,

    HSSFRow row = sheet.getRow(4);

    and for count =1

    it is again HSSFRow row = sheet.getRow(4);

    instead you need to do it like

    HSSFRow row = sheet.getRow(count);
    

    try this...