Reputation: 53
I am using coded UI platform for automation. If test case fails system automatic take screen shot but once it pass system is not able to capture screenshot of the test case. I am using C# selenium commands in the script.
Environment
I tried following thing.
Enable a log trace in QTAgent32.exe.config ( ).
LoggerOverrideState = HtmlLoggerState.AllActionSnapshot; but getting error in LoggerOverrideState.
[TestMethod]
public void demo2()
{
TestContext.WriteLine("Go to URL\n");
driver.Navigate().GoToUrl("http://www.test.com/");
driver.Manage().Window.Maximize();
// Enter username
TestContext.WriteLine("TestContext Writeline: test context \n");
js.ExecuteScript("arguments[0].setAttribute('value','username')", driver.FindElement(By.XPath("//*[@id='txtUsername']")));
//Enter password
js.ExecuteScript("arguments[0].setAttribute('value','password')", driver.FindElement(By.XPath("//*[@id='txtPassword']")));
// Click on the login Button
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='btLoginNow']")));
Upvotes: 1
Views: 3086
Reputation: 35
The whole issue of taking a screen shot of a browser window & naming it based on unique properties (e.g., browser title, date-time stamp, etc) proved to be extremely difficult because of the tool (CodedUI) not what I wanted to do. I won't editorialize concerning my opinion of the functionality (lack of) of this tool but it is definitely not as robust as Selenium. So how I accomplished this was I got a high level 'browser title' from this-> and used the following for taking & storing the screen shot in my MyTestCleanup() method.
public void MyTestCleanup()
{
//Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
//--------------------------------------------------------------------------------------------
DateTime time = DateTime.Now;
this.UIMap
var ttest = time.Millisecond.ToString();
var timeSecs = DateTime.Now.Millisecond;
var Window = this;
var ext = ".png";
Image screen = UITestControl.Desktop.CaptureImage();
string dirPath = string.Format("C\\:Users\\smaretick\\Desktop\\SCREENS\\{0}{1}", ttest, ext);
string dirPathN = string.Format("/C copy C:\\Users\\smaretick\\Desktop\\SCREENS\\CUIT.png C:\\Users\\smaretick\\Desktop\\SCREENS\\{0}{1}", ttest, ext);
string dirPathF = string.Format("/C copy C:\\Users\\smaretick\\Desktop\\SCREENS\\CUIT.png C:\\Users\\smaretick\\Desktop\\SCREENS\\{0}{1}{2}", Window, ttest, ext);
//string dirPathF = string.Format("/C copy C:\\Users\\smaretick\\Desktop\\SCREENS\\{0}{1}{2}", Window, ttest, ext);
UITestControl.Desktop.CaptureImage().Save("C:\\Users\\smaretick\\Desktop\\SCREENS\\CUIT.png");
string cmdF = string.Format("C\\:Users\\smaretick\\Desktop\\SCREENS\\{0}{1}", ttest, ext);
Process.Start("CMD.exe", dirPathF);
//--------------------------------------------------------------------------------------------
LogOut();
contact me if you need any more help ([email protected])
Upvotes: -1
Reputation: 179
After every Testcase you can call the Take Screen shot in both the case either Pass or Fail ex. Suppose Test Method to do Successful Login
@Test
public void ValidUserNamePasswordLoginTest() {
// Your code to do Assertion
}
Now use @AfterMethod in your call which will execute as soon as your test perform
@AfterMethod
public void takeScreenShot(ITestResult testResult) throws IOException
{
if(testResult.getStatus() == ITestResult.SUCCESS)
{
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("Screensot\\"+filename+".jpg"));
}
if(testResult.getStatus() == ITestResult.FAILURE)
{
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("Screensot\\"+filename+".jpg"));
}
}
Upvotes: 1
Reputation: 346
You could put something like the following in your code to take a screenshot at any given point:
Image SubmissionPic = UITestControl.Desktop.CaptureImage();
SubmissionPic.Save(@"C:\AutomatedScreenShots\SubmissionPage_" + TestContext.DataRow["Division"].ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".bmp");
Note: The filename formatting I've used above can be changed to whatever suits your needs. I just pulled this code from a test I'd written a while back.
Upvotes: 1
Reputation: 8183
Couldn't you just use the [TestCleanup]
and create a method using that Attribute that will take a screenshot, that way you will always get a Screenshot not matter the result? Is that what you require?
So, You could so this on the WebDriver setup:
public static IWebDriver SetUp()
{
private static IWebDriver _webDriver;
firingDriver = new EventFiringWebDriver(new ChromeDriver()); //Or whatever driver you want. You could also use the Activator class and create a Generic instance of a WebDriver.
var screenshot = new CustomScreenshot(firingDriver);
firingDriver.ScriptExecuted += screenshot.TakeScreenshotOnExecute;
_webDriver = firingDriver;
return _webDriver;
}
Then for the screenshot something like:
public class CustomScreenshot
{
private IWebDriver _webDriver;
public CustomScreenshot(IWebDriver webDriver)
{
_webDriver = webDriver;
}
public void TakeScreenshotOnExecute(object sender, WebDriverScriptEventArgs e)
{
var filePath = "Where you want to save the file.";
try
{
_webDriver.TakeScreenshot().SaveAsFile(filePath, ImageFormat.Png);
}
catch (Exception)
{
//DO something
}
}
}
Upvotes: 0