8

I have added PhantomJS and Selenium to my C# console app and i want to take a screen shot of the browser when it gets to a specific element. The reason is because for some reason when i use the ChromeDriver it works fine, but when i use PhantomJS it fails on a few elements.

I guess i need an introduction on how to take a screenshot in C# using phantomjs. I have looked around on the internet and it looks like everyone is using java scripts to do this. The problem i am having is i don't know how to integrate the java scripts into my C# app and then use that with phantomJS to get the screen shot. If i can get some help on how to do this it would be very nice.

TLDR: I have found http://code.tutsplus.com/tutorials/testing-javascript-with-phantomjs--net-28243 and this is what i want to do but i don't know how to use the javascript in my c# app.

Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
Darthlemi
  • 173
  • 1
  • 1
  • 9
  • This question already has an answer. http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver – Aaron Carlson Jul 07 '14 at 16:16
  • 1
    damn man i thought i did enough searching thanks and sorry for wasting your time – Darthlemi Jul 07 '14 at 16:17
  • Also, you may need to maximize your browser window. I noticed with the phantomJS driver that the screen was not maximized by default. Read the second answer from this question: http://stackoverflow.com/questions/3189430/how-do-i-maximize-the-browser-window-in-selenium-webdriver-selenium-2-using-c – Aaron Carlson Jul 07 '14 at 16:17

1 Answers1

11

As you mentioned that you have code working for Chrome already, it's better to post it, in order to show what exactly you are after.

However, here is how to take screenshot using PhantomJSDriver in C# in general:

var driver = new PhantomJSDriver();
driver.Manage().Window.Maximize(); // optional
driver.Navigate().GoToUrl("http://stackoverflow.com");

driver.TakeScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);

driver.Quit();

Note that you need to reference WebDriver.Support.dll and System.Drawing in your project.

Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • 5
    For those looking at this, the TakeScreenshot() is an extension method found in OpenQA.Selenium.Support.Extensions – Frederik Oct 23 '15 at 12:48
  • 8
    To get this to work, I had to install the [PhantomJS](https://www.nuget.org/packages/PhantomJS/), [Selenium WebDriver](https://www.nuget.org/packages/Selenium.WebDriver/), and [Selenium WebDriver Support Classes](https://www.nuget.org/packages/Selenium.Support/) NuGet packages, and add the OpenQA.Selenium.PhantomJS, OpenQA.Selenium.Support.Extensions, and System.Drawing.Imaging namespaces to my class. After that it worked perfectly. :-) – Gabby Paolucci Dec 16 '15 at 15:15
  • 1
    You also need to include the namespace, so using `OpenQA.Selenium.Support.Extensions;` – Liam Oct 03 '16 at 09:45