有人可以帮忙吗!
在 WebDriver 中执行测试期间,如何突出显示以下类中的所有 Web 元素?使用 Selenium RC,它非常简单,但使用 WebDriver 时我很挣扎。
如果有人可以向我提供一些我可以尝试的代码,我将不胜感激,还有这些代码适合下面的类的什么地方——抱歉,我的 Java 技能不是那么好。
package hisScripts;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import org.testng.Assert;
import static org.testng.Assert.fail;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
public class R_LHAS_Only_Account_Verification extends HIS_Login_Logout{
public WebDriver driver;
public String baseUrl;
public int exeMonth;
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass
@Parameters ({"browser1", "url", "executionMonth"})
public void setUp(String browser1, String url, int executionMonth) throws Exception {
exeMonth = executionMonth;
baseUrl = url;
if (browser1.equals("FF")) {
driver = new FirefoxDriver();
} else if (browser1.equals("IE")){
driver = new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void R_LHAS_Reports() throws Exception {
R_LHAS_Only_Login(baseUrl, driver);
Assert.assertEquals("Kingston upon Thames (RB)", driver.findElement(By.xpath("//html/body/div[9]/div/div[3]/div/div/div")).getText());
Assert.assertEquals("Average price", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr/td")).getText());
Assert.assertEquals("% price change", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[2]/td")).getText());
Assert.assertEquals("Lower quartile price", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[3]/td")).getText());
Assert.assertEquals("Time to sell (weeks)", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[4]/td")).getText());
Assert.assertEquals("% asking price achieved", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[5]/td")).getText());
Assert.assertEquals("House price to earnings ratio", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[6]/td")).getText());
Assert.assertEquals("Cost of buying outright - LQ 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[7]/td")).getText());
Assert.assertEquals("Private rent 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[8]/td")).getText());
Assert.assertEquals("80% private rent 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[9]/td")).getText());
Assert.assertEquals("Social rent 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[10]/td")).getText());
R_LHAS_Only_Logout(baseUrl,driver);
}
@AfterClass(alwaysRun=true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (! "".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
原文由 user929258 发布,翻译遵循 CC BY-SA 4.0 许可协议
在
WebDriver
(自 v2.21.0 起)中无法执行此操作。您可以尝试将常用的findElement(By)
方法替换为经过调整的方法,该方法使用 JavaScript 来突出显示找到的元素:现在您明白了,有一个改进版本可以在找到并突出显示新元素时恢复最后一个元素的原始
border
:还有剧本!我使用
FileUtils.readFileToString()
从文件加载它们。SCRIPT_GET_ELEMENT_BORDER( 取自该站点的 IE 友好版本),如果它通过更改背景颜色(例如,仅底部边框)使用突出显示,它会更短。但这是最好的 :)。
SCRIPT_UNHIGHLIGHT_ELEMENT
欢迎任何问题、注释、请求和改进!