简体   繁体   中英

Error CS0246: The type or namespace name 'WebDriverWait' could not be found?

I am new to creating tests in visual studio using selenium and C#. I have two files, one is search engine maing page and the other is a testing file. The files can be found here--- https://www.automatetheplanet.com/page-object-pattern/

I always get this error; Below are screenshots of the problem.

在此处输入图片说明 在此处输入图片说明

Error CS0246: The type or namespace name 'WebDriverWait' could not be found?

What am I doing wrong??

below is the following code-

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

[TestClass]
public class SearchEngineTests
{
    public IWebDriver Driver { get; set; }
    public WebDriverWait Wait { get; set; }

    [TestInitialize]
    public void SetupTest()
    {
        this.Driver = new FirefoxDriver();
        this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(30));
    }

    [TestCleanup]
    public void TeardownTest()
    {
        this.Driver.Quit();
    }

    [TestMethod]
    public void SearchTextInSearchEngine_First()
    {
        SearchEngineMainPage searchEngineMainPage = new SearchEngineMainPage(this.Driver);
        searchEngineMainPage.Navigate();
        searchEngineMainPage.Search("Automate The Planet");
        searchEngineMainPage.ValidateResultsCount("264,000 RESULTS");
    }
}

This is the second file-

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

public class SearchEngineMainPage
{
    private readonly IWebDriver driver;
    private readonly string url = @"searchEngineUrl";

    public SearchEngineMainPage(IWebDriver browser)
    {
        this.driver = browser;
        PageFactory.InitElements(browser, this);
    }

    [FindsBy(How = How.Id, Using = "sb_form_q")]
    public IWebElement SearchBox { get; set; }

    [FindsBy(How = How.Id, Using = "sb_form_go")]
    public IWebElement GoButton { get; set; }

    [FindsBy(How = How.Id, Using = "b_tween")]
    public IWebElement ResultsCountDiv { get; set; }

    public void Navigate()
    {
        this.driver.Navigate().GoToUrl(this.url);
    }

    public void Search(string textToType)
    {
        this.SearchBox.Clear();
        this.SearchBox.SendKeys(textToType);
        this.GoButton.Click();
    }

    public void ValidateResultsCount(string expectedCount)
    {
        Assert.IsTrue(this.ResultsCountDiv.Text.Contains(expectedCount), "The results DIV doesn't contains the specified text.");
    }
}

And also getting the below error

  1. Error CS1729: 'SearchEngineMainPage' does not contain a constructor that takes 1 arguments (CS1729) (testingProgram)

  2. Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)

#1: WebDriverWait is available in the Selenium.Support package. So, Please import the below package in SearchEngineTests Test class.

using OpenQA.Selenium.Support.UI;

#2: TimeSpan is available in the default System namespace.so, please add the below namespace as well

using System

Regarding to add the required namespace, If you are seeing any red color underlined code in your program, then move the cursor towards that particular field. It will give the suggestion to add the relevant namespace

#3: Regarding the SearchEngineMainPage constructor error. Please add the constructor in SearchEngineMainPage class with agument as driver. It looks, already you are having the argument constructor. Whether you are still getting this error?

认为您using OpenQA.Selenium.Support.UI;缺少名称空间using OpenQA.Selenium.Support.UI;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM