0

对于范围报告的版本 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);
            }
        }
4

0 回答 0