109 lines
3.0 KiB
Markdown
109 lines
3.0 KiB
Markdown
# Automated UI End to End(E2E) tests using ReqnRoll BDD, MSTest, and Chrome
|
|
|
|
## Prerequisites
|
|
|
|
- Visual Studio with ReqnRoll extension installed
|
|
- Chrome browser installed
|
|
- .NET Core SDK (please take a look of required version)
|
|
|
|
## Project Setup
|
|
|
|
1. Create a new SpecFlow project in Visual Studio.
|
|
|
|
2. Install the required NuGet packages:
|
|
- Reqnroll
|
|
- Reqnroll.MSTest
|
|
- Selenium.WebDriver
|
|
- Selenium.WebDriver.ChromeDriver
|
|
|
|
|
|
## Writing the Test. Example
|
|
|
|
1. Create a new feature file named "Login.feature" in the "Features" folder of your SpecFlow project.
|
|
|
|
2. Add the following scenario to the "Login.feature" file:
|
|
|
|
```gherkin
|
|
Feature: User Authentication
|
|
In order to access my account
|
|
As a user
|
|
I want to be able to authenticate in the system
|
|
|
|
Scenario: Successful User Login
|
|
Given I am on the login page
|
|
When I enter valid credentials
|
|
And I click the login button
|
|
Then I should be redirected to the home page
|
|
```
|
|
|
|
3. Right-click on the "Login.feature" file and choose "Generate Step Definitions."
|
|
|
|
4. Select the class where you want to generate the step definitions or create a new class.
|
|
|
|
### Writing the Test Code
|
|
|
|
1. Open the step definition file corresponding to the "Login.feature" scenario.
|
|
|
|
2. Add the following code to the step definition file:
|
|
|
|
```csharp
|
|
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Chrome;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using TechTalk.SpecFlow;
|
|
|
|
namespace YourNamespace
|
|
{
|
|
[Binding]
|
|
public class LoginSteps
|
|
{
|
|
private IWebDriver driver;
|
|
|
|
[Given(@"I am on the login page")]
|
|
public void GivenIAmOnTheLoginPage()
|
|
{
|
|
driver = new ChromeDriver();
|
|
driver.Navigate().GoToUrl("http://localhost:3000/login");
|
|
}
|
|
|
|
[When(@"I enter valid credentials")]
|
|
public void WhenIEnterValidCredentials()
|
|
{
|
|
// Enter valid credentials in the login fields here
|
|
var usernameField = driver.FindElement(By.Id("username"));
|
|
usernameField.SendKeys("yourusername");
|
|
|
|
var passwordField = driver.FindElement(By.Id("password"));
|
|
passwordField.SendKeys("yourpassword");
|
|
}
|
|
|
|
[When(@"I click the login button")]
|
|
public void WhenIClickTheLoginButton()
|
|
{
|
|
var loginButton = driver.FindElement(By.Id("login-button"));
|
|
loginButton.Click();
|
|
}
|
|
|
|
[Then(@"I should be redirected to the home page")]
|
|
public void ThenIShouldBeRedirectedToTheHomePage()
|
|
{
|
|
Assert.AreEqual("http://localhost:3000/", driver.Url);
|
|
}
|
|
|
|
[AfterScenario]
|
|
public void AfterScenario()
|
|
{
|
|
driver.Quit();
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
### Running the Test
|
|
|
|
1. Build your ReqnRoll project to compile the code.
|
|
|
|
2. In Visual Studio, open the "Test Explorer" window.
|
|
|
|
3. Click the "Run All Tests" button to execute the authentication test.
|