Selenium C# WebDriver not resizing browser window correctly - c#

I have to make visual tests for test automation purposes, with window browser size being (x=1366,y=668). When I'm running these tests in a headless mode, everything works fine and the window is as large as it should be. However, when I run the same tests WITHOUT headless mode, the browser window always is a bit smaller than it should be.
How can it be made that the window is being sized as it is specified?
Here we can see that the size of the window is not as expected - (1366, 668)
I have tried to use ChromeOptions for specifying window size.
I have also tried to resize the browser window with this line of code:
_driverHelper.Driver.Manage().Window.Size = new System.Drawing.Size(1366, 668);
But so far nothing has worked as expected.

You could try to use javascript esecutor to try it out
driver.Navigate().GoToUrl("http://www.example.com/");
((IJavaScriptExecutor)driver).ExecuteScript("window.resizeTo(1366, 668);");
For me, personally, setting user specific window size in ChromeOptions work just fine.
Please also have a look here. The size of the viewport and the outer window can be a bit confusing at times.

Related

C# Selenium No new Window Handles When Opening A New Tab In Chrome and Firefox

Selenium for Chrome and Firefox, coding in C#, will not handle switching over to the new tab that is opening during my automation test. I don't think it focuses on the new tab open because when I run my program completely, I receive this error: "out of range" exception and "Index was out of range. Must be non-negative and less than the size of the collection."
I've tried using:
driver.SwitchTo().Window(driver.WindowHandles.Last());
and
driver.SwitchTo().Window(driver.WindowHandles[1]);
driver.SwitchTo().Window(driver.WindowHandles[0]).Close(); // close the first tab
System.Threading.Thread.Sleep(5000); //Wait 5 seconds just in case
driver.SwitchTo().Window(driver.WindowHandles[1]); //Makes sure I'm on the correct tab
but there is no luck. If I don't close the first tab, I receive the error: "NoSuchElementException:Unable to locate element:{method...}" which I assume is correct because the focus is still on the first tab. Any help would be greatly appreciated. Thank you.
Once you've closed tab[0], the old tab[1] becomes the new tab[0], so you should be switching to tab[0].
Selenium operates in the background so the window does not need to be in focus while it is running. This also means that Selenium will never change the focus back to the browser window. If you are watching the window while your code runs however you should be able to see the browser changing tabs.

C# IE11 selenium webdriver, No new window handles with new tab

Selenium Webdriver for Internet Explorer 11 is not creating a new window handle when my app opens a new tab.
The problem is similar to this , which was a problem with the window handles disappearing in a fraction of a second, but in my case a new window handle isn't even created in the first place.
I used the test code from that link to check for disappearing window handles, but it never created another window handle when a new tab was opened, so the test only displayed the first console output and never the second part.
I tried using:
driver.SwitchTo().Window(driver.WindowHandles.Last());
but the Selenium Webdriver focus stays on the first tab, because I believe it isn't creating a new handle for the new tab.
Also, tried this:
driver.SwitchTo().Window(driver.WindowHandles[1]);
and that results in an "out of range" exception and "Index was out of range. Must be non-negative and less than the size of the collection."
If I change the [1] to a [0], then obviously I do not get the exception because the focus is just set to the first tab.
Based on my testing with the test code in the link I provided and my own attempts, I believe that new window handles are not being created.
I tried resetting IE11 and then only changing the settings as described on Selenium's website including adding the registry key, which I used Helium's solution to that found here.
Also, I read some suggestions of adding the websites that I am opening to the Trusted sites list in IE, but still no go.
Any help is appreciated. Thanks.
Hopefully this will help someone going forward...
I have been researching this after having the same issue and whatever I tried it did not resolve it!
In the regedit I added the 'BF_Cache' -> 'iexplorer.exe', added '2500' set as '3' in the zones 0,2,3,4 (was already present in zone 1) & Set the 'protected mode' to enabled but no luck... My new window would open correctly through Selenium but when debugging I confirmed only one handle was available!
But... After I nearly gave up - I decided to run visual studio as an
admin. And as if by Magic Selenium picked up that there was more
than one window handle!
Here's a workaround I managed to use.
You can use javascript to open a new window. It will give you the handle.
string javaScript = "window.open('" + url + "','_blank', 'resizable=yes, scrollbars=yes, titlebar=yes, width=1280, height=680, top=10, left=10');";
jsExecutor = (IJavaScriptExecutor)ieDriver;
jsExecutor.ExecuteScript(javaScript);

Taking an IE screenshot returns a black image

I'm building a console app which will connect to different computers in the network and take browser screenshots of a webpage.
Using Selenium 2.47.1 to set up server & nodes. The console app runs in the PC which is set up as selenium hub.
The screenshot is fine in firefox,chrome,ie from the hub computer.
The screenshot is also fine in firefox in remote pc.
But with IE it returns a black image.
Both the hub and node computers run on windows 7 64-bit, have IE11. I am using the 64bit IEDriver in both PCs.
The node computer is not locked.
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
static void Main(string[] args)
{
IWebDriver NewDriver = null;
using (NewDriver = new RemoteWebDriver(new Uri("http://172.165.10.111/wd/hub"), DesiredCapabilities.InternetExplorer()))
{
if (NewDriver != null)
{
NewDriver.Navigate().GoToUrl("http://www.google.com");
NewDriver.Manage().Window.Size = new Size(1804, 1096);
Screenshot ss = ((ITakesScreenshot)NewDriver).GetScreenshot();
ICapabilities capabilities = ((RemoteWebDriver)NewDriver).Capabilities;
ss.SaveAsFile(#"C:\Path\123.png", ImageFormat.Png);
NewDriver.Quit();
}
}
}
It's because your screen is locked, or a screensaver is running.
You will need to turn off your screensaver and configure windows to never lock itself when not in use. To turn off your screensaver:
Click the Start button.
Click Control Panel.
In the search box, type screen saver.
Click Turn screen saver on or off.
Then modify your screensaver settings. Make sure you have unchecked "On resume, display logon screen".
While you're in the control panel it's probably worth checking the power options and making sure the machine isn't going to sleep or powering down after a set period of time as well.
You will also want to use VNC or remote assist to access the GUI. If you RDP in it will lock the screen for the local user who is currently logged in and again it will lock the screen when you disconnect.
Finally don't use the 64-bit IE driver, you should be using the 32Bit one. Nobody runs the 64Bit version of IE (even if they have a 64Bit capable machine).
****** Edit
Adding a bit more info from a credible and reputable source ******
Below is a link to a post on the Selenium users forum where Jim Evans (the developer who wrote the IEDriver binaries) explains this:
http://selenium.10932.n7.nabble.com/IE-Screenshots-are-black-when-not-connected-via-Remote-Desktop-to-the-VM-hosting-an-IE-Node-td37004.html
This quote in particular about taking screenshots when you have disconnected from a RDP instance:
It's a known limitation. There is no known workaround. Complain to
Microsoft. They're the ones that make the PrintWindow API (which is
the proper API to use when grabbing screen captures) behave that way.
Either that, or if you discover a way to make it work, you're welcome
to submit a patch.
He explains how the screenshot code works in more detail on his blog here:
http://jimevansmusic.blogspot.co.uk/2014/09/screenshots-sendkeys-and-sixty-four.html
Specifically:
The IE driver takes screenshots using the PrintWindow API function.
PrintWindow can only take a screenshot of the visible portion of any
given window, which means that in order to get a full-page screenshot
(as required by the WebDriver API), the window must be sized large
enough to display the entire page without scroll bars. However,
Windows does not allow the window to be resized larger than the
visible screen resolution. When we ask IE to resize itself, a
WM_GETMINMAXINFO message is sent on a resize event so the IE can
figure how large a window can be. By intercepting that message with a
hook, and modifying the max values, we can trick IE into thinking that
a window can be sized greater than the screen resolution would
otherwise allow.
It looks like this might be a known issue with configuration:
https://code.google.com/p/selenium/issues/detail?id=3536
From what #ShubhasmitGupta mentioned above, it does look like the IE driver hands out black-screen screenshots when the desktop is locked (I'm assuming this has something to do with DWM/Explorer not rendering windows).
There's a workaround involving not locking the targeted computer when connecting to it via RDP. Essentially, you create a disconnect.bat file with the following contents:
tscon rdp-tcp#0 /dest:console
I'm not sure exactly when that's supposed to be run, but the concept is to prevent the test computer from being locked when connecting via Remote Desktop. If that helps, great; I haven't been able to test the workaround myself and figure out how to set it up. Maybe someone else can write a more concise answer.
Open up the Service Control Manager -- You can do WindowsKey+R (run) and type services.msc and hit <ENTER> and it'll start up.
Find the service and right click it and choose properties from the context menu.
Allow Service to Interact With Desktop
Now, select the Log On tab.
You want to make sure the checkbox is selected for "Allow service to interact with the desktop".
You can see it in the image below.
Note: I don't have that service so I just picked another one in the sample picture.
you may want to try removing below line
//NewDriver.Manage().Window.Size = new Size(1804, 1096);
maximize your browser instead.
NewDriver.Manage().Window.Maximize();
I have had a sneaky suspicion that IE has been rendering with DirectX for some time and this article certainly seems to indicate that this is the case... which would explain why you'd have problems as GDI based screen grabs won't work with DirectX surfaces.
I'm saying this is an absolute... just a hunch
I had the same issue a while back, and though I don't completely understand the problem it was basically that the process executing selenium was running under a system account and thus the IE gui was visible only in session 0, whereas session 1 was showing only a black screen.
The solution which worked for me was to create a local user account on the remote host, keep that user permanently signed in and unlocked so that they have an interactive desktop available, then run your selenium app/tests as that user. I know that is not ideal, but as we had dozens of old boxes lying around, it didn't hurt to use one as a dedicated test server. Hope that helps somewhat!
Sorry, not enough rep to add this as a comment.
Perhaps an interesting hint...
...working with Selenium 2.47.x and 2.52.0 Jenkins, RDP and IE 11 in mode IE 7 (setting by website) on the server Win 2008. Additional to this, I have to screens connected to my PC.
If I am disconnected from the Win Server (not logged off) I have two situations:
The JAVA prog on the Server produces black boxes or real screenshoots. The difference is just one marker in RDP options in the display part on the local RDP configuration of my client (Win 7).
I have not an english spoken display so I hope the translation may ok.
The configuration detail is: "using all monitors by remote session"
If this marker is set, I get real screen shoots. If this one is not set I have black boxes. I can't explain why, but it works in this way...
Windows would sometimes cause black screens because the machine/desktop was not in use.

box wont maximize further

I have this app written in C# and it won't maximize further even though the code forcefully specifies the size, the window doesn't follow it. Other windows app follows the size that I specified but this one doesn't (been figuring what might go wrong but it seems a bug). Thanks stackoverflow
As you can see, the running window is not following explicitly what the code tells. The displayed window should be way larger than the current size.

Silverlight - First time going to Full Screen opens in background (FF, Chrome, not IE)

I have not found an answer to this, not here or when I have googled for it.
The case is that we have a silverlight with a video-stream. If we have enabled fullscreen with the code : Application.Current.Host.Content.IsFullScreen first time the application is taken to fullscreen it opens in the background (and for the customer it seems like nothing happens).
It seems it will remember it to the next time.
The alternative for our sake is to not enable it to disable this so that when the window loses focus it is taken out of fullscreen.
Is there a way to get around this, as we would both like to open it in fullscreen on top of the screen (not hidden), and have the possibility to make it pinned there. (Since many of our users would like to have it on another monitor and use the computer at the same time)
I have been thinking about storing it in application storage, as it seems it rememebers the first time a user move it in front. But dont know how this can be done, and also seems a bit hackish.
In IE this works, but of course this is not good enough.
To have your application stay fullscreen even when not focused, you have to add the following to your Application:
Host.Content.FullScreenOptions = System.Windows.Interop.FullScreenOptions.StaysFullScreenWhenUnfocused;
This will generate a prompt for permission when you switch to full screen mode, similar to IsolatedStorage quota prompts.
For the app showing in background, I only have experienced this using Safari on MacOS, I never had this issue on Windows IE, FF or Chrome. Maybe the line above will help matters though.

Categories

Resources