0

This is my code:

Excel util:

public static Object [][] getTestData(String sheetName) 
{
    try 
    {
        FileInputStream ip = new FileInputStream(TESTDATA_SHEET_PATH);

        try 
        {
            book = WorkbookFactory.create(ip);
        } 
        catch (InvalidFormatException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sheet = book.getSheet(sheetName);
            
        Object data[][] = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];

        for (int i = 0; i < sheet.getLastRowNum(); i++)
        {
            for (int k = 0; k < sheet.getRow(0).getLastCellNum(); k++)
            {
                // if(data[i][k]!=null)
                data[i][k] = sheet.getRow(i+1).getCell(k).toString();
            }
        }

        return data;
    }
    catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Not able to fetch the values from the Excel");
    }

    return null;
}
    

Page method where I need to select multiple values from the drop down:

public void pipeline(String fieldvalue,String savepipe)
{
    elementutils.waitforElementPresent(DealsLink);
    elementutils.doclick(DealsLink);
    elementutils.waitforclickingElement(pipeline);
    elementutils.doclick(pipeline);
    elementutils.waitforElementPresent(Selectfieldsdropdownclick);
    elementutils.doclick(Selectfieldsdropdownclick);
    elementutils.selectvaluefromdropdown(selectfieldsvalueselection, fieldvalue);
}

Test page method:

@DataProvider
public Object[][] dealpipeline()
{
        Object data[][] = ExcelUtil.getTestData(AppConstants.Deal_Pipeline_Sheet_Name);
        return data;
}
    
@Test(priority=10,dataProvider="dealpipeline")
public void getdealspipelineinfo(String selectfields,String savepipelineas)
{
    dealspage.pipeline(selectfields, savepipelineas);
}

Page: has a drop down where we can select two values one at a time from the drop down

![enter image description here][1]

The screenshot shows the dropdown

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

Excel file with the values select fields Stage, Commission.

When I run this test it does not select the values that are there in the Excel and nor does it show any error. Can someone please let me know what needs to be done?

sandy
  • 13
  • 6
  • Cross check that you are able to read the data from excel and all looks there. So we can check other areas. – Dilip Meghwal Sep 14 '20 at 11:40
  • if there is one value entered in the excel it reads the value but if there are 2 values entered where in the values are seperated with a comma then it does not show the values in the drop down.What can i do in this case kindly suggest – sandy Sep 14 '20 at 12:39
  • you mean to say the data in the excel sheet can be eg. `user0` or `user0,user1`. if this is the case then the issue is in `pipeline` function at `elementutils.selectvaluefromdropdown(selectfieldsvalueselection, fieldvalue);` line. – Dilip Meghwal Sep 14 '20 at 13:21
  • this line of code is the issue any suggestion on what could be the solution – sandy Sep 14 '20 at 14:50
  • Check the updated answer, its working fine now. **Note** at a time you can select only two values from the drop down. – Dilip Meghwal Sep 19 '20 at 06:30

1 Answers1

0

You can use below approach to select the values in drop down.

public void pipeline(String fieldvalue,String savepipe){
    String[] str = fieldvalue.split(",");
    for(int i=0; i<str.length; i++) {
        //performed the required operations as per requirement by accessing the value using str[i]
    }
}

Edited :

public void multiselectdropdown(By locator,String value) { 
    String[] valueTemp = value.split(",");
    for(int i=0;i<valueTemp.length;i++) { 
        List<WebElement> dropdownoptions = driver.findElements(locator); 
        for(int j=0; j<dropdownoptions.size(); j++) {
            String text = dropdownoptions.get(j).getText();
            try { 
                if(!text.isEmpty()) { 
                    if(text.equalsIgnoreCase(valueTemp[i])) { 
                        dropdownoptions.get(j).click(); 
                        break; 
                    } 
                } 
            }catch (Exception e) { }
        }
    }
}

**Note: You can select max two values from drop down, so update the code as per your requirement. You were seeing stale element exception because the drop down is getting updated on each selection.

Dilip Meghwal
  • 632
  • 6
  • 15
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/221514/discussion-on-answer-by-dilip-meghwal-how-to-select-2-or-more-values-from-the-dr). – Samuel Liew Sep 15 '20 at 12:37