使用Selenium Java在窗口屏幕中拟合对象 [英] Fitting objects in window screen using Selenium Java
问题描述
我正在尝试适合窗口(Chrome浏览器)中的所有对象.手动执行操作(我使用的尺寸是使用切换栏的1024X786,但是当我使用相同的尺寸进行自动化操作时,它显示的效果与手动显示的效果不同.
I am trying to fit all the objects in the window (Chrome browser). Manually when I am doing it(the dimensions that I am using is 1024X786 using toggle bar, but when I am automating it using the same dimension, it's not showing the same as it was manually.
以下是代码:
driver = new ChromeDriver();
Map<String, Object> deviceMetrics = new HashMap<String, Object>();
deviceMetrics.put("width", 768);
deviceMetrics.put("height", 1024);
Map<String, Object> mobileEmulation = new HashMap<String, Object>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mobile S");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
System.out.println("Driver is now ChromeDriver");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
页脚在这里迷路了.我得到的是:
The footer is getting lost in this. What I am getting is:
我想得到的是:
我使用的另一个代码是:
Another code I used is:
driver.manage().window().setSize(new Dimension(1024, 786));
任何人都可以帮忙吗?谢谢
Can anyone please help? Thanks
推荐答案
您需要在ChromeOptions
Class实例中传递与mobileEmulation
相关的配置,如下所示:
You need to pass the mobileEmulation
related configuration within an instance of ChromeOptions
Class as follows:
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 768);
deviceMetrics.put("height", 1024);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
注意:使用移动设备仿真在桌面上测试移动网站可能会很有用,但是测试人员应注意,存在许多细微的差异,例如:
Note: Testing a mobile website on a desktop using mobile emulation can be useful, but testers should be aware that there are many subtle differences such as:
- 完全不同的GPU,可能会导致较大的性能变化;
- 未模拟移动用户界面(尤其是隐藏的网址栏会影响页面高度);
- 不支持消除歧义弹出窗口(在其中选择一些触摸目标之一);
- 许多硬件API(例如,directionchange事件)不可用.
移动仿真受已知问题的影响测试适用于所有平台的Chromium上来自MobileEmulationCapabilityTest类的testClickElement失败
Mobile Emulation is subject to a known issue Test testClickElement from MobileEmulationCapabilityTest class fails on Chromium for all platforms
这篇关于使用Selenium Java在窗口屏幕中拟合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!