Increasing screen resolution for selenium test in AWS instance Increasing screen resolution for selenium test in AWS instance selenium selenium

Increasing screen resolution for selenium test in AWS instance


There is a workaround that I was able to come up with for this exact scenario --

The problem is that unless you use RDP to connect, AWS EC2 instances use the default basic graphics adapter, which has a max res of 1024x768.Trying to set the Browser size via Selenium will not work, as the browser window will be automatically resized to the maximum desktop resolution size.

However, you can do this:After your browser is opened during your test run, you can call into unmanaged code and set the browser window's size via a method in user32.dll with a flag that stops Windows from automatically resizing the browser window back to the maximum desktop resolution size.

The code is as follows (partially taken from pinvoke.net). This is a C# example, but could be reworked into some other type of solution to be called from your framework.

Within a class somewhere within your framework, place the following, and then in your code to instantiate your browser window, simply make a call to the SetBrowserLocAndSize method:

// Windows Interop Screen window position setter extern method[DllImport("user32.dll", SetLastError = true)]private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);public static void SetBrowserLocAndSize(string windowTitle, int x, int y, int width, int height){    var proc = Process.GetProcesses().FirstOrDefault(p => p.MainWindowTitle.Contains(windowTitle));    if(proc == null)    {        throw new NotFoundException($"Could not find a window containing a window title of '{windowTitle}'");    }    var hwnd = proc.MainWindowHandle;    // See https://www.pinvoke.net/default.aspx/user32.setwindowpos for information on this method    SetWindowPos(hwnd, new IntPtr(0), x, y, width, height, 0x0100 | 0x0400 | 0x0040);}