【问题标题】:isElementPresent with custom error message strings for Selenium WebdriverisElementPresent 与 Selenium Webdriver 的自定义错误消息字符串
【发布时间】:2017-08-23 18:58:15
【问题描述】:

我最近在 Visual Studio 中安装了 Selenium Webdriver,我正在尝试创建一个函数来检查 isElementPresent,其中包含基于此处建议的参数的自定义错误消息:

isElementPresent in selenium 2.0

目前我的脚本看起来像

private bool isElementPresent(By by, string Message)
    {
        try
        {
            driver.FindElement(by); ;
            return true;
        }
        catch (NoSuchElementException e)
        {
            System.Diagnostics.Debug.WriteLine(Message);
            return false;
        }
    }

有人知道如何处理这个问题吗?

【问题讨论】:

    标签: c# visual-studio selenium-webdriver


    【解决方案1】:

    我的建议是不要在你的函数中混合验证。使用 NUnit Asserts 验证 boolean 返回。

    您的代码实际上并未使用您引用的链接中建议的方法。它应该看起来像

    private bool isElementPresent(By locator)
    {
        return Driver.FindElements(locator).Any();
    }
    

    并被称为

    Assert.IsTrue(isElementPresent(By.Id("login")), "Verify login button is present");
    

    注意:您可能需要添加 Linq 引用 using System.Linq;,才能使用 .Any()

    【讨论】:

    • -'System.Collections.ObjectModel.ReadOnlyCollection' 不包含接受“System.Collections.ObjectModel.ReadOnlyCollection' 可以找到(您是否缺少 using 指令或程序集引用?)- 由于某些原因,Any 似乎没有正确反应,我不确定为什么。
    • 我不知道您是否使用 Visual Studio,但如果您将鼠标悬停在 Any()(它应该有一个红色的曲线)上,单击“显示可能的修复”,然后添加 using System.Linq;。您缺少的参考是 Linq。
    • 如果这个(或任何)答案有用,请点赞。如果它回答了您的问题,请接受它作为答案,以免它没有得到回答。
    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2015-09-21
    • 2011-04-27
    • 2018-09-02
    • 2021-09-13
    • 2017-12-24
    相关资源
    最近更新 更多