Selenium – Writing Your First Test

Selenium Locators

In the last article of this series, we learnt about what Selenium and WebDriver were, and how they work. With that basic understanding, we can now look at how we implement the very basics of Selenium into code, and how we go about writing our first test.

For the purpose of this article, we’re going to put everything in to one class. Of course we eventually want to put it in to a framework, but for the sake of this learning exercise, we can be forgiven for writing some less than perfect code.

The first thing we need to do is create a new test project for our test. We’ll also need to download the Selenium WebDriver Nuget package so that you can use the code we’re going go through in this example. To do that you must do the following:

  1. Create a new project in Visual Studio of the type, Unit Test Creating the test project

 

2. Install the Selenium WebDriver Nuget Package via ‘Tools->Manage Nuget Packages…’ then search for Selenium WebDriver and install it for your project. You’ll need to also search for NUnit Framework and install that as well.

Selenium WebDriver Nuget Package

Once these steps are done, you are ready to begin writing some code! Exciting, right?

To help us get started, this is the template you need for your test and to begin writing the code:


using NUnit.Framework;

using OpenQA.Selenium;

namespace MyFirstTestProject

{
    [TestFixture]
    public class MyFirstTest

    {

        [Test]

        public void MyFirstTestScript()

        {

        }

    }

}

The above code is just creating our MyFirstTest class, and inside creating a method which we will use for our test. The NUnit keyword attribute [Test] is what tells our test runner of choice that there is in fact a test in our class. Without this it would just be treated as any other method.

And inside the MyFirstTestScript method, the very first line of code you must write is to create an instance of WebDriver for the browser we want. For this example I’m going to use Chrome, but you can use whatever you have installed or prefer. And we’re going to tell our newly create driver instance to go to the all too familiar Google homepage. For this simple test, all we’re going to do is navigate to Google, perform a search and check that results were returned.

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://www.google.co.uk");

And just like that, you have a test that will open a new Chrome window and navigate to Google, with just two lines of code. It’s a start, but it’s not much of a test

IWebElement searchBarElement = driver.FindElement(By.CssSelector(@"#lst-ib"));

Next we need to actually do something, and for that we need to find an element. Now I’m going to skip right over how we find and use locators for elements on a page for now, because I’m going to cover that in far more detail in another article, as it’s really important we go through that thoroughly. But for now, all you need to understand is that we are trying to locate the Google search bar on the home page. To do this we need to create an instance of IWebElement and then find the element on the page using the drivers FindElement method. FindElement takes a single parameter which is a By locator. By locators can be set to use an elements Id, XPath, CssSelector, Class, Name, TagName and Link Text. Again, I will cover each of those in a lot more detail in another article. In this case though we are telling FindElement to use the CssSelector for the search bar, which isĀ #lst-ib. This will allow us to assign the element and its properties to our element IWebElement variable.

And now we have an element we can fully interact with. Of course being the search bar, we need to send in some text that we want to search for. And for that we need to find a way to send keys to the page. We can do that with the following code:

 
string searchString = "This is a test";
searchBarElement.SendKeys(searchString);
 

There are many methods for interacting with an element, all of which will show up using Intellisense when writing your code. The one we need for this test however is SendKeys. This will mimic a user sending keystrokes to the browser, and enter the string we pass as a parameter in to the search bar. But before we are searching anything, we need to confirm our search. However Google with its fancy minimal design no longer has a search button, so we need to simulate the user pressing enter.

 searchBarElement.SendKeys(Keys.Enter); 

We are using SendKeys again, and this time instead of passing a string, we’re using the Selenium’s built in Keys type. By doing this we are effectively pressing enter in the search bar.

Now, this has been an extremely short test but we’ve done it and we need to do an Assert to check our test has passed. This is why I wanted to use the NUnit Framework as it provides very good and easy to use Assert methods that we can call within our test

 Assert.IsTrue(driver.Url.Contains("https://www.google.co.uk/search?q=test&"), $"Did not correctly search for {searchString}") 

The above code is performing a true or false check on the current URL of the browser containing the string that we have passed. Of course this isn’t the only way we would want to assert our search has been successful, but it’s an easy way to show you one way to use the Assert method. If it was to fail, our string “Did not search with the correct term” would be displayed in the test failure message. This is important to note as making sure your assert failure messages are clear is vital for you and anyone else running your tests, as without it it may not be easy to see the cause of failure without having to debug and step through.

So there it is, your first Selenium WebDriver test. It may be extremely simple and it doesn’t even scratch the surface of what Selenium is capable of, but it’s all about those first steps. And you can mess around with the code you now have and add to it. Add more asserts to make your test more robust, create more elements and interact with them and experiment with the element methods.

In the next article, we will look at locators and how to correctly use the right locator for your elements.