Skip to content

Latest commit

 

History

History
260 lines (183 loc) · 9.25 KB

File metadata and controls

260 lines (183 loc) · 9.25 KB

Selenium, NUnit, and Robot Framework tutorial

An example C# WebApp tested using Selenium browser automation with Nunit testing framework for unit tests and Robot Framework automation framework for acceptance tests.

  • .Net Core > 3 - written with .Net Core 3.1.102.
  • Python > 3 - written with Python 3.8.2 (used for the acceptance tests).
Before starting, please clone this repository and step into the solution folder.
It will be easier to follow this tutorial, as the next steps will assume you have done so:
git clone https://github.com/TomerFi/selenium-nunit-robotframework-tutorial.git
cd selenium-nunit-robotframework-tutorial
DemoWebApp
A simple C# WebApp created from the basic template provided with dotnet.
On top of the base application I've added:
  • a button tag with the id clickmeButton.

  • a h2 tag with the id displayHeader and the initial content text of Not clicked.

  • a script function called clickButton invoked by a click event from the clickmeButton
    and changes the displayHeader's content text to Button clicked.

I've added these elements in src/DemoWebApp/Pages/Index.cshtml.

To run the project and serve the app at http://localhost:5000:

dotnet run -p src/DemoWebApp
Please note: I want to clarify that the web application is not written by me,
It's the original template created with the command dotnet new webapp.
All I did was adding the button and h2 tags, and the simple script.
DemoWebApp.Tests

For unit testing I've used:

  • Nunit as the testing framework the project.
  • Selenium as the toolset providing browser capbalities and automation.

DemoWebAppTest.cs is test class, Nunit will pick it up based on its name.

I've used the OneTimeSetup attribute to spin-up the server prior of executing the test cases:

[OneTimeSetUp]
public void SetUpWebApp()
{
    app = DemoWebApp.Program.CreateHostBuilder(new string[] { }).Build();
    app.RunAsync();
}

And the OneTimeTearDown attribute to shutdown the server afterwards.

[OneTimeTearDown]
public void TearDownWebApp()
{
    app.StopAsync();
    app.WaitForShutdown();
}

The test itself is pretty straightforward:

  • It first navigates to the server at http://localhost:5000.
  • It will then find the button element by its id and click it.
  • Finaly, it will make sure the h2 element's content text is Button clicked.
The assert statement evaluates the clicked boolean value,
which will be false if expected test conditions are not met within 10 seconds.
public void TestButtonClick(Type drvType)
{
    bool clicked;
    using (var driver = (IWebDriver)Activator.CreateInstance(drvType))
    {
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        driver.Navigate().GoToUrl("http://localhost:5000");
        driver.FindElement(By.Id("clickmeButton")).Click();

        clicked = wait.Until(ExpectedConditions.TextToBePresentInElement(
            driver.FindElement(By.Id("displayHeader")), "Button clicked"));
    }
    Assert.True(clicked, "button not clicked.");
}
The test-cases will invoke the TestButtonClick test 3 times, one for each TestCase.
The result will be 3 tests performed, one with the chrome driver, one with the firefox driver,
and one with the ie driver.
[TestCase(typeof(ChromeDriver))]
[TestCase(typeof(FirefoxDriver))]
[TestCase(typeof(InternetExplorerDriver))]
public void TestButtonClick(Type drvType)
{
    ...
}

To check it out, just:

dotnet test
Please note: Based on your personal environment,
Internet Explorer may require specific configruation for the test to pass.
If so, please follow this.
acceptance

For acceptance tests I've used:

For the next steps, step into the acceptance folder.
The acceptance tests doesn't have, nor should it have, any direct connection to the solution's base code.
Robot Framework is a python tool, it requires a python binary and some requirements.
Assuming you have Python installed, and you're in the acceptance folder,
Just do:
pip install --upgrade -r requirements.txt
As this is the acceptance tests part, the tests needs a web server serving the web app.
You can follow the Web Application section to run the web app locally, or run it as you see fit.
just don't forget to set the URL variable in resources.robot to the correct address:
${URL}              http://localhost:5000
You can download the drivers stored in acceptance/drivers with the following links.
Just mind the versions and make sure they're in conjunction with the versions used in DemoWebApp.Tests.csproj.
webapp_tests.robot is the test suite. It declares 3 Test Cases, one for each driver.
Each test-case uses Test Template with its own Browser and Executable arguments.
*** Settings ***
...
Test Template    Press Button

*** Test Cases ***             Browser    Executable
Test With Chrome               chrome     drivers/chromedriver
Test With Internet Explorer    ie         drivers/iedriver
Test With Firefox              firefox    drivers/geckodriver
The Test Template invokes the keyword named Press Button,
For each execution, what Press Button does is pretty self-explanatory by its BDD nature:
*** Keywords ***
Press Button
    [Arguments]    ${browser}    ${executable}
    Open Browser With Url    ${browser}    ${executable}
    Click Test Button
    Validate New Text
    [Teardown]    Close Browser
The result of runing this test suite will be 3 tests, one for each driver,
each pressing the button and validating the side effects.
The Press Button uses 4 other keywords to perform its action.
As you can see in the Settings section, I've declared resources.robot as a resource.
It provides us with the following custom keywords:
  • Open Browser With Url
  • Click Test Button
  • Validate New Text
The 4th keyword, Close Browser, is not a custom one, it comes from SeleniumLibrary,
imported within resources.robot:
*** Settings ***
...
Library          SeleniumLibrary

The same library is also used in by the custom keyowrds in resources.robot.

To execute the acceptance tests, simplly run:

robot -d rfoutput webapp_tests.robot
This will run the tests and save a pretty and useful html report summary and xml logs in a folder
called rfoutput (gitignored).
You can see an example of the summary report here.