我第一次使用带有C#的Selenium进行自动化测试。我是this link的新手,正在按照一些说明进行操作。但是,它不起作用。它说1测试失败。我有以下代码:
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) ==========
我已经搜索了多个网站以找到答案,但没有成功。怎么了?我该怎么办?
编辑
我提供了一些屏幕截图。
答案 0 :(得分:1)
在查看错误消息屏幕截图时,需要在测试中初始化驱动程序时指定chromedriver.exe的位置。
IWebDriver driver = new ChromeDriver("location_of_chromedriver.exe");
答案 1 :(得分:1)
driver.FindElement(By.LinkText("Dismiss").Click();
一种简单的方法是使用静态的等待方法,如下所示:
Task.Delay(2000).Wait();
您还需要添加:
using System.Threading.Tasks;
“ 2000”是您要等待的毫秒数。
一种更动态的等待方式是,首先创建一个wait方法,然后在您想wait
进行某些特定操作时调用该方法(在这种情况下,等待帐户链接可单击)。
创建动态等待方法并使用它,如下所示:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("By.LinkText("My Account"))).Click();
对于这种方法,您还需要:using OpenQA.Selenium.Support.UI;