1

I have a for loop in which I look for attributes that match what I have typed in as a parameter. It passes when it is found but when it isn't found, it keeps looping through. I need a way in which to get the test to fail if it isn't found.

foreach (IWebElement link in links)
{
    string text = link.GetAttribute("alt");
    if (text == transportMode)
    {
        Assert.AreEqual(text, transportMode);
    }
}
wkl
  • 1,896
  • 2
  • 15
  • 26
Coder
  • 117
  • 2
  • 13

3 Answers3

2

No need to assert inside the loop (because your if statement checks exactly the same). Just jump out of the test if you found it. Otherwise fail at the end of the test:

foreach (IWebElement link in links)
{
    string text = link.GetAttribute("alt");
    if (text == transportMode)
    {
        return;
    }
}
Assert.Fail("not found");

You may want to think of a more clever message than I used.

wkl
  • 1,896
  • 2
  • 15
  • 26
1

You haven't mentioned whether this is NUnit or MSTest, but in both cases

Assert.Fail("Error message");

fails the test

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
1

Pretty simple using LINQ:

Assert.IsTrue(links.Select(l => l.GetAttribute("alt")).Contains(transportMode));
chambo
  • 491
  • 3
  • 8