@CacheLookup如何在WebDriver中工作?

我不确定我是否理解缓存原则:

@CacheLookup @FindBy(how = How.ID, using = namespace + signifLvl) private WebElement sigLvl; 

如果我们使用这种Annotation方法,正在使用ElementLocator,并且第一次引用该字段时,元素被找到driver.findElement(by)并通过ElementLocator缓存,以便下次我们引用它时,它将从缓存。

它看起来取决于ElementLocator和PageObject实例的生命周期。

它也与direct driver.findElement(By); 调用。

我假设,WebElement就像是元素的指针/引用,对吧? 因此,如果元素在浏览器中发生更改,则会立即反映到WebElement。 就像在JavaScript中一样。 因为所有RemoteWebElement关于元素状态的方法都是对浏览器执行命令/请求。

这样即使在缓存元素中也会反映出这些变化,对吧?

页面工厂的工作原理是在初始化页面工厂时配置代理,每次使用WebElement时,它都会搜索元素。

现在cachelookup的作用是存储在其上应用了@cachelookup注释的元素,然后存储该元素以供进一步参考。 例如

  public class SearchPage { // The element is now looked up using the name attribute, // and we never look it up once it has been used the first time @FindBy(how = How.NAME, using = "q") @CacheLookup private WebElement searchBox; public void searchFor(String text) { // We continue using the element just as before searchBox.sendKeys(text); searchBox.submit(); } } 

这个注释的作用是存储searchBox元素的值,现在不需要再次在网页上搜索这个元素。

Imho问题应该是:元素指针/ id是什么?

由于WebElement没有状态,只有调用浏览器的方法。 @CacheLookup只是public WebElement el = driver.findElement(By);的快捷方式public WebElement el = driver.findElement(By); 例如,在初始化WebDriver的PageObject时。

获得实例后,您正在执行调用浏览器的方法。

WebElement ID对应于JS元素实例。 如果你在客户端JS上这样:

 var node1 = document.createElement('a'); 

然后将它附加到某处,从那里删除它,将它附加到其他地方等等。它仍然是相同的node1实例,WebElement实例仍然指向node1元素,因为它是相同的JS节点实例。

我知道这个答案在晚会上已经很晚了。 我一直在努力了解@Cachelookup,并提出了一些测试和结论。 这个主题在这里很难解释。 因此,请查看试图了解@CacheLookup内部工作原理的文章,以及使用此注释获得的性能改进。 文章在这里是ToolsQA