1

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

  • Visual studio premium 2012.

I tried following thing.

  1. Enable a log trace in QTAgent32.exe.config ( ).

  2. 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']")));
    
Niraj
  • 53
  • 1
  • 9
  • You appear to just be using WebDriver & CodedUI to execute JavaScript. Do the built in methods like `Click` and `SendKeys` not work? – Arran Jan 12 '15 at 10:16
  • Thanks for reply Arran. But it is working fine . I just need to capture screen shot of passed test case, which coded UI take automatic once we generated script with Record and Play. – Niraj Jan 12 '15 at 11:13

4 Answers4

1

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.

LuFaMa
  • 346
  • 1
  • 6
  • 15
  • This is a great and simple way to take a screenshot of the whole screen. I had to remove the TestContext bit though, not sure what it is. – Ewan Nov 28 '16 at 11:39
1

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"));
    }

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Samir 007
  • 179
  • 2
  • 8
0

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
        }
    }
}
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • Thanks for reply Jamie. Yes something like that, do i need to put code in [TestClean], is there any other option to catch the event and capture screenshot in my log. – Niraj Jan 12 '15 at 11:33
  • I need to capture screenshot of every event for UI which my script does. E.g Suppose if the js.ExecuteScript("arguments[0].setAttribute('value','username')", driver.FindElement(By.XPath("//*[@id='txtUsername']"))) execute then it should capture screen and same for another event. – Niraj Jan 12 '15 at 11:48
  • Then using the `[TestCleanup]` attribute will not work for what you require. I have updated the original answer to reflect what you could use. – Jamie Rees Jan 13 '15 at 10:33
-1

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 (scottmaretick51@gmail.com)