Orgest Likaj
Orgest Likaj

Reputation: 11

How to assert in selenium webdriver in C#?

I am working with Selenium WebDriver in C# and I have to create a service for an applicant. I have done this already but after I confirm the service goes to a List ( Services that need to be confirmed from another user ) which is increased by 1 in read mode. Is there any way how to assert these values that are increased by 1 every time a new service is added?

Upvotes: 0

Views: 1156

Answers (2)

ggariepy
ggariepy

Reputation: 1075

Selenium's built-in assert functionality only exists in SeleniumIDE, the point-and-click browser add-on available for Chrome and Firefox.

If you are going to write your tests in C#, as Christine said, you need to use a unit testing framework. For example, I'm using Xunit, and a simple test looks like this:

using Xunit;                   // Testing framework.  NuGet package
using OpenQA.Selenium.Firefox; // Driver for Firefox
using Xunit.Priority;          // NuGet add-on to Xunit that allows you to order the tests
using OpenQA.Selenium;         // NuGet package 
using System.Diagnostics;      // Can Debug.Print when running tests in debug mode

namespace Test_MyWebPage
{
    [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)] // Set up ordering
    public class Test_BasicLogin : IDisposable
    {
       public static IWebDriver driver = new FirefoxDriver(@"path\to\geckodriver");

       // Here be the tests...
       [Fact, Priority(0)]
       public void Test_LaunchWebsite()
       {
           // Arrange
           var url = "https://yourserver.yourdomain/yourvirtualdir";

           // Act

           // Sets browser to maximized, allows 1 minute for the page to
           // intially load, and an implicit time out of 1 minute for elements
           // on the page to render.
           driver.Manage().Window.Maximize();
           driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 1, 0);
           driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 1, 0);

           driver.url = url;  // Launches the browser and opens the page

           /* Assuming your page has a login prompt 
           /* we'll try to locate this element 
           /* and perform an assertion to test that the page comes up
           /* and displays a login prompt */

           var UserNamePrompt = driver.FindElement(By.Id("userLogin_txtUserName"));   

           // Assert
           Assert.NotNull(UserNamePrompt); // Bombs if the prompt wasn't found.
           Debug.Print("Found User Name Prompt successfully.");
        }

        public void Dispose()
        {
           // Properly close the browser when the tests are done
            try
            {
                driver.Quit();
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error disposing driver: {ex.Message}");
            }
        }
    }
}

As you can see, there is considerably more work in setting up tests for the Selenium WebDriver than in setting up simple smoke tests with the SeleniumIDE. I haven't addressed how you should store your configs properly (hardcoding as in the example is bad) and you will have to tailor your driver.find() statements to fit your precise situation. I am using the Xunit.Priority package so I can make sure the tests don't all run in parallel; I need to test one thing at a time in a progression. Your needs might be met by putting all of the steps into a single Test_* method. Each method appears as a separate test in the Visual Studio Test Explorer window. Right-clicking a test in the Test Explorer and selecting 'Debug selected tests' will allow you to set breakpoints and also enable your Debug.Print (or Debug.Write/Writeline) methods to display in the Tests section of the VS output window.

Another gotcha is in setting up the IWebDriver: don't put the complete path including the executable in the path, just the path to the folder that contains it.

Good luck and happy testing!

Upvotes: 1

CEH
CEH

Reputation: 5909

You need to use a test framework to do this -- selenium itself cannot assert for you.

If you are using C#, I recommend installing NUnit. You can find this under NuGet Package manager, and you'll also want to install NUnitTestAdapter if you are using Visual Studio.

Once you have installed a test framework on your project, you can use [Test] flags to designate entry point methods for test cases, and use Assert statements which are part of the NUnit namespace.

You can find documentation here: https://github.com/nunit/docs/wiki/NUnit-Documentation

Upvotes: 2

Related Questions