【发布时间】: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