我正在测试一个仍在开发中的网站.
I am testing a website which is still in development.
通常一个元素的 id、类、文本或在 DOM 中的位置会改变.然后我一直使用的定位器将无法找到该元素.
Often an element's id, class, text, or position in the DOM will change. And then the locator I've been using will no longer be able to find the element.
但这些功能仍然正常运行.当没有实际回归时,我不希望多个测试失败.
But the features are still functioning properly. I don't want several tests to fail when there is no actual regression.
因此,我没有为每个元素使用一个定位器,而是有一个定位器集合.
So instead of having a single locator for each element, I have a collection of locators.
public static final ArrayList<By> LOGIN_ANCHOR_LOCATORS = new ArrayList<By>();
static {
LOGIN_ANCHOR_LOCATORS.add(By.id("loginLink"));
LOGIN_ANCHOR_LOCATORS.add(By.linkText("Login"));
LOGIN_ANCHOR_LOCATORS.add(By.xpath("/html/body/div[5]/a"));
}
我查找元素的方法如下:
My method for finding the element looks like this:
public WebElement locateElement(ArrayList<By> locators){
// create an element to return
WebElement element = null;
// until the desired element is found...
while (element == null){
// loop through the locators
for (By locator : locators){
// try to find by locator
element = customWait.until(ExpectedConditions.presenceOfElementLocated(locator));
// if not found...
if (element == null){
// log the failure
logFailingLocator(locator);
}
}
}
return element;
}
它试图找到集合中第一个定位器的元素,只有当它失败时,才尝试下一个定位器.
It tries to find the element with the first locator in the collection, and only if it fails, try the next locator.
集合是一个 ArrayList
(顺序由插入顺序定义),这意味着我的 for 循环
将按照它们添加到列表中的顺序尝试每个定位器.
The collection is an ArrayList
(order is defined by insertion order), which means my for loop
will try each locator in the order which they were added to the List.
我通过按特定顺序添加定位器来初始化上面的列表.id 是第一位的,因为我认为如果元素在 DOM 中的位置发生变化,但它保留了它的 id,那么这将是我最有可能找到正确元素的方式.Xpath 是最后一个,因为即使 id/class/text 发生了变化,但在 DOM 中该位置仍然存在相同类型的元素,它可能是正确的元素,但可能不如其他定位器那么确定.
I initialized the list above by adding the locators in a particular order. Id is first, because I figure if the element's position in the DOM changes, but it retains its id, then that will be the way I'll be most likely to locate the correct element. Xpath is last because even if the id/class/text changes, but there is still the same type of element in the DOM at that position, it's probably the right element, but maybe less certain than other locators would be.
我正在使用忽略 NoSuchElementException 的流畅等待:
I'm using a fluent wait which ignores NoSuchElementException:
// Wait 5 seconds for an element to be present on the page, checking
// for its presence once every quarter of a second.
Wait<WebDriver> customWait = new FluentWait<WebDriver>(driver)
.withTimeout(5L, TimeUnit.SECONDS)
.pollingEvery(250L, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
因此,当一个定位器失败时,它不会中断循环 - 它只是记录失败,然后继续尝试下一个定位器.
So when one locator fails, it won't break the loop - it just logs the failure and then still goes on to try the next locator.
如果所有定位器都失败了,那么元素将保持为空,测试将失败,原因很可能是特性/功能的实际回归.
If all the locators fail, then the element will remain null, the test will fail, and it's far more likely the reason is actual regression of features/functionality.
我会定期检查我的日志中是否存在具有 1 或 2 个失败定位器的任何元素,同时在我的 pageObject 中更新它们,同时测试继续顺利运行.
I periodically check my logs for any element with 1 or 2 failing locators, and update them in my pageObject in the meantime, while tests continue running smoothly.
以这种方式设置我的项目有什么优点或缺点?
What are pros or cons to setting up my project this way?
这是一种有趣的方法,但我担心您可能会掩盖其他问题.我更愿意与开发人员更密切地合作,以避免一开始就破坏 UI 问题.
It's an interesting approach, but I'm concerned you might be masking other issues. I'd prefer to work more closely with the developers to avoid breaking UI issues in the first place.
不断变化的 ID 是动态生成的吗?如果是这种情况,请查看是否无法在 ID 上获得后缀,例如 _loginlink.您可能还必须使用从附近的静态 ID 开始的 XPath://div[@id='login_link_container'/a".(从文档的根目录开始,如您的示例所示,这是一个痛苦的秘诀!:))
Are the changing IDs dynamically produced? If that's the case, look to see if you can't get a suffix on the IDs, something like _loginlink. You might also have to work with an XPath that starts from a nearby static ID: "//div[@id='login_link_container'/a". (Starting from the root of the document like your example shows is a recipe for pain! :) )
这篇关于在 Selenium 中为每个元素使用多个定位器的优缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!