使用Selenium API扩展来添加命令-';(命令)是一个';方法';而是像';键入'

本文关键字:命令 一个 方法 键入 Selenium API 使用 扩展 添加 | 更新日期: 2023-09-27 18:06:34

我目前正在使用Selenium WebDriver API开发web应用程序的UI测试。在我的工作中,我真的觉得缺少"Assert Element Present"类型的命令,在谷歌上搜索后,我发现了一个有人写来添加的扩展。

我已经将其集成到我的一个测试中,但当试图运行测试时,它会抛出错误"(Command(是一个"方法",但使用起来像一个"类型"。我知道这对像我这样的新手来说是一个很常见的问题,但我读了很多关于这个问题的答案,还没有找到一个适用于我的答案——我真的看不出这里出了什么问题。

这是我的分机号:

namespace SeleniumTest
{
    public static class WebDriverExtensions 
    {
     public bool IsElementPresent(IWebDriver driver, By locator) //adds 'IsElementPresent' command which asserts presence of element
        {
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(100));
            try
            {
                driver.FindElement(locator);
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(100));
                return true;
            }
            catch (NoSuchElementException)
            {
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMilliseconds(20000));
                return false;
            }
        }
    }

下面是一个我如何在测试用例中使用它的例子:

 [TestFixture]
   public class SeleniumTest
    {
        private IWebDriver driver;
        [SetUp]
        public void Setup()
        {
            driver=new FirefoxDriver();
            Console.WriteLine("Starting Login Test"); 
        }

        [TestCase]
        public void LoginTest()
        {
            var IsElementPresent = new WebDriverExtensions.IsElementPresent(By.Id("MainContent_MainContent_Panel_Grids"));
            driver.Navigate().GoToUrl("http://www.website.com/account/Login.aspx");
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
            IWebElement query = driver.FindElement(By.Name("LoginForm$UserName"));
            query.SendKeys("username");
            driver.FindElement(By.Name("LoginForm$Password"));
            query.SendKeys("password");
            driver.FindElement(By.Id("LoginForm_Button1"));
            query.Click();
            driver.IsElementPresent(By.Id("MainContent_MainContent_Panel_Grids"));
        }

准确的错误是:

'SeleniumTest.WebDriverExtensions.IsElementPresent(OpenQA.Selenium.IWebDriver, OpenQA.Selenium.By)' is a 'method' but is used like a 'type' - Line 55, Column 60

感谢所有的建议/帮助,如果可能的话,请尽可能基本地解释我哪里出了问题——C#对我来说是新的,我想从我的错误中吸取教训!

使用Selenium API扩展来添加命令-';(命令)是一个';方法';而是像';键入'

此代码中有3个问题:

  1. WebDriverExtensions是静态的,而它的方法IsElementPresent不是。静态类的所有成员也必须是静态的。

  2. 在您得到错误的那一行(第55行(:var IsElementPresent = new WebDriverExtensions.IsElementPresent(By.Id("MainContent_MainContent_Panel_Grids"));您应该删除new关键字。

  3. 方法IsElementPresent接受两个参数——driver和locator,但只传递locator。

修复它的最佳方法是通过在IWebDriver之前添加关键字this,使方法IsElementPresent成为扩展方法,如下所示:

public bool IsElementPresent(this IWebDriver driver, By locator)

并将调用代码更改为:var IsElementPresent = new driver.IsElementPresent(By.Id("MainContent_MainContent_Panel_Grids"));

说明:

new关键字仅用于创建非静态类的实例(对象((静态类不能有实例(。此外,如果您必须创建一个实例,那么您应该在类名(例如var IsElementPresent = new WebDriverExtensions().IsElementPresent(...)(后面放括号,以便它调用构造函数。

使用扩展方法,将第一个参数(用this标记的参数(作为表达式传递到点的左侧(在本例中为driver(,而不是括号内。这只是一个语法糖,但实际上它的行为与用2个参数调用方法完全一样。

相关文章: