有没有办法列出网页上的所有更改?通过更改我的意思是添加到页面的所有新html元素,删除的旧元素,某些元素的位置发生了变化?
我正在使用带有java的selenium web驱动程序。这是我的代码:
@Test(dataProvider = "deviceName")
public void mobileEmulation(String deviceName, String url){
String ChromeDriverPath = "C:\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", deviceName);
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
for ( int i = 0 ; i < 2 ; i ++){
driver.get(url);
String source=driver.getPageSource();
}
driver.quit();
}
我被困了,因为我不知道怎么做或者从哪里开始?
答案 0 :(得分:0)
听起来您正在尝试将默认页面的更改与页面的移动版本进行比较,看看有什么不同之处?如果是这种情况,则传递一段未作为参数为移动设备模拟的页面源,然后比较移动设备的页面源和默认页面。您可以使用类似Extract the difference between two strings in Java之类的内容打印出两个来源之间的差异。
更好的方法可能是这样的
public String getDefaultSource(String deviceName, String url){
String ChromeDriverPath = "C:\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
WebDriver driver = new ChromeDriver();
driver.get(url);
return driver.getPageSource();
}
public String getMobileSource(String deviceName, String url){
String ChromeDriverPath = "C:\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", deviceName);
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
driver.get(url);
return driver.getPageSource();
}
public void compareSource(String defSource, String mobileSource){
//You can used google's diff_match_patch library to get string differences
}
然后你只需得到每个来源并进行字符串比较,它应该显示两者之间的所有差异。