【问题标题】:Selenium C# How do I call a method from another class both classes inherit from a base classSelenium C#如何调用另一个类的方法两个类都继承自基类
【发布时间】:2018-04-22 15:23:08
【问题描述】:

我在查看其中一堂课的方法时遇到了麻烦。 我创建了一个 MobileLoginPage 类,我想单击菜单按钮。 我注意到已经定义了一个方法来单击名为 MobileNavigationPage 的类中的菜单按钮。该方法称为 ClickMenuNavigation

由于这个方法已经存在,我应该可以从我的 MobileLoginPage 类中调用它。避免重复代码。 当我编写代码 MobileNavigationLogin 时,我无法调用它。 没有方法出现。 我什至尝试通过定义一个方法(getMenuNavigationElement)来调用定位器元素来返回定位器。我在 LoginPage 类中看不到这个定位器。

类已经在BasePage类中实例化了,不知道为什么看不到它的方法。

我的代码 sn-p 是:

BasePage 和我们所有的类都在这里实例化:

public class BasePage : SpecflowBaseTest

{
    internal MobileNavigationPage MobileNavigationPage => new 
    MobileNavigationPage(Browser);
    internal MobileLoginPage MobileLoginPage => new 
    MobileLoginPage(Browser);
}

MobileNavigationPage 是:

public void MobilenavigationPage : BasePage
{
    #region Elements

    [FindsBy(How = How.Id, Using = "MobileMenuNavigation")]
    private IWebElement MenuButton {get; set;}

    #endregion

    #region Actions

    private void ClickMenuNavigation()
    {
        Actions.Click.Element(Browser, MenuButton);

    }

    private IWebElement getMenuNavigationElement()
    {
        return MenuButton;
    }

    #endregion 
}

MobileLoginPage 是:

public class MobileLoginPage : BasePage
{
    # region Actions

    public void ClickBrandFromMenuNavigation{}
    {
        // I want to call ClickMenuNavigation() from here                   
    }

    #endregion
}

如何从 LoginPage 类调用 MobileNavigationPage 中定义的方法?

【问题讨论】:

    标签: c# selenium selenium-webdriver webdriver


    【解决方案1】:

    方法是private,这意味着它只能在MobilenavigationPage 中使用。

    你可以把它设为public,然后它就会显示出来 --

    public void ClickBrandFromMenuNavigation()
    {
        base.MobileNavigationPage.ClickMenuNavigation();                   
    }
    

    【讨论】:

    • 我已尝试将其公开并尝试使用受保护。它仍然没有出现
    • 啊,我还没有尝试使用 base.MobileNavigationPage。我会试试这个
    • 抱歉,还是不行。如果我输入基础。它看不到 MobileNavigationPage。我公开了方法
    • BasePage 是否在同一个程序集中?尝试将其属性设为public(或至少protected)。
    猜你喜欢
    • 2016-01-13
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多