第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用泛型和工廠設(shè)計實例化 PageObject 類

如何使用泛型和工廠設(shè)計實例化 PageObject 類

C#
慕姐8265434 2023-04-29 15:46:53
我正在用 c# 開發(fā)一個 selenium 框架。頁面類使用工廠設(shè)計實例化以隱藏實例化邏輯,我希望能夠使用 var myPageClass = HelperSelenium.Instance(MyPageClass)而不是實例化類var myPageClass = MyPageClass.Instance(driver)我已經(jīng)嘗試在 HelperSelenium 類中創(chuàng)建一個泛型函數(shù),但我還不能完全理解泛型是如何工作的。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 ActiveError   CS0119  'T' is a type parameter, which is not valid in the given context    InfrastructureSelenium  C:\testSelenium\Infrastructure\Helper\HelperSelenium.cs 140 Active
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經(jīng)驗 獲得超4個贊

最后我通過使用修復(fù):


        public T InstantiatePage<T>() where T : class

    {

        // COUNTRY code as defined in

        // https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements

        var className = string.Format("{0}_{1}", typeof(T).FullName, getCulture()); 


        // T will be base class if no derived class with COUNTRY code suffix is found 

        var classType = Type.GetType(className) ?? typeof(T);


        // Instantiate the page

        var instance = Activator.CreateInstance(classType, new object[] { WebDriver });


        return (T)instance;

    }

我很確定使用 PageObject.PageFactory 可以改善它


查看完整回答
反對 回復(fù) 2023-04-29
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號