#SpecFlow# For CreateDynamicInstance() - error - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : The best overloaded method

Viewed 257

I am using Specflow 3 with Selenium for Page Object Model pattern. Below is the code:

FeatureFile

When I want to edit the profile 
    | Hats | Shoes |
    | 98   | 99    |

Page class-

public IWebElement TxtHat => _driver.FindElement(By.Id("Hat"));

public IWebElement TxtShoe => _driver.FindElement(By.Id("Shoe"));

public void FillDetails (string hat, string shoe)
{
    TxtHat.SendKeys(hat);
    TxtShoe.SendKeys(shoe);
}

StepDefinition -

[When(@"I want to edit the profile")]
public void WhenIWantToEditTheProfile(Table table)
{
    profile.ClearDetails();
    dynamic var = table.CreateDynamicInstance();
    profile.FillDetails(var.Hats, var.Shoes);
}

Error

Message: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : The best overloaded method match for 'RefreshESS.Pages.UpdateProfilePage.FillDetails(string, string)' has some invalid arguments

Thank you for help in advance!

1 Answers

You need to use a "vertical" table instead:

When I want to edit the profile 
    | Field | Value |
    | Hats  | 98    |
    | Shoes | 99    |

The "horizontal" layout of the data table, where the column headers should be property names will only work when creating a set or collection of objects. Since you want a single object, you need a table with two columns. Column #1 is the key or property name in your dynamic object, and the second column is the property value.

More info on converting data tables to objects: https://specflow.org/documentation/SpecFlow-Assist-Helpers/

Please note that the documentation talks about converting data tables to concrete classes, but the same basic philosophy applies to converting data tables to dynamic objects as well.

Related