增加AWS实例中selenium测试的屏幕分辨率

时间:2017-12-20 12:13:09

标签: selenium amazon-ec2 bamboo

我正在用竹子进行硒试验。在竹子,我使用弹性剂,这是一个aws实例 - Windows 2012。默认情况下,代理的屏幕分辨率为1024x768。我只是想提高屏幕分辨率。

是否可以在创建ami或启动代理后增加屏幕分辨率?

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

对于这种确切的情况,我可以提出一种解决方法-

问题在于,除非您使用RDP进行连接,否则AWS EC2实例将使用默认基本图形适配器,其最大分辨率为1024x768。 尝试通过Selenium设置浏览器大小将有效,因为浏览器窗口将自动调整为最大桌面分辨率大小。

但是,您可以执行以下操作: 在测试运行期间打开浏览器后,您可以调用非托管代码,并通过user32.dll中的方法设置浏览器窗口的大小,并带有一个标志,该标志可以阻止Windows自动将浏览器窗口的大小调整为最大桌面分辨率大小。 / p>

代码如下(部分取自pinvoke.net)。这是一个C#示例,但可以重新设计成其他类型的解决方案,以从您的框架中调用。

在您的框架中某个类的某个类中,放置以下内容,然后在您的代码中实例化您的浏览器窗口,只需调用SetBrowserLocAndSize方法即可:

// 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);
}