我在我曾经构建的每一个框架中都放入了一些东西,这非常有效。这是从这里找到的框架的摘录。你可以在这里下载...
在对对象执行任何操作之前,我实现了一种伪等待类型的方法。自己试试。这是非常有效的。
这些是来自AutomationTest类的方法
/**
* Checks if the element is present or not.<br>
* @param by
* @return <i>this method is not meant to be used fluently.</i><br><br.
* Returns <code>true</code> if the element is present. and <code>false</code> if it's not.
*/
public boolean isPresent(By by) {
if (driver.findElements(by).size() > 0) return true;
return false;
}
/**
* Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
*/
private WebElement waitForElement(By by) {
int attempts = 0;
int size = driver.findElements(by).size();
while (size == 0) {
size = driver.findElements(by).size();
if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
by.toString(),
MAX_ATTEMPTS));
attempts++;
try {
Thread.sleep(1000); // sleep for 1 second.
} catch (Exception x) {
fail("Failed due to an exception during Thread.sleep!");
x.printStackTrace();
}
}
if (size > 0) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");
return driver.findElement(by);
}
自己结合这些方法,你就可以开始了。
还
在性能方面,我不能对你强调这一点。使用 CSS。它更快,更清洁。你自己看。
考虑以下,
<div id="something">
<div class="someClass">
<a href='http://google.com/search?'>Search Google</a>
</div>
</div>
让我们找到<a>
.
CSS:
div#something div.someClass > a[href^='http://google']
路径:
//div[@id='something']/div[contains(@class, 'someClass')]/a[starts-with(@href, 'http://google')]