java - ChromeDriver 是否支持 Rotable for mobileEmulation

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

我正在使用 Selenium 2.44 和 ChromeDriver 2.13 测试网络应用。我正在使用 Chrome 移动模拟来模拟从移动设备进行的连接。我需要在测试中将屏幕方向从纵向更改为横向,但我似乎无法让它工作。

尝试将 WebDriver 增强为可旋转的示例代码如下,但在尝试转换为可旋转时抛出 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 不支持可旋转界面,有没有办法动态更新屏幕尺寸?

最佳答案

这是一个艰难的过程。希望这能为某人节省大量的精力。目前,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您可以找到适合您的分辨率的正确比率。在我的例子中,视口(viewport)的 1024x768 对应于窗口大小的 1039x859。

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

关于java - ChromeDriver 是否支持 Rotable for mobileEmulation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27851815/

相关文章:

delphi - 在 TestComplete 脚本中使用 TTabSet

java - 使用消息使用者时的 JMS 自动确认

java - java中如何删除字符串中的空值?

python - 选择同名的 2nd DIV CLASS - Python Selenium

python - 如何点击谷歌搜索结果中的下一个链接?

selenium - 使用页面对象模式执行操作的多种方式

java - JDBC:多列 IN 查询

Java泛型继承神秘错误

php - 选择 PHPUnit Selenium 2 测试用例中的所有匹配元素

javascript - 如何从Cypress工具中的元素中获取值?