Question

I have a method I want to be able to return a list of IWebElements, a list of just the names of the elements or a string array. Is it possible to return multiple data types with one method? Is there a more feasible way to get the different return types without using just one method?

/// <summary>
/// Gets all options belonging to this selected tag
/// </summary>
/// <returns>Returns a list of IWebElements</returns>
public List<IWebElement> SelectAllOptions(IWebDriver driver, ref DataObject masterData)
{
    //Get the ID of the dropdown menu
    DatabaseRetrieval.GetObjectRepository(ref masterData);
    var strDropMenuId = masterData.DictObjectRepository["ID"];
    //Find the dropdown menu and pull all options into a list
    try
    {
        var dropMenu = new SelectElement(driver.FindElement(By.Id(strDropMenuId)));
        return dropMenu.Options as List<IWebElement>;
    }
    catch (NoSuchElementException exception)
    {
        masterData.Logger.Log(Loglevel.Error, "Boom: {0}", exception.Message);
    }
    masterData.Logger.Log(Loglevel.Debug, "No options found for DropDownMenu: {0}", strDropMenuId);
    return null;
}
Was it helpful?

Solution 3

You can change the return type to a

Dictionary<IWebElement, string>

Once you have the compiled the list of IWebElements, then you can simply add the associated string with the IWebElement into a Dictionary to return - whoever calls your method will have both the IWebElement and the string for processing.

var options = dropMenu.Options as List<IWebElement>;
if (options != null)
{
    var values = options.ToDictionary(option => option, option => option.Text);
    return values;
}

OTHER TIPS

You can't return multiple types. You can however:

  1. Create a type that holds all the possible return types
  2. Use .Net's Tuple instead
  3. Have out parameters.

But that's a very bad design decision and you should really consider avoiding it. (It usually means your method does more than it should)

Yes but You have to use the out paramaters like the following

 public List<IWebElement> SelectAllOptions(IWebDriver driver, ref DataObject masterData, out SomeType returnResult1, out SomeType returnResult2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top