-1

I have successfully read data from an Excel file , consisting of three columns(URL/FQDN , username , password). This Excel Sheet consists of FQDNs of various servers of mine. I have another function that needs to perform certain task on all of these servers. Since I am very new to Selenium/java , I would appreciate any help regarding the fact on how I can open all the url's on different browser windows , and pass username and password for each one of them. And later on I can try to manage the task/same action which I need to perform on all of them Here's the piece of code I have written so far to read Excel Sheet-:

public class modif {

public static FileInputStream fis;
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static String row;
public static XSSFCell cell;



public static void main(String[] args) {
    
    try {
        getCellData();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void getCellData() throws IOException
{
    fis=new FileInputStream("C:\\Users\\windows\\Desktop\\hostname.xlsx");
    wb=new XSSFWorkbook(fis);
    sheet=wb.getSheet("hostname");

    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();

    //Create a loop over all the rows of excel file to read it

    for (int i = 0; i < rowCount+1; i++) {
        
        

        Row row = sheet.getRow(i);

        //Create a loop to print cell values in a row

        for (int j = 0; j < row.getLastCellNum(); j++) {

            //Print Excel data in console

            System.out.print(row.getCell(j).getStringCellValue() + " ");
            
            
            
        }

        System.out.println();
    }
}

}

DP_
  • 1

2 Answers2

0

first of all look at the access modifiers of your functions (always very important!). Functions that you only use in a single class should always be private.

What you want to do is actually not that hard to achieve. Just write a (private) function that you give 3 parameters: url, username and password. You will call this function within your for loop of the getCellData() function. On how you exactly start a new window (or tab which i think would be better imo), should be pretty straight forward. I would have to google to! Just import the Selenium WebDriver lib, and then read into it! There are plenty of funtions to assist in what you want to achieve here.

0

Let's say getCellData() at least return a list of URL for this instance.

Once it gets executed you would have something like this :

Code :

public String[] getCellData() {
        return new String[]{"https://www.google.co.in", "https://www.youtube.com", "https://stackoverflow.com"};
    }

To open every URL in new tab :

@Test
public void testSO() throws InterruptedException {
    try {
        String[] allUrls = getCellData();
        System.out.println(allUrls.length);
        int count = 1;
        for(String url : allUrls) {
            driver.get(url);
            String windowsBefore = driver.getWindowHandle();
            JavascriptExecutor js = (JavascriptExecutor)driver;     
            js.executeScript("window.open('');");
            ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
            driver.switchTo().window(tabs.get(count));
            count = count + 1;
            Thread.sleep(3);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
cruisepandey
  • 28,520
  • 6
  • 20
  • 38