【问题标题】:Does ChromeDriver support Rotatable for mobileEmulationChromeDriver 是否支持可旋转的 mobileEmulation
【发布时间】:2015-03-07 06:11:04
【问题描述】:

我正在使用 Selenium 2.44 和 ChromeDriver 2.13 测试一个网络应用程序。我正在使用 Chromes 移动仿真来模拟来自移动设备的连接。我需要将测试中的屏幕方向从纵向更改为横向,但我似乎无法正常工作。

下面是尝试将 WebDriver 扩充为 Rotatable 的示例代码,但在尝试转换为 Rotatable 时会抛出 ClassCastException。谁能告诉我我做错了什么或者 ChromeDriver 不支持轮换?

package test;

import org.openqa.selenium.Rotatable;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.AddRotatable;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        System.setProperty(
              "webdriver.chrome.driver",
              "Path/To/chromedriver.exe"
        );

        Map<String, String> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceName", "Google Nexus 4");

        Map<String, Object> chromeOptions = new HashMap<>();
        chromeOptions.put("mobileEmulation", mobileEmulation);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        capabilities.setCapability(CapabilityType.ROTATABLE, true);

        WebDriver driver = new ChromeDriver(capabilities);

        driver.get("http://m.rte.ie");

        // Try and rotate
        Augmenter augmenter = new Augmenter();
        augmenter.addDriverAugmentation(CapabilityType.ROTATABLE, new AddRotatable());
        WebDriver augmentedDriver = augmenter.augment(driver);
        ScreenOrientation currentOrientation = ((Rotatable) augmentedDriver).getOrientation();
        System.out.println(String.format("Current Orientation is: %s", currentOrientation));

        driver.quit();
    }
}

如果您查看“交换尺寸”部分,Chrome 似乎支持切换方向,如下所述:(https://developer.chrome.com/devtools/docs/device-mode)。我在浏览器中手动尝试过,效果很好。只是想知道 ChromeDriver 是否不支持 Rotatable 界面,有没有办法即时更新屏幕尺寸?

【问题讨论】:

  • 寻找相同的答案..

标签: java selenium-webdriver automated-tests google-chrome-devtools selenium-chromedriver


【解决方案1】:

这是一个艰难的过程。希望这可以为某人节省大量的精力。目前,ChromeDriver 仅支持移动仿真模式下的纵向。没有任何解决方法可以将其作为选项传递,也无法从 javascript 触发。

然而,有一个似乎对我有用的解决方案。您需要做的只是使用代表您设备的 userAgent 选项启动 ChromeDriver。例如,下面是 iPad 仿真的样子:

Map<String, Object> mobileEmulation = new HashMap<>();

String userAgent = "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
                "(KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10";

ChromeOptions options = new ChromeOptions();

options.addArguments("--user-agent=" + userAgent);
options.addArguments("window-size=1039,859");

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

此外,您可能希望设置类似于模拟设备的尺寸。这有点棘手,因为只能在 ChromeOptions 中设置窗口大小。同样,使用此工具:Chrome viewport dimensions plugin,您可以找到适合您分辨率的正确比率。在我的例子中,视口的 1024x768 对应于 1039x859 的窗口大小。

附:它只能在驱动程序启动时完成。无法即时更改方向

【讨论】:

  • 路过说声谢谢。我发现了一个已存档的 Chromium 错误。看起来在 2020 年仍然如此。
【解决方案2】:

它似乎接受“deviceOrientation”作为容量,其值为“portrait”和“landscape”,但不幸的是这还没有实现。

等待相同的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多