Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:27:06+00:00 2026-06-12T19:27:06+00:00

I have a C# unit test using Selenium WebDriver to test to see if

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:27:07+00:00Added an answer on June 12, 2026 at 7:27 pm

    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 registerLink will be null. What’s the point of asserting on a null object’s property?

    Your test works fine, just delete the Assert line.

    However, if you also want to test the link’s text try the following code:

    [TestMethod()]
    public void RegisterLinkExistTest()
    {
        try
        {
            IWebElement registerLink = genericBrowserDriver.FindElement(By.PartialLinkText ("Register1"));
            Assert.AreEqual("Register here", registerLink.Text, "Register's link text mismatch");
        }
        catch(NoSuchElementException)
        {
            Assert.Fail("The register link was not found");
        }
    }
    

    EDIT

    You can seperate your test, the first test will check if the link exists and the second will assert it’s properties.

    [TestMethod()]
    public void RegisterLinkExistTest()
    {
        IWebElement registerLink = genericBrowserDriver.FindElement(By.PartialLinkText ("Register1"));
    }
    
    [TestMethod()]
    public void RegisterLinkTextTest()
    {
        IWebElement registerLink = genericBrowserDriver.FindElement(By.PartialLinkText ("Register1"));
        Assert.AreEqual("Register here", registerLink.Text, "Register's link text mismatch");
    }
    

    Then use an OrderedTest and add them in that order so the RegisterLinkExistTest will be executed first. If it fails then the second test will not run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using selenium python webdriver for my python test cases. I have a
I have some code in a test using Moq: public class Invoice { ...
I have following code: [TestMethod] public void StartWorkInFirstThread() { if (SynchronizationContext.Current == null) SynchronizationContext.SetSynchronizationContext(
I have a Unit test project for my Application using DUnit framework. This project
I have a VS2010 unit test project set to using SpecFlow 1.8.1 and mstest.
I'm using Google Test Framework to set some unit tests. I have got three
I have an Rails 3 App, here i have a unit test in test/unit/test.rb
I have a Vector class, and I was testing the following unit test (using
I'm using NUnit for my unit tests and I have my unit test class
I have various methods that I would like to unit test using Visual Studio's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.