Selenium with Nunit+ExtentReports
In this article we will create Selenium script and execute as Nunit. We will configure the Nunit+ExtentReports in Visual Studio and then execute the script and generate the html report using ExtentReports.
Step-1: Install Visual Studio Extensions for Nunit
- Install “Nunit 3 Test Adapter” plugin.
2. Restart the visual studio to complete the installation
Step-2: Create Class Library project
- Launch Visual Studio
- Click on “Create a new project” option.
3. Select the project type “Class Library (.Net Framework)”
4. Click on Next button. Enter the Project Name and other details –
5. Click on Create button. Project gets created and shows below screen –
6. Delete the default create class “Class1”.
Step-3: Install NuGet packages for Selenium, NUnit and ExtentReports
- Right click on project under Solution Explorer and select “Manage NuGet Packages..”
2. Search and install following packages in the same order as mentioned below —
Selenium.WebDriver.3.141.0
Selenium.Support.3.141.0
NUnit.3.12.0
ExtentReports.3.1.3
3. Now the Solution looks like this –
4. Now the “Packages” folder looks like this –
Step-4: Edit & update the project for Report, Test Cases and Page Methods
- Right click on the project and select Add->New Folder. Create below folders -
Config
TestCases
PageMethods
2. Right click on the folder name “Config” and select Add->Class…Create the class name as “ReportsGenerationClass.cs”
3. Update the class “ReportsGenerationClass.cs” as below –
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.IO;
namespace SeleniumNUnitExtentReport.Config
{
[SetUpFixture]
public abstract class ReportsGenerationClass
{
protected ExtentReports _extent;
protected ExtentTest _test;
public IWebDriver _driver;
[OneTimeSetUp]
protected void Setup()
{
var path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
var actualPath = path.Substring(0, path.LastIndexOf(“bin”));
var projectPath = new Uri(actualPath).LocalPath;
Directory.CreateDirectory(projectPath.ToString() + “Reports”);
var reportPath = projectPath + “Reports\\ExtentReport.html”;
var htmlReporter = new ExtentHtmlReporter(reportPath);
_extent = new ExtentReports();
_extent.AttachReporter(htmlReporter);
_extent.AddSystemInfo(“Host Name”, “LocalHost”);
_extent.AddSystemInfo(“Environment”, “QA”);
_extent.AddSystemInfo(“UserName”, “TestUser”);
}
[OneTimeTearDown]
protected void TearDown()
{
_extent.Flush();
}
[SetUp]
public void BeforeTest()
{
ChromeDriverService service = ChromeDriverService.CreateDefaultService(“webdriver.chrome.driver”, @”D:\\Automation\\WebDrivers\\chromedriver.exe”);
_driver = new ChromeDriver(service);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
_driver.Manage().Window.Maximize();
_test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
}
[TearDown]
public void AfterTest()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)? “”
: string.Format(“{0}”, TestContext.CurrentContext.Result.StackTrace);
Status logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = Status.Fail;
DateTime time = DateTime.Now;
String fileName = “Screenshot_” + time.ToString(“h_mm_ss”) + “.png”;
String screenShotPath = Capture(_driver, fileName);
_test.Log(Status.Fail, “Fail”);
_test.Log(Status.Fail, “Snapshot below: “ + _test.AddScreenCaptureFromPath(“Screenshots\\” + fileName));
break;
case TestStatus.Inconclusive:
logstatus = Status.Warning;
break;
case TestStatus.Skipped:
logstatus = Status.Skip;
break;
default:
logstatus = Status.Pass;
break;
}
_test.Log(logstatus, “Test ended with “ + logstatus + stacktrace);
_extent.Flush();
_driver.Quit();
}
public IWebDriver GetDriver()
{
return _driver;
}
public static string Capture(IWebDriver driver, String screenShotName)
{
ITakesScreenshot ts = (ITakesScreenshot)driver;
Screenshot screenshot = ts.GetScreenshot();
var pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
var actualPath = pth.Substring(0, pth.LastIndexOf(“bin”));
var reportPath = new Uri(actualPath).LocalPath;
Directory.CreateDirectory(reportPath + “Reports\\” + “Screenshots”);
var finalpth = pth.Substring(0, pth.LastIndexOf(“bin”)) + “Reports\\Screenshots\\” + screenShotName;
var localpath = new Uri(finalpth).LocalPath;
screenshot.SaveAsFile(localpath, ScreenshotImageFormat.Png);
return reportPath;
}
}
}
4. Right click on the folder name “PageMethods” and select Add->Class…Create the class name as “LoginPage.cs”
5. Update the class “LoginPage.cs” as below —
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeleniumNUnitExtentReport.PageMethods
{
class LoginPage
{
private IWebDriver driver;
String toggle_menu = “//a[@id=’menu-toggle’]/i”;
String loginMenu = “//a[contains(text(),’Login’)]”;
String userID = “//input[@id=’txt-username’]”;
String password = “//input[@id=’txt-password’]”;
String loginBtn = “//button[@id=’btn-login’]”;
String menuDashboard = “//section[@id=’appointment’]/div/div/div/h2”;
public LoginPage(IWebDriver driver)
{
this.driver = driver;
}
public void goToPage()
{
driver.Navigate().GoToUrl(“http://demoaut.katalon.com/");
}
public void goToToggleMenu()
{
driver.FindElement(By.XPath(toggle_menu)).Click();
}
public void goToLoginMenu()
{
driver.FindElement(By.XPath(loginMenu)).Click();
}
public void enterUserName(string text)
{
driver.FindElement(By.XPath(userID)).SendKeys(text);
}
public void enterPassword(string text)
{
driver.FindElement(By.XPath(password)).SendKeys(text);
}
public void clickLoginBtn()
{
driver.FindElement(By.XPath(loginBtn)).Click();
}
public Boolean verifyDashboard()
{
Boolean res = driver.FindElement(By.XPath(menuDashboard)).Displayed;
return res;
}
public void closeBrowser()
{
driver.Quit();
}
}
}
6. Now it’s time to generate Test Cases. Right click on the folder name “TestCases” and select Add->Class…Create the class name as “LoginTest.cs”
7. Update the class “LoginTest.cs” as below —
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SeleniumNUnitExtentReport.Config;
using SeleniumNUnitExtentReport.PageMethods;
namespace SeleniumNUnitExtentReport.TestCases
{
[TestFixture]
public class LoginTest : ReportsGenerationClass
{
LoginPage loginPage;
[Test]
[Category(“Login”)]
public void test_validLogin()
{
loginPage = new LoginPage(GetDriver());
loginPage.goToPage();
loginPage.goToToggleMenu();
loginPage.goToLoginMenu();
loginPage.enterUserName(“John Doe”);
loginPage.enterPassword(“ThisIsNotAPassword”);
loginPage.clickLoginBtn();
Assert.IsTrue(loginPage.verifyDashboard());
loginPage.closeBrowser();
}
[Test]
[Category(“Login”)]
public void test_invalidLogin()
{
loginPage = new LoginPage(GetDriver());
loginPage.goToPage();
loginPage.goToToggleMenu();
loginPage.goToLoginMenu();
loginPage.enterUserName(“John Doe”);
loginPage.enterPassword(“ThisIsNotA”);
loginPage.clickLoginBtn();
Assert.IsTrue(loginPage.verifyDashboard());
loginPage.closeBrowser();
}
}
}
8. Now Project Explorer looks like this –
9. Now Script creation completed
Step-5: Build and execute the project as NUnit
- Click on the Build->Build Solution
2. Build will happen successfully and shows below message –
3. Now open the Test Explorer. From Menu click on Test->Windows->Test Explorer
4. Test Explorer shows below screen –
5. Right click on the Login test and select “Run Selected Tests”
6. Script will execute successfully.
Step-6: Verify Extent Report
- Report will generate under “Reports” in the project directory
- Open the html report (ExtentReport.html) and its look like this –
You can find the video of this demo at location SeleniumNunitExtent
You can find the complete code at location — SeleniumNunitExtentReport