现场:http://live.guru99.com/index.php/mobile.html 我想查看所有可用产品的价格,更准确地说:在图片下查看mobile.html上的平均价格,点击图片后查看产品详情。
哪种方式最适合做手术? 我刚开始检查第一个产品的价格
String price1 = driver.findElement(By.cssSelector(".price")).getText();
driver.findElement(By.cssSelector(".product-image")).click();
String price2 = driver.findElement(By.cssSelector(".price")).getText();
Assert.assertTrue(price1.equals(price2));
答案 0 :(得分:1)
driver.get("http://live.guru99.com/index.php/mobile.html");
List<WebElement> phones = driver.findElements(By.cssSelector(".products-grid > li"));
int size = phones.size();
for(int i=0;i<size;i++) {
WebElement phone = phones.get(i);
// read prices from list page
StringBuffer exp_prices = new StringBuffer();
// for some phone, there are two price, one is old, another is new price
List<WebElement> eles = phone.findElements(By.cssSelector(".price-box .price"));
for(WebElement ele:eles) {
exp_prices.append(ele.getText().trim() + ",");
}
// click phone image enter detail page
phone.findElement(By.cssSelector(".product-image")).click();
// read prices from detail page
StringBuffer act_prices = new StringBuffer();
List<WebElement> eles2 = driver.findElements(By.cssSelector(".product-view .price-info .price"));
for(WebElement ele:eles2) {
act_prices.append(ele.getText().trim() + ",");
}
// check prices
Assert.assertEquals(exp_prices.toString(), act_prices.toString());
// history back to detail page
driver.navigate().back();
// find phones on list page again to avoid `StateReferenceException`
phones = driver.findElements(By.cssSelector(".products-grid > li"));
}