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).
git clone https://github.com/TomerFi/selenium-nunit-robotframework-tutorial.git
cd selenium-nunit-robotframework-tutorial
DemoWebApp |
C# WebApp
created from the basic template provided with dotnet
.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 clickmeButtonand 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 commanddotnet new webapp
.All I did was adding thebutton
andh2
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 isButton clicked
.
clicked
boolean value,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.");
}
TestButtonClick
test 3 times, one for each TestCase
.chrome
driver, one with the firefox
driver,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:
- Robot Framework as the automation framework for executing the tests.
- SeleniumLibrary as the library providing browser capbalities and automation.
acceptance
folder.Robot Framework
is a python tool, it requires a python binary and some requirements.acceptance
folder,pip install --upgrade -r requirements.txt
URL
variable in resources.robot to the correct address:${URL} http://localhost:5000
acceptance/drivers
with the following links.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
Test Template
invokes the keyword named Press Button
,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
Press Button
uses 4 other keywords to perform its action.Settings
section, I've declared resources.robot as a resource.Open Browser With Url
Click Test Button
Validate New Text
Close Browser
, is not a custom one, it comes from SeleniumLibrary,*** 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
rfoutput
(gitignored).