How to select 2 or more values from the drop down with Data Driven Approach - this is working now

214 views Asked by At

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?

1

There are 1 answers

1
Dilip Meghwal On

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.