Reputation: 193
I am very new to nUnit testing I try to use nUnit attributes and in my code [Retry] does not work when Assert fails. I am using Microsoft Visual Studio Professional 2022 Current Version 17.6.2 NuGet packages: Selenium.WebDriver, Selenium.Support, Selenium.Chrome.WebDriver, Gherkin, NUnit, NUnit3TestAdapter
I use in Assert method wrong title (see "Attention! WrongTitle so that Assertion will fail") to make assertion fail.
the code is:
using NUnit;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools.V113.CSS;
using System;
namespace TestThePage
{
[TestFixture]
public class NavigateToTheWepPage
{
private IWebDriver driver;
public IWebDriver theDriver
{
get { return driver; }
set { driver = value; }
}
string url = "https://www.Some Web Site I Use to Experiment With nUnit";
[OneTimeSetUp]
public void InitaliseSmth()
{
var options = new ChromeOptions();
driver = new ChromeDriver();
options.AddArgument("--no-sandbox");
options.AddArgument("start-maximized");
options.AddUserProfilePreference("disable-popup-blocking", "true");
}
[Test]
[Retry(2)]
public void CallSomeTest()
{
//Step 1: Navigate through couple of pages
OpenSomePage(url);
IWebElement element = theDriver.FindElement(By.XPath("/html/body/div[8]/header/div[2]/div[1]/div[1]/nav/ul/li[5]/a/span[1]"));
element.Click();
IWebElement element1 = theDriver.FindElement(By.XPath(".//*[@id=\"orb-modules\"]/header/div[2]/div[1]/div[1]/nav/ul/li[6]/a/span[1]"));
element1.Click();
//element2.Click();
string currentPageTitle = theDriver.Title;
// Step 2 Assert you have an expected content on the page you navigated to
try
{
Assert.IsTrue(currentPageTitle.Contains("Attention! WrongTitle so that Assertion will fail"));
TestContext.WriteLine("Word \"World\" was found in the header");
}
catch (Exception ex)
{
TestContext.WriteLine("Word \"World\" was NOT found in the header");
if (ex is AssertionException)
Console.WriteLine($"Processing failed: {ex.Message}");
}
}
public void OpenSomePage(string url)
{
theDriver.Url = url;
}
}
}
I would expect that when assertion fails, so because I have [Retry(2)] it would try to run the code again. However, when I run the code it just fails for the first time and would NOT try to execute again. Is it my wrong expectation about [Retry(2)] ,or I am doing something wrong?
Upvotes: 1
Views: 448
Reputation: 193
In my code I changed [TearDown] to [OneTimeTearDown] and it is working now. It was just closing the driver before the second attempt.
using NUnit;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools.V113.CSS;
using System;
namespace TestThePage
{
[TestFixture]
public class NavigateToTheWepPage
{
private IWebDriver driver;
//public IWebDriver Driver => driver;
public IWebDriver theDriver
{
get { return driver; }
set { driver = value; }
}
readonly string url = "https://Some web site url";
int count = 0;
[OneTimeSetUp]
public void InitaliseSmth()
{
var options = new ChromeOptions();
driver = new ChromeDriver();
options.AddArgument("--no-sandbox");
options.AddArgument("start-maximized");
options.AddUserProfilePreference("disable-popup-blocking", "true");
}
[Test]
[Retry(2)]
[Category("PracticenUnit")]
public void CallSomeTest()
{
//Step 1: Navigate through couple of pages
OpenSomePage(url);
IWebElement element = theDriver.FindElement(By.XPath("/html/body/div[8]/header/div[2]/div[1]/div[1]/nav/ul/li[5]/a/span[1]"));
element.Click();
IWebElement element1 = theDriver.FindElement(By.XPath(".//*[@id=\"orb-modules\"]/header/div[2]/div[1]/div[1]/nav/ul/li[6]/a/span[1]"));
element1.Click();
string currentPageTitle = theDriver.Title;
// Step 2 Assert you have an expected content on the page you navigated to
try
{
TestContext.Progress.WriteLine("Here it is in try assertion, count: {0}", ++count);
Assert.IsTrue(currentPageTitle.Contains("Attention! WrongTitle so that Assertion will fail"));
TestContext.Progress.WriteLine("Here it is AFTER Assertion");
}
catch
{
TestContext.Progress.WriteLine("In the catch");
}
// Step 3 return to the previous page
ReturnToThePreviousPage();
}
public void OpenSomePage(string url)
{
theDriver.Url = url;
}
public void ReturnToThePreviousPage()
{
theDriver.Navigate().Back();
}
//[TearDown]
// public void closeBrowser()
//{
//theDriver.Close();
//}
[OneTimeTearDown]
public void closeBrowser()
{
theDriver.Close();
}
}
}
Now it is running twice before the program is closed, see the Output:
Upvotes: 1