Keep Selenium Webdriver minimized while taking a screenshot - c#

I'm using selenium webdriver to load and take the screenshot of a webpage at regular intervals this way:
System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(new System.IO.MemoryStream(byteArray));
however, every time this line gets called, the chrome window becomes maximized and in the way. Is there a way to have this happen in the background without chrome popping up all the time?

Yes, running Chrome headless.
Starting from version 59 for Mac and version 60 for Windows it’s possible to run Chrome in headless mode, effectively hiding the browser window.
using OpenQA.Selenium.Chrome;
...
// Create a browser with BrowserOptions
var options = new ChromeOptions();
options.AddArgument("headless");
var driver = ChromeDriver(options);
// Do your other Selenium stuff here
// Take your screenshot
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(fileName, imageFormat);

Related

How to increase the screen resolution on Test Agent in TFS 2015

I'm using TFS 2015 to trigger Selenium automated test suite. Few of test cases are failing due to some HTML elements get overlapped and this happens due to the screen resolution of the session which TFS service account opens is smaller (Programmatically I took screenshots while running the test methods in TFS). All the test cases are passing when I run from Visual Studio or through CMD locally or Remote Desktop session in TFS server.
I tried increasing the screen resolution using bellow techniques , but still screenshots saved in TFS server shows smaller screen size.
Increase the window size :
driver.Manage().Window.Size = new Size(x, y);
Chrome capabilities - Resolution
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("resolution", "1920x1080");
Chrome capabilities - Window Size
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--window-size=1300,1000");
Maximize the window
driver.manage.window.maximize();
None of these approaches works since the screen resolution of the session which TFS service account opens is smaller.
When I run the test cases with Headless option, browser windows opens with Maximum window size but test fails since selenium can't identify the elements (NoSuchElementException) :
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
(Project which I'm automating is a ReactJS project)
I would like to know a way to increase the screen size when test are being executed form TFS agent.
Thank You
chromeOptions.addArgument("--headless");
chromeOptions.addArgument("--disable-gpu");
chromeOptions.addArgument("--window-size=1920,1080")
WebDriver driver = new ChromeDriver(chromeOptions);
don not use browser.maximize after this it will reset the windows size to system default.
Try also:
chromeOptions.addArguments("--headless","--disable-gpu","--window-size=1920,1080")
WebDriver driver = new ChromeDriver(chromeOptions);
Debugging tips
See if the test are passing with same window size but non headless mode
If it does pass then it is because of faster execution speed , headless browser is faster than non headless browser so you need to add more explicit waits for those elements

Can't take screenshot in command line with Chrome

My final goal is to be able to take automated screenshots. For this I've been reading this article which says that it should be as simple as this in the command line:
chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/
But I don't get any screenshot.png files anywhere in the current directory, in my case C:\temp. I've also tried omitting the 'headless' parameter but the only thing that happens is that Chrome opens the website but no file is created.
The plan is to use chrome and selenium together as such:
public void ScreenshotPage(string url)
{
ChromeOptions o = new ChromeOptions();
o.AddArgument("window-size=1920,1200");
o.AddArgument("disable-gpu");
o.AddArgument("disable-extensions");
o.AddArgument("headless");
o.AddArgument(url);
using (var driver = new ChromeDriver(o))
{
//driver.Navigate().GoToUrl(url); // if o.AddArgument(url) doesn't work then try this instead.
//Thread.Sleep(10000);
Screenshot screenshot = driver.GetScreenshot();
screenshot.SaveAsFile(#"C:\temp\test.png", ScreenshotImageFormat.Png);
}
}
The result is a blank png-file which seems to have the right resolution.
One reason that I'm looking into headless Chrome is that my program is going to be run in a virtual machine and without a user logged in. This creates a problem for ChromeDriver's GetScreenshot() since it will use a very low resolution for taking the screenshot. I was hoping that a headless Chrome would solve this issue.
There doesn't seem to be any relevant information on the Internet, save Using headless Chrome as an automated screenshot tool (alternative to PhantomJS) where the author has explicitly stated that he is targeting Chrome v60. I'm a bit hesitant using puppeteer since I'm writing in .NET and not so sure if current ports are
Environment: Google Chrome 63.0.3239.132 64-bit, Windows 10 (and Server 2012), Chromedriver 2.34
Give this one a try
using (var driver = new ChromeDriver(o))
{
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(#"C:\temp\test.png", ScreenshotImageFormat.Png);
}

Selenium: Chrome doesn't maximize under IIS

I have a web API application. When I call some URL, selenium starts and goes to some site and makes screenshots and saves them to files.
When I run code in Visual Studio by pressing F5, the application works well.
But after I'd publish my application to IIS, I noticed that browser doesn't maximize and I got small screenshots.
I use a chrome driver, because IE driver and Firefox driver throw error
unable to connect.
I tried this code:
var chromeOptions = new ChromeOptions()
{
};
chromeOptions.AddArgument("--start-maximized");
using (IWebDriver driver = new ChromeDriver(chromeOptions))
{
driver.Manage().Window.Maximize();
/*driver.Manage().Window.Size = new System.Drawing.Size(1920, 1040); this doesn't work too*/
I notice when selenium starts under IIS I don't see a browser window. I only see a process of chrome in the task manager.
How to make maximize window of a browser under IIS?
Instead of :
chromeOptions.AddArgument("--start-maximized");
Can you try :
chromeOptions.AddArgument("start-maximized");
And remove :
driver.Manage().Window.Maximize();

Selenium, how to take screenshot without opening the browser?

I want to take a screenshot of a webpage using Selenium.
I have notice that the action to take the screenshot require to open the web browser itself.
tried to change webDriver.Navigate().GoToUrl("http://www.google.com"); with webDriver.Url = "http://www.google.com"; but no success,
I even tried to leave it with no url and the browser opened with url of 'data', which now I understand that something else makes the browser to be open.
private void button1_Click(object sender, EventArgs e)
{
var capabilitiesInternet = new
OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.
SetCapability("ignoreProtectedModeSettings", true);
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("http://www.google.com");
Screenshot screenshot = ((ITakesScreenshot)webDriver).GetScreenshot();
screenshot.SaveAsFile("E:\\ScreenShot.png",
System.Drawing.Imaging.ImageFormat.Png);
webDriver.Quit();
}
No - you need to let the WebDriver request the page, otherwise how can it know what screenshot to produce?
If you're trying to avoid a real, 'slow' browser starting up and opening a window, you should either consider running that browser headlessly, as per:
How do I run Selenium in Xvfb?
Or check out the headless WebKit browser PhantomJS (or maybe SlimerJS), and using almost exactly the same WebDriver API as you have now, ask it to produce your screenshots 'in-memory':
Phantomjs - take screenshot of a web page
Just replace:
WebDriver webDriver = new ChromeDriver();
with:
WebDriver webDriver = new PhantomJSDriver();
(Obviously requires the application to be installed locally)
Edit: Just a note that the typical use-case for this is 'overnight' continuous-integration / continuous-testing when run from headless CI servers. However, it can be very easily added to other work-flows, e.g. for visual regression-testing, and simple one-off checks.
There's multiple ways to go about it:
There is chrome flag --no-startup-window which should open chrome without window. I tried it, and it didn't work, however, you can give it a go.
Use hacks - there is a way to start chrome without you ever seeing it, it works nicely. Just use --window-position=-9999,0, note that, TakeScreenShot() has some weird anomalies in my experience - it focuses the window, it runs out of memory randomly, etc. What I ended up doing is I wrote chrome extension which can take screenshots and return them back to selenium through JS.

How to stop invoking of Firefox browser before opening any othre browser in selenium web driver cross browser testing

I am using selenium WD in C# for cross browser testing but facing a strange problem that when ever i run my test using Nunit firstly Firefox window will open & then my desired browser window will open & run the test on it(desired browser).
As per my knowledge if any system is not having Firefox installed in it then it fails the script.
So is there any way to change this default value of browser in selenium.
I am able to run tests on different browser, my problem is only that before opening my desired browser by default first system is opening firefox. which create issue for me & my tests.
public void SetupTest()
{
driver = new SafariDriver();
baseURL = "http://google.com/";
verificationErrors = new StringBuilder();
}
Most probably, somewhere in your code you are initializing the Firefox Driver. Search for this within you code:
new FirefoxDriver();
You could also debug to the line
driver = new SafariDriver();
and see if it has an assigned value already.
But I am also pretty certain that you are initializing a FirefoxDriver somewhere.

Categories

Resources