优点/缺点使用每个元素的多个定位器中的硒? [英] Pros/cons for using multiple locators per element in Selenium?

查看:163
本文介绍了优点/缺点使用每个元素的多个定位器中的硒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试网站仍处于发展。

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 (顺序是由插入顺序定义),这意味着我的循环将尝试它们被添加到列表中的顺序每个定位器。

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 /班/文本的变化,但仍有同一类型在该位置的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?

推荐答案

这是一个有趣的方法,但我担心你可能会掩盖其他问题。我想preFER与开发者更紧密地合作,以避免在首位打破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! :) )

这篇关于优点/缺点使用每个元素的多个定位器中的硒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆