1

I am using Selenium WebDriver in C# and I am trying to dynamically create a folder and save screenshots of failing tests to it. Here I am running the group of test cases (Test Suite of 66 test cases). After running the test suite I found few failed tests with GDI+ error and were not captured as a screenshot. But when I run them individually most of the failed cases (GDI+ error) were passing except few.

Here is the code for creating a folder:

TestExecutionStartTime = DateTime.Now;
baseDirectory = AppDomain.CurrentDomain.BaseDirectory + @"\" + ConfigurationManager.AppSettings.GetValues("failedTests")[0];
Browser = ConfigurationManager.AppSettings["WebDriver"];

DirectoryInfo directory = new DirectoryInfo(baseDirectory);
DirectoryInfo[] subdirs = directory.GetDirectories();

if (System.IO.Directory.GetDirectories(baseDirectory).Length == 0)
    {
        screenshotDirectory = baseDirectory + @"\" + (DateTime.Now.ToString("yyyy_MM_dd_hh_mm") + "_" + Browser);
        Directory.CreateDirectory(screenshotDirectory);
    }

Here is the code for taking screenshot:

public void takeScreenshot(string filename)
        {
            string fname = filename + ".jpg";
            string screenshot = screenshotDirectory + @"\" + fname;
            Screenshot ss = ((ITakesScreenshot)WebDriver).GetScreenshot();
            byte[] image = ss.AsByteArray;
            using (MemoryStream ms = new MemoryStream(image))
            {
                Image i = Image.FromStream(ms);
                i.Save(screenshot);
            }

I assume that the error is at this i.Save(screenshot) call, but I was not able to resolve it.

Laurel
  • 5,965
  • 14
  • 31
  • 57
mitikiri
  • 9
  • 2
  • so you just want to capture screenshots when a test fails and place them in subdirectory that's named with the date? – AntonB Aug 11 '16 at 18:51
  • Which driver are you using? The GDI error is probably due to an empty image returned by the driver. You could retry to take the screenshot when the exception occurs. – Florent B. Aug 11 '16 at 18:53

2 Answers2

0

I have reason to believe (from experience) that your issue comes about as a result of the stream being destroyed while it is being saved (the using statement).

Things to be aware of:

  1. Write permissions wherever you are saving the image
  2. Make sure the path is correct - this will throw a GDI+ exception and is very misleading, verify your path, try a temporary directory instead of creating your custom image directory to rule this one out.
  3. Make sure the height of the image is not bigger than (65534px)

    1. You can verify this by looking at the size:

          var bitmapTemp = new Bitmap(stream);
          Console.WriteLine(bitmapTemp.Height);
      

Here's some code that destroys the stream only after the image is saved:

    public static Screenshot GetScreenshot(ChromeDriver driver)
    {
        Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        return ss;
    }
    public static void SaveScreenshot(byte[] byteArray, string location)
    {
        var stream = new MemoryStream(byteArray);
        var img = Image.FromStream(stream);
        img.Save(location);
        stream.Dispose();
    }

And use the functions like so:

    var path = AppDomain.CurrentDomain.BaseDirectory;
    var ss = GetScreenshot(driver);
    SaveScreenshot(ss.AsByteArray, path + "imagename.jpg");
AntonB
  • 2,724
  • 1
  • 31
  • 39
-1

Thanks for your inputs AntonB. I have considered your points and tried differently and got the solution. i have used [SetUpFixture], [OneTimeSetUp] and [OneTimeTearDown] to create folder only once and it solved the problem. Here is the code:

[SetUpFixture]
public class Config
{
    public Config()
    {

    }
    public string baseDirectory;
    public static string screenshotDirectory;

    [OneTimeSetUp]
    public void SetUp()
    {
        Console.WriteLine("Creating a folder to capture failed scenarios");
        baseDirectory = AppDomain.CurrentDomain.BaseDirectory + @"\" + ConfigurationManager.AppSettings.GetValues("failedTests")[0];
        string Browser = ConfigurationManager.AppSettings["WebDriver"];

        screenshotDirectory = baseDirectory + @"\" + (DateTime.Now.ToString("yyyy_MM_dd_hh_mm") + "_" + Browser);
        Directory.CreateDirectory(screenshotDirectory);
    }
    [OneTimeTearDown]
    public void TearDown()
    {

    }
}
tetralobita
  • 453
  • 6
  • 16
mitikiri
  • 9
  • 2