C# Assert.AreEqual问题,验证错误为c
我的Assert.AreEqual有问题。我的代码正在验证我预期的验证错误是否正确。但是在我的Assert.AreEqual和validationError中抛出了一个异常 如果继续,然后单击“继续”按钮而不在任何字段中输入任何信息,则可以看到验证错误C# Assert.AreEqual问题,验证错误为c,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我的Assert.AreEqual有问题。我的代码正在验证我预期的验证错误是否正确。但是在我的Assert.AreEqual和validationError中抛出了一个异常 如果继续,然后单击“继续”按钮而不在任何字段中输入任何信息,则可以看到验证错误 WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)); IWebElement error = wait.Until(ExpectedConditi
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
IWebElement error = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
Assert.AreEqual("Excpected validation error text", validationError.TextTrim());
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;
namespace Exercise1
{
class RunPath
{
static void Main()
{
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
webDriver.Manage().Window.Maximize();
String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";
if (title.Contains(expectedTitle))
{
Console.WriteLine("Tile is matching with expected value");
}
else
{
throw new Exception("Title does not match");
}
String someText = webDriver.FindElement(By.XPath("//h1[@class='c-header__heading']")).Text;
String expectedHeader = "Switch today and save on your energy bills";
if (someText.Contains(expectedHeader))
{
Console.WriteLine("title matches");
}
else
{
throw new Exception("Title does not match");
}
webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
Assert.AreEqual("Expected validation error text", validationError.TextTrim());
与此相反:
Assert.AreEqual("Excpected validation error text", validationError.TextTrim());
试试这个:
IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
Assert.AreEqual("Please provide your postcode, as different regions have different fuel prices.", country.Text.Trim());
更新:
如果你想在没有Nunit的情况下完成它。根据我们的讨论
您可以按照以下步骤操作:
IWebElement country = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
String errorMsg = country.Text();
try{
if(errorMsg.Trim().Contains("Please provide your postcode, as different regions have different fuel prices."))
Console.WriteLine("Yes matched");
}
catch(Exception ex)
{
Console.WriteLine("Error msg did not match with the exact error msg");
}
这是什么错误?给出stacktrace,错误消息etcI get名称“Assert”在当前上下文中不存在,并且IWebElement不包含“TextTrim”@L.Dockster的定义:更新了整个答案,请立即检查。Assert and wait在当前上下文中不存在打印此行:Console.WriteLinecountry.Text;问题是=等待和断言用红色下划线