如何使用泛型和工厂设计实例化PageObject类

时间:2019-06-23 13:31:41

标签: c# selenium generics factory pageobjects

我正在用C#开发一个硒框架。使用工厂设计实例化页面类以隐藏实例化逻辑,我希望能够使用实例化该类。 var myPageClass = HelperSelenium.Instance(MyPageClass)代替var myPageClass = MyPageClass.Instance(driver)

我尝试在HelperSelenium类中创建泛型函数,但是我还不能完全理解泛型的工作原理。

PageObject类示例(注意,我使用Instance方法实例化该类

public class HomePage : BasePage
{       
    private HomePage(IWebDriver driver, CountryEnum enum) : base(driver) { }

    private static HomePage homePage { get; set; }

    public static HomePage Instance(IWebDriver driver, CountryEnum enum)
    {
        switch (enum)
        {
             case CountryEnum.Sweden:
                 HomePage = new HomePage_SWE(driver);
                 break;
             case CountryEnum.Germany:
                 HomePage = new HomePage_GER(driver);
                 break;
             case CountryEnum.Italy:
                 HomePage = new HomePage_ITA(driver);
                 break;
             default:
                 HomePage = new HomePage_UK(driver);
                 break;
        }

        return homePage;
    }

...
}
public T InstancePage<T>() where T : BasePage
{
    return (T).Instance(WebDriver, CountryEnum.Sweden);
}
Error   CS0119  'T' is a type, which is not valid in the given context  InfrastructureSelenium  C:\testSelenium\Infrastructure\Helper\HelperSelenium.cs 140 Active
Error   CS0119  'T' is a type parameter, which is not valid in the given context    InfrastructureSelenium  C:\testSelenium\Infrastructure\Helper\HelperSelenium.cs 140 Active

1 个答案:

答案 0 :(得分:0)

最后我使用以下方法解决了

compose

我很确定可以通过使用PageObject.PageFactory

来改善它