[英]Take screenshot
我正在嘗試對測試失敗進行截圖。
[TearDown]
public void TearDown()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stackTrace = "<pre>" + TestContext.CurrentContext.Result.Message + "</pre>";
var errorMessage = TestContext.CurrentContext.Result.Message;
if (status == NUnit.Framework.Interfaces.TestStatus.Failed)
{
test.Log(LogStatus.Fail, status + errorMessage);
var ScreenShotPath = GetScreenShot.Capture(_webdriverChrome);
test.Log(LogStatus.Fail, "Screen Shot Below: "+test.AddScreenCapture(ScreenShotPath));
}
else if (status == NUnit.Framework.Interfaces.TestStatus.Passed)
{
test.Log(LogStatus.Pass, status + errorMessage);
}
extent.EndTest(test);
_webdriverChrome.Quit();}
和捕獲功能是
public static string Capture(IWebDriver Webdrievr)
{
string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile(projectPath + "ErrorReportScreenshot\\ErrorScreenshot.jpeg", ScreenshotImageFormat.Jpeg); //use any of the built in image formating
string _fullPathToReturn = projectPath + "ErrorReportScreenshot";
return _fullPathToReturn;
}
我收到一個錯誤
結果信息:
OpenQA.Selenium.WebDriverException :對 URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element的遠程 WebDriver 服務器的 HTTP 請求在 60 秒后超時。 ----> System.Net.WebException : The operation has timed out TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/screenshot timed out after 60 秒。 ----> System.Net.WebException : 操作超時
問題是,只要我從TearDown()
調用capture()
方法,它就無法截取屏幕截圖。 如果我只是通過在新測試中運行它來調用capture()
,它就像一個魅力。 在調試模式下,我可以看到它在這一行失敗: Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();
我錯過了什么?
編輯:我看過((ITakesScreenshot)Webdrievr)
並收到錯誤:
錯誤 CS0103:當前上下文中不存在名稱“Webdrievr”
調用堆棧:
> Assign_Represnt.dll!Assign_Represnt.GetScreenShot.Capture(OpenQA.Selenium.IWebDriver Webdrievr) Line 22 C#
我發現了問題。 出於某種原因,以下原因導致了這一切
var options = new ChromeOptions();
options.AddArgument("no-sandbox");
_webdriverChrome = new ChromeDriver(options);
我只使用了沒有選項的 chromedriver,現在可以使用了。
_webdriverChrome = new ChromeDriver();
我在嘗試在測試失敗時截取屏幕截圖時遇到了類似的問題。 當我嘗試在失敗情況下截取屏幕截圖時出現超時錯誤。 它在 try 塊中工作正常,但在 catch 塊中超時。 我沒有使用 Chrome 選項作為上面提供的解決方案。 任何幫助,將不勝感激。
下面是截圖的方法:
public class Logging
{
public static void ErrorScreenshot()
{
//Take the screenshot
Screenshot ssh = ((ITakesScreenshot)Driver.BrowserInstance).GetScreenshot();
//Save the screenshot
ssh.SaveAsFile("C:/Users/", ScreenshotImageFormat.Png);
}
}
這是我的測試方法
public static bool FindElement
{
get
{
try
{
var element = Driver.BrowserInstance.FindElement(By.XPath(" "));
if (element != null)
{
return true;
}
}
catch (Exception ex)
{
Logging.ErrorScreenshot();
Logging.Error("Not able to find element" + ex.ToString());
}
return false;
}
}
當它無法找到元素時,它會轉到 catch 塊,並且 Logging.ErrorScreenshot 方法會引發超時異常。
Error details below:
OpenQA.Selenium.WebDriverException
HResult=0x80131500
Message=The HTTP request to the remote WebDriver server for URL http://localhost:55418/session/f3dbde1645dd91e453c5823d72199ea9/screenshot timed out after 60 seconds.
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
at Logging.ErrorScreenshot() in C:\Users\Logging.cs:line 66
at DashboardPage.get_Verifylogin() in C:\Users\DasboardPage.cs:line 65
at Tests() in C:\Users\SmokeTests\Tests.cs:line 33
Inner Exception 1:
WebException: The operation has timed out
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.