1

私は Selenium を使用しており、javascript を実行するための次の拡張メソッドがあります。

    private const int s_PageWaitSeconds = 30;

    public static IWebElement FindElementByJs(this IWebDriver driver, string jsCommand)
    {
        return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(jsCommand);
    }

    public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            wait.Until(d => d.FindElementByJs(jsCommand));
        }
        return driver.FindElementByJs(jsCommand);
    }

    public static IWebElement FindElementByJsWithWait(this IWebDriver driver, string jsCommand)
    {
        return FindElementByJsWithWait(driver, jsCommand, s_PageWaitSeconds);
    } 

私の HomePage クラスには、次の属性があります。

public class HomePage : SurveyPage
{
    [FindsBy(How = How.Id, Using = "radio2")]
    private IWebElement employerSelect; 

    public HomePage(IWebDriver driver) : base(driver)
    {
    }

    public override SurveyPage FillOutThisPage()
    {
        employerSelect.Click();
        employerSelect.Submit();
        m_driver.FindElement(By.)
        return new Level10Page(m_driver); 
    }
}

ただし、employerSelectはJavascriptによって生成されるため、次のような方法はありますか:

public class HomePage : SurveyPage
{
    const string getEmployerJsCommand= "return $(\"li:contains('Employer')\")[0];";

    [FindsBy(How = How.FindElementByJsWithWait, Using = "getEmployerJsCommand")]
    private IWebElement employerSelect; 

    public HomePage(IWebDriver driver) : base(driver)
    {
    }

    public override SurveyPage FillOutThisPage()
    {
        employerSelect.Click();
        employerSelect.Submit();
        m_driver.FindElement(By.)
        return new Level10Page(m_driver); 
    }
}

本質的に、次のような FindsBy 属性の一部として生の ExecuteJs 呼び出しを置き換えたいと考えています。

    const string getEmployerJsCommand = "return $(\"li:contains('Employer')\")[0];";
    IWebElement employerSelect = driver.FindElementByJsWithWait(getEmployerJsCommand);

次のような FindsBy 属性の一部に

    const string getEmployerJsCommand= "return $(\"li:contains('Employer')\")[0];";

    [FindsBy(How = How.FindElementByJsWithWait, Using = "getEmployerJsCommand")]
    private IWebElement employerSelect; 

そのようなことをするために何を拡張できますか?

4

1 に答える 1

0

残念ながら、SeleniumにFindsByAttributeあります。sealedその結果、それをオーバーライドして新しい動作を追加することはできません。Howそのため、Enum属性クラス全体をオーバーライドせずに新しい値を追加する方法はありません。

したがって、現在やりたいことを行う方法はありません。

ただし、FindsBy を封印することは意図的な設計上の決定ではなく、.NET Selenium が Java Selenium の直接ポートであるという事実の結果だと思います。Java では、すべてのクラスがデフォルトで「封印」されており、オーバーライドできるように明示的に仮想としてマークする必要があります。C# は逆で、ほとんどのクラスは Java-C# への移植の際に、それを許可するかどうかを考慮せずに、sealed とマークされたと思います。

Selenium の最新バージョンまでは、Byクラスも封印されていましたが、これは修正されたばかりで、それを利用して独自の By ルックアップを作成しています。(jQuery セレクターを使用)。同じことをするためにプルリクエストを送信できると確信していますFindsBy

于 2012-12-06T00:49:26.557 に答える