Skip to content

Running Your First Test

Terminology

Before we start the lab, there are a number of different components that you should be aware of when building a test suite using a WebDriver. - API: Application Programming Interface. This is the set of “commands” you use to manipulate WebDriver. Selenium dotnet api doc

  • Library: A code module which contains the APIs and the code necessary to implement them. Libraries are specific to each language binding, eg .jar files for Java, .dll files for .NET, etc.

  • Driver: Responsible for controlling the actual browser. Most drivers are created by the browser vendors themselves. Drivers are generally executable modules that run on the system with the browser itself, not on the system executing the test suite. Driver requirements

  • Framework: An additional library used as a support for WebDriver suites. These frameworks may be test frameworks such as JUnit, NUnit or MSTest. They may also be frameworks supporting natural language features such as Cucumber/Specflow or Robotium.

We will be using the MSTest Unit Testing Framework. Checkout the MSTest api browser doc to learn more about the MSTest framework attributes.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
Library Purpose
Microsoft.VisualStudio.TestTools.UnitTesting MSTest framework used for driving test code. Supports helpful test classes(ex: Assert)
OpenQA.Selenium Selenium testing library is used for interacting with a web driver to extract page contents and submit data. This interaction makes it possible to automate UI testing via code.
OpenQa.Selenium.Edge Selenium Edge Browser package that allows test code to target Microsoft Edge. Similar packages are available for Chrome and Firefox. Used to create browser sessions and customize configuration

Test Case

The following test case will:

  • Create a connection from Selenium to our Edge Web Driver
  • Connect to our local app using APP_ENDPOINT environment variable
  • Verify that a banner exists with an image named renters.jpg
public void VerifyBannerImage()
{
    driver = new EdgeDriver();
    var endpoint = Environment.GetEnvironmentVariable("APP_ENDPOINT");
    driver.Navigate().GoToUrl(endpoint);

    var element = driver.FindElement(By.ClassName("hero"));
    string imageSrc = element.GetAttribute("src");
    Assert.IsTrue(imageSrc.Contains("renters.jpg"));

    Thread.Sleep(3500);
}

Useful Selenium Functions

Function Description
EdgeDriver() Creates browser object that you can use to interact with the DOM
Navigate().GoToUrl() Once this statement is executed browser navigates to the new page specified by the URL parameter.
FindElement() Returns the first matching web element if multiple web elements are discovered by the locator
Things to callout
  • We connect our test code to a live browser via:
  • Creating a driver object(new EdgeDriver)
  • Using Selenium functions to replace human behavior
  • Asserting valid content after performing user interaction
Discussion Questions
  • Discussion Questions:
  • What are some benefits of using an environment variable for APP_ENDPOINT?
    • Hint: This allows the pipeline to support remote func test runs or users to pass their own host & port.
  • What kind of configuration would we want to pass to our driver? Hints:
    • Alternative Web Driver binaries
    • Mobile Device emulation
    • Custom user preferences
    • See more here

Running Tests

Note: If you are using Visual Studio Code or just the Powershell terminal, you need to set the environment variable for ModelApp url manually $env:APP_ENDPOINT = 'http://localhost:5000'

  1. Make sure the ModelApp is running locally on port 5000. Review how to run the ModelApp locally here

  2. Navigate to the functional test folder: cd ModelApp.FunctionalTests/

  3. Build the project with the command dotnet build

  4. Run the TestPageContent test with the command dotnet test --filter TestPageContent

  5. You should see a new Edge Browser pop up and navigate to your app page. The screen will quickly change as it runs through your test instructions.