ExtentReports:使用Hooks无法运行多个测试方案

时间:2019-06-23 22:28:28

标签: c# selenium nunit hook extentreports

我正在使用ExtentReports使用Hooks生成NUnit测试报告。当我运行单个测试方案时,它正在通过而没有任何错误。但是,当我尝试同时运行多个方案时,只有第一个测试方案通过了,而其余的都以错误“ OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法定位元素”异常失败。我认为ExtenetReports的Hooks配置与我无法理解正确的配置有关。有人可以帮助我了解成功运行多个测试方案的正确设置吗?

我试图通过运行单个和多个场景来调试代码,发现仅运行单个测试场景就可以了。但是,当我一起运行多个场景时,第一个测试场景成功运行,但是在第二个测试场景中,钩子[AfterStep]下的代码未运行,并且由于此原因,给定的时候,然后代码尝试查找不存在的元素在页面上可用,因此抛出此异常。

OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法找到元素

Hook.cs文件:

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;
using AventStack.ExtentReports.Reporter;
using BoDi;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using TechTalk.SpecFlow;
using UAT.MainSite.Automation.Helpers;
using UAT.MainSite.Automation.MainSite.Pages;
using UAT.MainSite.Automation.WebDriver;
using TestContext = NUnit.Framework.TestContext;

namespace UAT.MainSite.Automation.MainSite
{
[Binding]
public sealed class Hooks
{
private readonly IObjectContainer _objectContainer;

// For additional details on SpecFlow hooks see http://go.specflow.org/doc-hooks
private static MainSiteNavigation _mainSiteNavigation;
private static WebDriverManager _webDriverManager;
private string _screenShotFileName;

//Global Variable for Extend report
private static ExtentTest featureName;
private static ExtentTest scenario;
private static ExtentReports extent;

public Hooks(IObjectContainer objectContainer)
{
_objectContainer = objectContainer;
}

public static WebDriverManager WebDriverManager
{
get
{
if (_webDriverManager?.WebDriver == null)
{
_webDriverManager = WebDriverFactory.Get();
}

return _webDriverManager;
}
}

[BeforeTestRun]

public static void InitializeReport()
{
//Initialize Extent report before test starts
var htmlReporter = new ExtentHtmlReporter(@"C:\Users\Desktop\index.html");
htmlReporter.Config.Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;

//Attach report to reporter
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
htmlReporter.LoadConfig(Directory.GetParent(TestContext.CurrentContext.TestDirectory).Parent.FullName+"\\extent-config.xml");
htmlReporter.Config.DocumentTitle = "Automation Test report";
htmlReporter.Config.ReportName = "Automation test report";
extent.AddSystemInfo("Operating System:", "Windows 10");
extent.AddSystemInfo("Host name:", "Selenium");
extent.AddSystemInfo("Browser:", "Google chrome");
}

public static void BeforeTestRun()
{
_mainSiteNavigation = new MainSiteNavigation(WebDriverManager);

if (Configuration.GTMEnabled)
{
WebDriverManager.WebDriver.Navigate().GoToUrl(Configuration.GTMEnviroment);
}

if (_mainSiteNavigation.GoToHomePage().HomePage.CulturePage.IsDisplayed)
{
_mainSiteNavigation.SetCulture();
}

}

[AfterTestRun]
public static void AfterTestRun()
{
WebDriverManager.Teardown();
extent.Flush();

}


[BeforeFeature]
public static void BeforeFeature()
{
//Create dynamic feature name
featureName = extent.CreateTest<Feature>(FeatureContext.Current.FeatureInfo.Title);
}

[AfterStep]
public void InsertReportingSteps()
{

var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();

PropertyInfo pInfo = typeof(ScenarioContext).GetProperty("TestStatus", BindingFlags.Instance | BindingFlags.NonPublic);
MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
object TestResult = getter.Invoke(ScenarioContext.Current, null);

if (ScenarioContext.Current.TestError == null)
{
if (stepType == "Given")
scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
else if (stepType == "When")
scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
else if (stepType == "Then")
scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
else if (stepType == "And")
scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
}
else if (ScenarioContext.Current.TestError != null)
{
if (stepType == "Given")
scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
else if (stepType == "When")
scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
else if (stepType == "And")
scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
else if (stepType == "Then")
scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);

}

// Pending Status
if (TestResult.ToString() == "StepDefinitionPending")
{
if (stepType == "Given")
scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
else if (stepType == "When")
scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
else if (stepType == "And")
scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
else if (stepType == "Then")
scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");

}

}

[BeforeScenario]

public void Initialize()
{
//Create dynamic scenario name
scenario = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);
}

public void BeforeScenario()
{
_screenShotFileName = string.Concat(TestContext.CurrentContext.Test.Name, ".png").Replace(@"\", string.Empty).Replace(@"""", "");

_mainSiteNavigation.ClearShoppingBag();

ProductDetailPage.IsHemmingProduct = false;

if (_mainSiteNavigation.GoToHomePage().HomePage.CulturePage.IsDisplayed)
{
_mainSiteNavigation.SetCulture();
}

_objectContainer.RegisterInstanceAs(WebDriverManager);
_objectContainer.RegisterInstanceAs(_mainSiteNavigation);
}

[AfterScenario]
public void AfterScenario()
{
if (TestContext.CurrentContext.Result.Outcome.Status == ResultState.Failure.Status)
{
// Take screenshot
var path = string.Concat(AppDomain.CurrentDomain.BaseDirectory, @"\logs\screenshots\");

var directoryInfo = new DirectoryInfo(path);
if (!directoryInfo.Exists)
{
    directoryInfo.Create();
}

var fullPath = string.Concat(path, _screenShotFileName);
var screenshot = ((ITakesScreenshot)_webDriverManager.WebDriver).GetScreenshot();

screenshot.SaveAsFile(fullPath, ScreenshotImageFormat.Png);
}

_webDriverManager.WebDriver.Manage().Cookies.DeleteAllCookies();

}

private void DeleteScreenShot()
{
if (_screenShotFileName != string.Empty)
{
var filePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory + @"\logs\screenshots\", _screenShotFileName);

if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
}
}

所有测试方案都应通过且没有任何错误。

0 个答案:

没有答案