屏幕截图无法使用范围报告版本4捕获-Selenium C#

时间:2019-04-30 05:43:58

标签: c# selenium-webdriver selenium-extent-report protractor-net

使用版本4进行范围报告时,使用Selenium Webdriver C#,我无法将屏幕快照附加到报告中。

尝试了,但存在相同的问题。

catch (Exception ex)
            {
_test.Log(Status.Fail, "Exception" + AssertionStatus.Error);                              
                _test.Info(ex.Message);
    _test.Fail("details", MediaEntityBuilder.CreateScreenCaptureFromPath("screenshot.png").Build());

    _test.Fail("details").AddScreenCaptureFromPath("screenshot.png");

}

捕获屏幕快照的方法,放置在OneTimeTearDown

公共字符串捕获(IWebDriver浏览器,字符串screenShotName)         {

        try
        {
            Thread.Sleep(4000);
            ITakesScreenshot ts = (ITakesScreenshot)browser;
            Screenshot screenshot = ts.GetScreenshot();
            string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            var dir = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
            DirectoryInfo di = Directory.CreateDirectory(dir + "\\Defect_Screenshots\\");
            string finalpth = pth.Substring(0, pth.LastIndexOf("bin")) + "\\Defect_Screenshots\\" + screenShotName + ".png";
            localpath = new Uri(finalpth).LocalPath;
            screenshot.SaveAsFile(localpath);
        }
        catch (Exception e)
        {
            throw (e);
        }
        return localpath;
    }

在TearDown中跟踪代码以捕获失败案例的屏幕截图。

public void AfterTest()
        {
            try
            {
                var status = TestContext.CurrentContext.Result.Outcome.Status;
                var stacktrace = "" + TestContext.CurrentContext.Result.StackTrace + "";
                var errorMessage = TestContext.CurrentContext.Result.Message;
                Status logstatus;
                switch (status)
                {
                    case TestStatus.Failed:
                        logstatus = Status.Fail;
                        string screenShotPath = Capture(ngDriver, TestContext.CurrentContext.Test.Name);
                        _test.Log(logstatus, "Test ended with " + logstatus + " – " + errorMessage);
                        _test.Log(logstatus, "Snapshot below: " + _test.AddScreenCaptureFromPath(screenShotPath));
                        break;
                    case TestStatus.Skipped:
                        logstatus = Status.Skip;
                        _test.Log(logstatus, "Test ended with " + logstatus);
                        break;
                    default:
                        logstatus = Status.Pass;
                        _test.Log(logstatus, "Test ended with " + logstatus);
                        break;
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

0 个答案:

没有答案