@CacheLookup如何在WebDriver中工作?

lis*_*sak 6 java caching webdriver selenium-webdriver

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

@CacheLookup
@FindBy(how = How.ID, using = namespace + signifLvl)
private WebElement sigLvl;
Run Code Online (Sandbox Code Playgroud)

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

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

它也与直接driver.findElement(By);呼叫无关.

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

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

小智 10

页面工厂的工作原理是在初始化页面工厂时配置代理,每次使用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();
   } }
Run Code Online (Sandbox Code Playgroud)

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


lis*_*sak 3

恕我直言,问题应该是:元素指针/id 是关于什么的?

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

获得实例后,您将执行它的方法,即调用 browser.

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

var node1 = document.createElement('a');
Run Code Online (Sandbox Code Playgroud)

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