Setting display resolution for Selenium tests on virtual machine as Jenkins Slave - selenium

I'm having trouble running some Selenium tests on a Jenkins Slave. To be specific, the display resolution that is used to run the tests is too small, causing some of the tests to fail.
To check the display resolution, we log the display height and width to the console, using:
driver.manage().window().maximize();
System.out.println("Window height: " + driver.manage().window().getSize().getHeight());
System.out.println("Window width: " + driver.manage().window().getSize().getWidth());
This returns:
Window height: 784
Window width: 1040
which seems like a very strange resolution to me. The desired resolution is 1920 x 1080.
The server that is used as a slave is a virtual machine (Windows Server 2012 R2). The Jenkins Slave is Connected via JNLP agent. The slave has the service running with Log On As "Local System" with the "Allow service to interact with desktop" option enabled.
So far we've tried a number of things like:
Connecting to slave VM using RDP and disconnecting to leave session open with desired display resolution
Using powershell to set the display resolution
Setting the default display resolution in the VM configuration
Setting the window dimensions using Selenium
And more...
All of these didn't resolve the issues. Suggestions are very welcome!

Finally managed to fix these issues after realizing that Jenkins does not necessarily need to run the slaves as a windows service. To start the slave, the JNLP agent can be downloaded from Jenkins and copied to the server. When running the JNLP file, you can select for the option to install the slave agent as a service.
Previously we had this option selected, that's why the slave was running as a service. After stopping and removing the service, we ran the JNLP file again and made sure to not select the option.
The solutions suggested that included disconnecting the remote desktop session and leaving the session open with a large resolution didn't work when running the slave as a service. They do work however when running the slave in the default way.
Make sure that the remote desktop session is not ended after a certain period of time:
Hope this helps someone!

Based on my experience with this you can't solve this problem programmatically. Your tests will run on the resolution which was used last time when you physically accessed this VM's display. For example, if I open VM on my big screen monitor and maximize it, tests will run on that resolution. But if I open it on my laptop screen and close RDP connection, tests will run on that smaller screen size.
I know it sounds strange, but I really couldn't find better solution. :D So now I must be careful to maximize VM display on my bigger screen before I close VM. You will probably dislike this answer, but remember it when you find yourself out of other solutions. ;)

Solution that worked for me is to run Chrome in a 'headless' mode (without GUI). It works with Jenkins Agent running as a service, when GUI is not available.
Here is the code sample of the web driver initialization:
var options = new ChromeOptions();
options.BinaryLocation = #"C:\Program Files\Google\Chrome\Application\chrome.exe";
options.AddArgument("Headless");
options.AddArgument("window-size=1920,1080");
driver = new ChromeDriver(#"<path>\Selenium.WebDriver.ChromeDriver.2.37.0\driver\win32", options);

I've been experienced the same issue. My seleniumn tests run under jenkins slave installed as windows service, using "Local System" account with "Allow service to interact with desktop" option enabled in windows 7. Some test cases were always failed due to incorrect display resolution.
I logged in to the console of the windows VM (EXSI Server + VMware Fusion), changed the resolution to 1400x900, and restart the windwos VM. Everything works well now.

Related

Jenkins - Timed out connecting to Chrome

Odd one here that I have not seen before. I decided to load a local instance of Jenkins on my Win 10 Laptop. The service is up and running and everything is building fine but when it tries to open chrome, I see the below in the log. I can execute it from Visual Studio and Command Line with no issues.
Research has mentioned lowering the chromedriver version so I tried with no luck. I have only run jenkins in master/agent config so maybe this has to do with jenkins being run on local host?
Starting ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-
refs/branch-heads/3865#{#442}) on port 7797
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to
prevent access by malicious code.
[1571096148.894][WARNING]: Timed out connecting to Chrome, retrying...
Additional things I have tried....
Creating a new user with all permissions to the jenkins dir
Running the service as the new user
OK I gave up running as a service. I copied the Jenkins.war file to the the directory I wanted. In this case, D:\Jenkins and then I executed:
D:\Jenkins>Java -jar Jenkins.war
This reloaded the Jenkins install. I added msbuild/tfs plugins and I am now running jenkins from the command line vs as a service. Everything is working now.
Just a work around but it may help someone else out.

Webdriver getting wrong resolution from bamboo agent

I have developed a framework with selenium + .NET
I'm running all test using Bambo agent. Everything is working fine, except one thing.
As you know, chromedrive can not set window size bigger than actual machine screen resolution.
My bamboo machine has set 1920x1080 resolution. To make sure that it is for real 1920x1080, I wrote a powershell script that is executed on agent berofe tests and print out resolution. Btw I have bamboo agent with Windows
But when webdriver start browser (chrome, firefox or IE) it limits the browser window size to 1044x788. Looks like the webdriver is getting wrong resolution from bamboo agent.
As you know webdriver is not allowing to set bigger size of window than you actual machine has. Thats why we limit browser size to 1044x788
There is no way to change size of window for bigger one. Things like: Manage().Window.Maximize() or Manage().Window.Size = new Size (1920x1080) or starting chrome with webdriver adding some options for chrome like: chromeOptions.AddArgument("--window-size=1920,1080"); it is always limited to 1044x788.
Did any one have similiar problem? Can I somehow "hack" webdriver to open browser with 1920x1080? Meaby somebody knows how webdriver is checking the screen resolution? If he is looking for resolution in some windows file then meaby I could inject 1920x1080 resolution there?
Or meaby there is some setting in bamboo agent that I missed?
Why is that important? Becouse at same point I faced problem that on this smaller browser some webelements sometimes are overlaping (figure out that on screenshots) and it case some test to fail.
When I run test on my laptop it always have this 1920x1080 resolution.
P.S. I know about Selenium Grid but my aproach works great for me, until now :)
Before second I wrote another powershell script and create a screenshot on bamboo machine, and the screenshot has 1024x768 resolution. So meaby it is coused somehow by machine..
I know the answer. Bamboo runs all tasks as windows service (not as a user). That's why I was not able to see the browser and the screen resolution was wrong. There are two solutions:
Set up selenium grid hub and node on that one machine with your user, and paste url of hub in you framework. Then bamboo service will trigger selenium grid hub that is set by your 'user' and you will be able so see browser when you will log into the virtual machine and it wil have normal resolution (I have select this solution)
I think that there could be another solution to set up bamboo to run tasks not as windows service but as a user (not sure if it is possible).

Logging the output of VBoxHeadless

I want to be able to launch a VM in headless mode and log the outputs of its tty to the command line.
Basically I have a situation where the VM will be launched remotely (hence the command line requirement), but every once and a while, the VM's bootup sequence freezes, and right now there's no way for me to debug why the VM is not booting up properly unless I go there manually and view it in a non-headless mode.
The VM itself does not have a GUI, it just boots into TTY mode.
So is there anyway I can get some debugging output from the TTY while it's headless?
One crazy idea would be using non-headless mode and taking screenshots of the window that opens, but there must be a better way!
You can use the following command to output a PNG file of the current screen, even in headless mode:
vboxmanage controlvm [vmname] screenshotpng screen.png
I am trying to get some integration testing for bootstrapped virtualboxes up and running and ran into the exact same problem. After finding out that VRDP is a dead end, because RDP cannot send text but only bitmaps, I remembered that some cloud providers require grub to output to ttyS0.
What is ttyS0 you ask? It's the serial console of course :-)
Here's how to configure Debian to output to serial console
VirtualBox allows you to redirect the serial port into either a file or a pipe, meaning you should be able to get all the information you desire by simply reading the specified filepath.

How to configure a Windows Server 2008 VM to run with a specific resolution

I have a build server running Windows Server 2008 R2. It runs a suite of automated acceptance tests which use the Selenium web driver. These tests are triggered automatically after a check-in and are failing due to having too small of a screen resolution. They are unable to access elements that are in a modal window because the modal window is too big to fit within the limited viewport that the tests are running in.
If I RDP into the machine at 1280 x 1024, I can run the tests and see that they pass just fine. Is there a way to specify the "default" resolution for a windows box when a user isn't actually logged in with a monitor?
Thanks!
I found a link, it looks like you can set your desired resolution with regedit: http://philipflint.wordpress.com/2008/06/30/changing-the-screen-resolution-in-windows-server-2008-server-core/
From the URL:
HKLM\System\CurrentControlSet\Control\Video\{ClassID}000\DefaultSettings.XResolution
HKLM\System\CurrentControlSet\Control\Video\{ClassID}000\DefaultSettings.Yresolution
The ClassID is a GUID. There is one for each display driver installed on your system. You can tell which one is currently in use as below the 0000 Key you will have another Key called “Volatile Settings”.

Getting black images with selenium.captureScreenshot

I'm executing selenium tests with testng, that are started on a remote system with Selenium RC via hudson (with ssh connection). The remote system is windows xp with MKS Toolkit installed, hence ssh. Tests are NOT executed as a windows service.
I've tried using both captureScreenshot and captureEntirePageScreenshot methods. The first one always produces a black image. The second one creates the correct screen shot but it only works on Firefox and our tests usually pass on Firefox and fail in other browsers, so it is crucial to capture screen shots for the other browsers (mainly IE and Safari). The tests are ran in parallel, with many browser windows open at the same time. I'm not certain if this is what's causing the problem. Any thoughts will be appreciated.
Unfortunately screenshots in Selenium have been problematic from the start in browsers that are not Firefox. This is something that we Selenium Developers have been working on for a while to correct.
The latest work has been updating Snapsie to work in IE. There is a blog post at http://blog.codecentric.de/en/2010/02/remote-screenshots-mit-selenium-und-dem-robot-framework/ that explains what has happened.
I have noticed that if the screen isn't active, i.e. the screensaver has kicked in, it can produce black screenshots.
Edit:
I just had a thought. You can always run Castro to video record your tests and then watch it play back. This is something SauceLabs use to run Selenium in the cloud.
Write a method for this and call that whereever you need to take the screenshot. Use the java.awt package which has been used in selenium. For example, check this site
After setting Windows Auto-Logon, and launching process not as Windows Service, I found how to solve the Remote Desktop with Black Screenshots problem of IEDriverServer.exe, by creating a batch file that disconnects RDP, instead of closing the RDP session with the regular X button:
%windir%\system32\tscon.exe %SESSIONNAME% /dest:console
See more details here:
https://stackoverflow.com/a/24529629/658497
(Although, I would prefer there was a way to run it as the default action, when terminating RDP session with X Windows button).