1

I've written code with an IF statement. But all my statement currently does is output whether or not the text I'm expecting is correct or not.

How can I change the implementation so that if the title isn't matching to what I expect the test stops and fails?

 String title = webDriver.Title;

 String expectedTitle = "Utilities from Go Compare";
 if (title.Contains(expectedTitle))
 {
     Console.WriteLine("Tile is matching with expected value");
 }
 else
 {
     Console.WriteLine("Tile is not matching");
 }
4
  • 1
    I don't see any loop here Commented Jul 12, 2018 at 8:55
  • Do you mean you want to throw an exception if the title doesn't match? This could be done with the throw expression. Commented Jul 12, 2018 at 8:59
  • as I say before stackoverflow.com/questions/51282067/… you need to use NUnit for this. This is a library for write tests. Example here swtestacademy.com/selenium-webdriver-csharp-nunit Commented Jul 12, 2018 at 9:00
  • Yes that's what i want to do Commented Jul 12, 2018 at 9:00

3 Answers 3

2

If it's a Test Method you should throw an exception or use the "Assert" class as follows:

String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";

bool ignoreCase = ...;
Assert.AreEqual(title, expectedTitle, ignoreCase);
Sign up to request clarification or add additional context in comments.

Comments

2
String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";

if (title.Contains(expectedTitle))
{
    Console.WriteLine("Title is matching with expected value");
}
else
{
    throw new Exception("Title does not match");
}

or in a [TestMethod]:

String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";

bool contains = title.Contains(expectedTitle);
Assert.IsTrue(contains);

1 Comment

Perfect i've opted to go with the first suggestion
0
You can use assert on you verification point, if assert conditions is not matching assert will throw exception :

String expectedTitle = "Utilities from Go Compare";

// 1st Way
bool isTrue= webDriver.Title.Contains(expectedTitle);
Assert.IsTrue(isTrue);

OR 

// 2nd Way
Assert.Contains(expectedTitle,webDriver.Title);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.