Skip to content

Your Turn!

Write your own test!

Now that we've taken a look at some existing tests and seen how they are run, let's try to expand our test coverage. So far we've only verified that web content is where we expect it to be but that's not the only thing we're concerned about. We also want to make sure that we can submit data via our form. Luckily, Selenium makes it easy to have our test interact with our app and act as a user submitting information.

  1. Begin by adding a new class in ModelApp.FunctionalTests called TestFormFill.cs
  2. Make sure to include your system & testing libraries and MSTest attributes.
  3. Checkout the Selenium documentation for pointers on how to interact with our WebApp
  4. Run your test to see if you can successfully test your form submission!

To help get you started, here's some skeleton code that includes a function signature and the assertion you should be aiming to satisfy:

[TestMethod]
public void ShouldBeAbleToFillForm()
{
    // Arrange -> setup testing objects and prerequisites

    // Act -> Perform actual work for the test 

    // Assert -> verify the result
    Assert.IsTrue(); // Your URL should contain the "Create" path when data is submitted

    Thread.Sleep(3500);
}
Hints

Browser input is key to completing this test case. Here's some shortcuts to some helpful documentation:

Action Link
Finding page objects https://www.selenium.dev/documentation/en/webdriver/locating_elements/
Keyboard Input https://www.selenium.dev/documentation/en/webdriver/keyboard/
Interact with Element https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/
Cheat Sheet

Valid Test Implementation:

using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;

namespace ModelApp.TestFormFill
{
    [TestClass]
    public class FillForm
    {

        IWebDriver driver;
        string endpoint;

        [TestMethod]
        public void ShouldBeAbleToFillForm()
        {
            driver.Navigate().GoToUrl(endpoint);

            var dropdown = driver.FindElement(By.Id("Product"));
            dropdown.FindElement(By.XPath("//option[. = 'Auto']")).Click();

            driver.FindElement(By.Id("Name")).SendKeys("John Doe");
            driver.FindElement(By.Id("Email")).SendKeys("test1@gmail.com");
            driver.FindElement(By.Id("Message")).SendKeys("test1 message");
            driver.FindElement(By.CssSelector(".btn")).Click();
            Assert.IsTrue(driver.Url.Contains("Create"));

            Thread.Sleep(3500);
        }

        [TestInitialize()]
        public void SetupTest()
        {
            endpoint = Environment.GetEnvironmentVariable("APP_ENDPOINT");

            string browser = "Firefox";    //Participants can choose whatever driver works for them
            switch (browser)
            {
                case "Edge":
                    driver = new EdgeDriver();
                    break;
                case "Chrome":
                    driver = new ChromeDriver();
                    break;
                case "Firefox":
                    driver = new FirefoxDriver();
                    break;
                case "IE":
                    driver = new InternetExplorerDriver();
                    break;
                default:
                    driver = new ChromeDriver();
                    break;
            }

        }

        [TestCleanup]
        public void CleanUp()
        {
            driver.Quit();
        }
    }
}