Запуск диспетчера хуков отчетов по экстентам для работы с Specflow - PullRequest
0 голосов
/ 04 февраля 2019

У меня есть Selenium проект с Specflow и Extent Reports.

В моих тестах у меня есть несколько .feature файлов, поэтому я нашел этот учебник с Extent Report иSpecflow.

В этом решении он имеет Hooks класс:

[Binding]
public class Hooks
{
    //Global Variable for Extend report
    private static ExtentTest featureName;
    private static ExtentTest scenario;
    private static ExtentReports extent;
    //private static KlovReporter klov;

    private readonly IObjectContainer _objectContainer;

    private RemoteWebDriver _driver;

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

    [BeforeTestRun]
    public static void InitializeReport()
    {
        //Initialize Extent report before test starts
        var htmlReporter = new ExtentHtmlReporter(@"C:\extentreport\SeleniumWithSpecflow\SpecflowParallelTest\ExtentReport.html");
        //htmlReporter.Configuration().Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;
        //Attach report to reporter
        extent = new ExtentReports();
       // klov = new KlovReporter();

        //klov.InitMongoDbConnection("localhost", 27017);

        //klov.ProjectName = "ExecuteAutomation Test";

        // URL of the KLOV server
        //klov.KlovUrl = "http://localhost:5689";

        //klov.ReportName = "Karthik KK" + DateTime.Now.ToString();


        extent.AttachReporter(htmlReporter);
    }

    [AfterTestRun]
    public static void TearDownReport()
    {
        //Flush report once test completes
        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.InnerException);
            else if (stepType == "When")
                scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
            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 == "Then")
                scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");

        }

    }


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

    [AfterScenario]
    public void CleanUp()
    {
        _driver.Quit();
    }

    internal void SelectBrowser(BrowserType browserType)
    {
        switch (browserType)
        {
            case BrowserType.Chrome:
                ChromeOptions option = new ChromeOptions();
                //option.AddArgument("--headless");
                _driver = new ChromeDriver(option);
                _objectContainer.RegisterInstanceAs<IWebDriver>(_driver);
                break;
            case BrowserType.Firefox:
                var driverDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(driverDir, "geckodriver.exe");
                service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
                service.HideCommandPromptWindow = true;
                service.SuppressInitialDiagnosticInformation = true;
                _driver = new FirefoxDriver(service);
                _objectContainer.RegisterInstanceAs<RemoteWebDriver>(_driver);
                break;
            case BrowserType.IE:
                break;
            default:
                break;
        }
    }

}

enum BrowserType
{
    Chrome,
    Firefox,
    IE
}

Так что после добавления этого класса в мое решение я задаюсь вопросом, как с ним работать, я имею в виду, что этоIObjectContainer objectContainer внутри конструктора?Как я могу позвонить, чтобы написать свой отчет и прослушать все мои шаги?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...