如何使用Selenium和C#修复测试自动化代码?

如何使用Selenium和C#修复测试自动化代码?,c#,selenium,selenium-webdriver,automated-tests,selenium-chromedriver,C#,Selenium,Selenium Webdriver,Automated Tests,Selenium Chromedriver,我第一次使用Selenium和C#进行自动化测试。作为一个初学者,我正在遵循一些指导。然而,它不起作用。它说有一次测试失败了。我有以下代码: using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace OnlineStore.TestCases { class LogInTest { [Test] public void Test()

我第一次使用Selenium和C#进行自动化测试。作为一个初学者,我正在遵循一些指导。然而,它不起作用。它说有一次测试失败了。我有以下代码:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


namespace OnlineStore.TestCases
{
    class LogInTest
    {
        [Test]
        public void Test()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Url = "http://www.store.demoqa.com";

            // Find the element that's ID attribute is 'account'(My Account) 
            driver.FindElement(By.XPath(".//*[@id='account']/a")).Click();

            // Find the element that's ID attribute is 'log' (Username)
            // Enter Username on the element found by above desc.
            driver.FindElement(By.Id("log")).SendKeys("testuser_1");

            // Find the element that's ID attribute is 'pwd' (Password)
            // Enter Password on the element found by the above desc.
            driver.FindElement(By.Id("pwd")).SendKeys("Test@123");

            // Now submit the form.
            driver.FindElement(By.Id("login")).Click();

            // Find the element that's ID attribute is 'account_logout' (Log Out)
            driver.FindElement(By.XPath(".//*[@id='account_logout']/a")).Click();

            // Close the driver
            driver.Quit();

        }
    }
}
以及以下信息:

[10/6/2019 5:05:53 AM Informational] Executing test method 'OnlineStore.TestCases.LogInTest.Test'
[10/6/2019 5:05:53 AM Informational] ------ Run test started ------
[10/6/2019 5:05:54 AM Informational] NUnit Adapter 3.15.1.0: Test execution started
[10/6/2019 5:05:54 AM Informational] Running selected tests in C:\Users\enead\source\repos\OnlineStore\OnlineStore\bin\Debug\OnlineStore.dll
[10/6/2019 5:05:55 AM Informational]    NUnit3TestExecutor converted 1 of 1 NUnit test cases
[10/6/2019 5:05:55 AM Informational] NUnit Adapter 3.15.1.0: Test execution complete
[10/6/2019 5:05:55 AM Informational] ========== Run test finished: 1 run (0:00:01.8817664) ==========
我搜索了多个网站来寻找答案,但没有成功。怎么了?我能做什么?
编辑
我提供了一些截图


查看错误消息屏幕截图,您需要在测试中初始化驱动程序时指定chromedriver.exe的位置

IWebDriver driver = new ChromeDriver("location_of_chromedriver.exe");
  • 此页面顶部有一个横幅阻止“account”元素。 您需要添加一个测试步骤,单击该步骤可“取消”此横幅 首先
  • driver.FindElement(By.LinkText(“disease”)。单击();

  • VisualStudio中的脚本移动速度总是比浏览器快,因此您需要在脚本中添加步骤,等待页面加载后再单击新元素
  • 一种简单的方法是使用如下静态等待方法:

    Task.Delay(2000).Wait();
    
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("By.LinkText("My Account"))).Click();
    
    您还需要添加:
    使用System.Threading.Tasks;

    “2000”是要等待的毫秒数

    一种更为动态的等待方式是,首先创建一个wait方法,然后在您想
    等待
    特定事件发生时调用该方法(在本例中,等待帐户链接可单击)

    创建动态等待方法并使用它,如下所示:

    Task.Delay(2000).Wait();
    
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("By.LinkText("My Account"))).Click();
    

    对于此方法,您还需要:
    使用OpenQA.Selenium.Support.UI;

    此消息并不表示测试失败。如果测试失败,应该会有一条异常消息,请发布。@Guy我刚刚添加了一个代码截图,您需要在左下角向我们显示错误消息。就在其下方,显示“1测试失败”@David这就是它显示的所有内容,它没有任何消息您需要首先在测试资源管理器窗口中单击测试用例。之后,下面的摘要将更改并显示实际的错误消息。我添加了位置。现在它执行了,但我仍然有一个错误。我正在再次添加屏幕截图。