'System.Dynamic.ExpandoObject' does not contain a definition for 'UserName' in Specflow C#

时光毁灭记忆、已成空白 提交于 2021-01-07 02:35:27

问题


I am getting an error that says:

'System.Dynamic.ExpandoObject' does not contain a definition for 'UserName'

Here is my code also it Specflow, Selenium, and Visual Studio

Scenario

Feature: OasisLogin
    Login to Oasis application

Scenario: Perform Login to Oasis application site
    Given I launch the application
    #And I click login link
    And I enter the following details
    | UserID   | Password |
    | admin    | password |
    And I click login button
    Then I should see Dashboard details link

Step Definition

namespace OasisSpecFlowTest.StepDefinition
{
    [Binding]
    public sealed class OasisSteps
    {
        OasisLoginPage oasisLoginPage = null;

        [Given(@"I launch the application")]
        public void GivenILaunchTheApplication()
        {
            IWebDriver webDriver = new ChromeDriver();
            webDriver.Navigate().GoToUrl("https://oasis.com/Login.aspx");
            oasisLoginPage = new OasisLoginPage(webDriver);
        }

        //[Given(@"I click login link")]
        //public void GivenIClickLoginLink()
        //{
           // oasisLoginPage.ClickLogin();
        //}

        [Given(@"I enter the following details")]
        public void GivenIEnterTheFollowingDetails(Table table)
        {
            dynamic data = table.CreateDynamicInstance();
            oasisLoginPage.Login((string)data.UserId, (string)data.Password);
        }

        [Given(@"I click login button")]
        public void GivenIClickLoginButton()
        {
            oasisLoginPage.ClickLoginButton();
        }

        [Then(@"I should see Dashboard details link")]
        public void ThenIShouldSeeDashboardDetailsLink()
        {
            Assert.That(oasisLoginPage.IsEmployeeDetailsExist(), Is.True);
        }

    }
}

namespace OasisSpecFlowTest.StepDefinition
{
    [Binding]
    public sealed class OasisSteps
    {
        OasisLoginPage oasisLoginPage = null;

        [Given(@"I launch the application")]
        public void GivenILaunchTheApplication()
        {
            IWebDriver webDriver = new ChromeDriver();
            webDriver.Navigate().GoToUrl("https://oasis.com/Login.aspx");
            oasisLoginPage = new OasisLoginPage(webDriver);
        }

        //[Given(@"I click login link")]
        //public void GivenIClickLoginLink()
        //{
           // oasisLoginPage.ClickLogin();
        //}

        [Given(@"I enter the following details")]
        public void GivenIEnterTheFollowingDetails(Table table)
        {
            dynamic data = table.CreateDynamicInstance();
            oasisLoginPage.Login((string)data.UserId, (string)data.Password);
        }

        [Given(@"I click login button")]
        public void GivenIClickLoginButton()
        {
            oasisLoginPage.ClickLoginButton();
        }

        [Then(@"I should see Dashboard details link")]
        public void ThenIShouldSeeDashboardDetailsLink()
        {
            Assert.That(oasisLoginPage.IsEmployeeDetailsExist(), Is.True);
        }

    }
}

Selenium Page

namespace OasisSpecFlowTest.Pages
{
    class OasisLoginPage
    {
        public IWebDriver WebDriver { get; }

        public OasisLoginPage(IWebDriver webDriver)
        {
            WebDriver = webDriver;
        }

        //UI Elements
        //public IWebElement InkLogin => WebDriver.FindElement(By.LinkText("Login"));

        public IWebElement TxtUserID => WebDriver.FindElement(By.Name("User ID"));

        public IWebElement TxtPassword => WebDriver.FindElement(By.Name("Password"));

        public IWebElement BtnLogin => WebDriver.FindElement(By.CssSelector(".btn-default"));

        public IWebElement InkEmployeeDetails => WebDriver.FindElement(By.LinkText("Employee Details"));

        //public void ClickLogin() => InkLogin.Click();

        public void Login(string userid, string password)
        {
            TxtUserID.SendKeys(userid);
            TxtPassword.SendKeys(password);
        }

        public void ClickLoginButton() => BtnLogin.Submit();

        public bool IsEmployeeDetailsExist() => InkEmployeeDetails.Displayed;
    }
}

Also the same code but this time with UserID??

Screenshot of the exception message

Here is the errors it shown

dynamic data = table.CreateDynamicInstance();
oasisLoginPage.Login((string)data.UserName, (string)data.Password);

I want to try both.


回答1:


I figure it out I have change my code public void Login(string UserID, string Password) { TxtUserName.SendKeys(UserID); TxtPassword.SendKeys(Password); And Step Definition dynamic data = table.CreateDynamicInstance(); oasisLoginPage.Login((string)data.UserID, (string)data.Password);



来源:https://stackoverflow.com/questions/65225328/system-dynamic-expandoobject-does-not-contain-a-definition-for-username-in-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!