定位元素selenium + java的最快方法 [英] fastest way to locate elements selenium+java

查看:355
本文介绍了定位元素selenium + java的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试过程中,我需要检查我的网站上是否存在无效的工具提示和其他错误消息,以确保一切正常。问题是硒似乎要花很长时间才能找到这些元素,并且它们被设置在计时器上,因此它们有时有时间在硒找到它们之前消失,我们正在谈论一个3秒的计时器。



现在我发现它们是这样的:

  List< WebElement>错误= driver.findElements(By.cssSelector( div.alert .error)); 
if(error.size()== 0){返回true;}
else {返回false;}

这将找到给定时间在网站上显示的所有错误消息,有时这会很慢。工具提示是使用javascript创建和销毁的,因此不会隐藏然后显示。对此有任何想法吗?



编辑:



由于该网站是单个页面,因此需要大量元素经过寻找合适的商品,虽然当我寻找实际可见的商品时它很快,所以为了最大程度地减少我的搜索,我这样做是:

  WebElement消息= driver.findElement(By.id(-div-the-message-is-created-in))); 
List< WebElement>错误= messages.findElements(By.cssSelector( div.alert .error)));
if(error.size()== 0){返回true;}
else {返回false;}

虽然似乎没有更快地验证元素的存在。

解决方案

在我建立的每个框架中都放了一些东西,这非常有效。.这是从此处您可以在此处下载 ...



在对对象执行任何操作之前,我先实现了一种伪等待类型的方法。自己尝试。



这些是 AutomationTest

  / ** 
*检查元素是否存在。< br>
* @param by
* @return< i>该方法并不意味着可以流畅使用。
*返回< code> true< / code>如果该元素存在。和< code> false< / code>如果不是。
* /
public boolean isPresent(by by){
if(driver.findElements(by).size()> 0)返回true;
返回false;
}

/ **
*充当隐式超时的仲裁器的私有方法。有点像Wait For Ajax方法。
* /
private WebElement waitForElement(by by){
int次尝试= 0;
int size = driver.findElements(by).size();

而(size == 0){
size = driver.findElements(by).size();
if(attempts == MAX_ATTEMPTS)fail(String.format(%d秒后找不到%s,
by.toString(),
MAX_ATTEMPTS));
次尝试++;
try {
Thread.sleep(1000); //睡眠1秒钟。
} catch(Exception x){
fail(由于Thread.sleep期间发生异常而失败!);
x.printStackTrace();
}
}

if(size> 0)System.err.println( WARN:大于1 + by.toString()+的!);

返回driver.findElement(by);
}

自行组合这些方法,您就可以开始使用了。



ALSO



在性能方面,我不能向您这么强调。.使用CSS 。它更快,更干净。自己看看。



请考虑以下内容,

  

< div class = someClass>
< a href =’http://google.com/search?’>搜索Google< / a>
< / div>
< / div>

让我们找到< a>



CSS:

  div#something div.someClass> a [href ^ ='http:// google'] 

XPATH:

  // div [@ id ='something'] / div [contains(@class,'someClass')] / a [starts-with(@ href,'http:// google')] 


I need to check my site for invalid tooltips and other error mesages on my site during the tests to make sure everything is okey. the problem is that selenium seem to take forever to find these elements and they are set on a timer so they sometimes have time to disapere before selenium finds them, we are talking about a 3 second timer.

right now I find them like so:

      List<WebElement> error = driver.findElements(By.cssSelector("div.alert .error"));
        if (error.size() == 0) {return true;}
        else {return false;}

this will find all the error messages that are shown on the site at the given time and this is sometimes to slow to keep up. the tooltips are created and destroyd with javascript so they are not hidden and then displayed. Any thoughts on this?

EDIT:

Since the site is single page it's a massive amount of elements to go through to find the right one, altho it's fast when I look for a item that is actually vissable so to minimize my search I did like this:

WebElement messages = driver.findElement(By.id("the-div-the-message-is-created-in"));
List<WebElement> error = messages.findElements(By.cssSelector("div.alert .error"));
if (error.size() == 0) {return true;}
else {return false;}

altho it didn't seem to verify the existence of the element it any faster.

解决方案

Something that I put in every single framework i've ever built, which is VERY efficient.. Here is an exerpt from the framework found here. You can download it here...

I implement sort of a pseudo-wait type method before i ever perform any actions on objects. Try it yourself. It's very efficient.

These are methods from the AutomationTest class

    /**
     * Checks if the element is present or not.<br>
     * @param by
     * @return <i>this method is not meant to be used fluently.</i><br><br.
     * Returns <code>true</code> if the element is present. and <code>false</code> if it's not.
     */
    public boolean isPresent(By by) {
        if (driver.findElements(by).size() > 0) return true;
        return false;
    }

    /**
     * Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
     */
    private WebElement waitForElement(By by) {
        int attempts = 0;
        int size = driver.findElements(by).size();

        while (size == 0) {
            size = driver.findElements(by).size();
            if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
                                                             by.toString(),
                                                             MAX_ATTEMPTS));
            attempts++;
            try {
                Thread.sleep(1000); // sleep for 1 second.
            } catch (Exception x) {
                fail("Failed due to an exception during Thread.sleep!");
                x.printStackTrace();
            }
        }

        if (size > 0) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

        return driver.findElement(by);
    }

Combine these methods yourself, and you're good to go.

ALSO

In terms of performance, I cannot stress to you this enough.. USE CSS. It is faster, and cleaner. See for yourself.

Consider the following,

<div id="something">
  <div class="someClass">
    <a href='http://google.com/search?'>Search Google</a>
  </div>
</div>

Let's find the <a>.

CSS:

div#something div.someClass > a[href^='http://google']

XPATH:

//div[@id='something']/div[contains(@class, 'someClass')]/a[starts-with(@href, 'http://google')]

这篇关于定位元素selenium + java的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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