I have a C# unit test using Selenium WebDriver to test to see if a link exists. Here’s the code:
[TestMethod()]
public void RegisterLinkExistTest()
{
IWebElement registerLink = genericBrowserDriver.FindElement(By.PartialLinkText ("Register1"));
Assert.AreEqual("Register here", registerLink.Text, "Failed");
}
I wanted to see what happens if I set the PartialLinkText as “Register1” instead of “Register”. MSTest failed this test with a exception thrown from Selenium. I wanted the Assert.AreEqual to execute but MSTest throws a exception on the previous line. I know I can use ExpectedException attribute to specify “OpenQA.Selenium.NoSuchElementException” but I don’t want to do that way because I’m not expecting that exception. How do I go about handling this?
As @AD.Net already said, your test is working as expected.
You could catch the exception in case the link was not found but I don’t see the point to do that. If the link is not found then the
registerLinkwill benull. What’s the point of asserting on a null object’s property?Your test works fine, just delete the
Assertline.However, if you also want to test the link’s text try the following code:
EDIT
You can seperate your test, the first test will check if the link exists and the second will assert it’s properties.
Then use an OrderedTest and add them in that order so the
RegisterLinkExistTestwill be executed first. If it fails then the second test will not run.