我写了一个非常基本的硒测试。我的测试返回页面的页面标题,但是如果我现在想更进一步,并根据我期望的方式验证页面标题,那么该怎么做?
因此,例如,如果我希望标题为“ aaa”,但返回的内容却不同,我希望我的测试失败。
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 test1
{
static void Main()
{
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
webDriver.Manage().Window.Maximize();
String title = webDriver.Title;
Console.WriteLine("Title of webpage is " + title);
// webDriver.Quit();
}
}
}
答案 0 :(得分:0)
您可以将其进行比较
String expectedTitle = "Utilities from Go Compare";
if(title.contains(expectedTitle)){
Console.writeline("Title is matching with expected value");
}
else{
Console.writeline("Title is not matching with expected value");
}
或者您可以使用NUnit
这样的断言方法
Assert.AreEqual(true, title.contains(expectedTitle), "Title is not matching");
答案 1 :(得分:0)
您可以使用此代码
String expectedtitle = "type expected title";
String actualtitle = driver.FindElement(By.XPath("copy xpath perticuler title")).Text.ToString();
Assert.AreEqual(expectedtitle, actualtitle);