截图
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;
}
我遇到错误
Result Message:
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element timed out after 60 seconds.
----> 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 seconds.
----> System.Net.WebException : The operation has timed out
问题是,只要我从 TearDown()
调用 capture()
方法,它就无法截取屏幕截图。
如果我只是在新测试中通过 运行 调用 capture()
,它就像一个魅力。
在调试模式下,我可以看到它在这一行失败:Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();
我错过了什么?
编辑:
我看过 ((ITakesScreenshot)Webdrievr)
并收到错误消息:
error CS0103: The name 'Webdrievr' does not exist in the current context
调用堆栈:
> 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
我正在尝试截取测试失败的屏幕截图。
[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;
}
我遇到错误
Result Message:
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element timed out after 60 seconds. ----> 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 seconds. ----> System.Net.WebException : The operation has timed out
问题是,只要我从 TearDown()
调用 capture()
方法,它就无法截取屏幕截图。
如果我只是在新测试中通过 运行 调用 capture()
,它就像一个魅力。
在调试模式下,我可以看到它在这一行失败:Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();
我错过了什么?
编辑:
我看过 ((ITakesScreenshot)Webdrievr)
并收到错误消息:
error CS0103: The name 'Webdrievr' does not exist in the current context
调用堆栈:
> 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