java @CacheLookup 如何在 WebDriver 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6215857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does @CacheLookup work in WebDriver?
提问by lisak
I'm not sure that I understand the caching principle :
我不确定我是否理解缓存原理:
@CacheLookup
@FindBy(how = How.ID, using = namespace + signifLvl)
private WebElement sigLvl;
If we use this Annotation way, ElementLocator is being used and the first time one refer to the field, the element is found driver.findElement(by)
and cached via ElementLocator, so that next time we refer to it, it is returned from the cache.
如果我们使用这种Annotation方式,则使用ElementLocator,第一次引用字段时,driver.findElement(by)
通过ElementLocator找到并缓存元素,以便下次引用时从缓存中返回。
It looks it depends on the lifetime of the ElementLocator & PageObject instance.
看起来它取决于 ElementLocator & PageObject 实例的生命周期。
Also it doesn't relate to direct driver.findElement(By);
calls.
它也与直接driver.findElement(By);
调用无关。
I'm assuming, that WebElement is like a pointer/reference to the element, right ? So that if the element changes in browser, it is reflected to the WebElement right away. As it is in JavaScript. Because all RemoteWebElement's methods regarding element's state are executing command/request to browser.
我假设 WebElement 就像一个指向元素的指针/引用,对吧?这样,如果元素在浏览器中发生变化,它会立即反映到 WebElement。就像在 JavaScript 中一样。因为所有有关元素状态的 RemoteWebElement 方法都在向浏览器执行命令/请求。
So that the changes are reflected even in the cached element, right ?
这样更改甚至会反映在缓存元素中,对吗?
采纳答案by lisak
Imho the question should rather be : What is the element pointer/id is about ?
恕我直言,问题应该是:元素指针/id 是关于什么的?
As WebElement doesn't have a state, only methods that call browser. @CacheLookup is only a shortcut for public WebElement el = driver.findElement(By);
when initializing WebDriver's PageObject, for instance.
由于 WebElement 没有状态,只有调用浏览器的方法。例如,@CacheLookup 只是public WebElement el = driver.findElement(By);
初始化 WebDriver 的 PageObject 时的快捷方式。
After you have the instance, you are executing its methods, that call browser.
拥有实例后,您正在执行它的方法,即调用浏览器。
The WebElement ID corresponds to a JS element instance. if you go like this on client JS :
WebElement ID 对应一个 JS 元素实例。如果你在客户端 JS 上这样做:
var node1 = document.createElement('a');
and then append it somewhere, remove it from there, append it some place else, etc. and it is still the same node1 instance, the WebElement instance still points to the node1 element, because it is the same JS node instance.
然后将它附加到某个地方,从那里删除它,将它附加到其他地方等等,它仍然是同一个 node1 实例,WebElement 实例仍然指向 node1 元素,因为它是同一个 JS 节点实例。
回答by Khyati Sehgal
Page Factory works on the principle of configuring the proxies when Page Factory is initialized and every time you use a WebElement it will go and search for the element.
页面工厂的工作原理是在页面工厂初始化时配置代理,每次使用 WebElement 时,它都会去搜索元素。
Now what cachelookupdoes is it stores elements having @cachelookup annotation applied over it and then stores this element for further reference/s. For example:
现在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();
} }
What this annotation does is it stores the value the searchBox element and now there is no need to search for this element on webpage again.
该注解的作用是存储 searchBox 元素的值,现在无需再次在网页上搜索该元素。
回答by virusrocks
I know this answer is late in the party. I have been trying to understand @Cachelookup myself and came up with certain tests and conclusions. This topic is hard to explain in short here. So please take a look at the article which tries to understand the inner workings of @CacheLookup and also the performance improvements that we get by using this annotation. Article is on ToolsQA here
我知道这个答案在聚会中迟到了。我一直试图自己了解 @Cachelookup 并提出了某些测试和结论。这个话题在这里很难简单解释。因此,请查看这篇文章,该文章试图了解 @CacheLookup 的内部工作原理以及我们通过使用此注释获得的性能改进。文章是关于ToolsQA 在这里