当前位置 : 主页 > 网络安全 > 测试自动化 >

Webdriver Xpath性能

来源:互联网 收集:自由互联 发布时间:2021-06-22
评估长xpath而不是短xpath所需的时间有很大差异吗? 防爆. 两者之间是否存在性能差异? / div [@id =’id1′] / label [contains(text(),’Hello’)/../../ descendant :: input 和 //输入 使用之间有什么区别
评估长xpath而不是短xpath所需的时间有很大差异吗?
防爆.
两者之间是否存在性能差异?
/ div [@id =’id1′] / label [contains(text(),’Hello’)/../../ descendant :: input

//输入

使用之间有什么区别呢
By.id( “ID1”)

By.Xpath( “// * [@ id中= ‘ID1’]”)

我很高兴你问,我发现答案令人惊讶.

>短xpath比长xpath快,但不是很多
>在Firefox上按名称搜索比长xpath更快但是xpath短(有时更快)
>在Internet Explorer上,By.name比xpath慢得多

这似乎是面对Simon Stewart给出的指导:IE的xpath性能,所以我会带着一点点盐,但在下面的代码中,它非常一致.

我写了一个快速测试来说明这一点.它会在Google上搜索搜索框

package com.PeterNewhook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class FooTest {

public static void main(String[] args) {
    long start;
    long end;
    WebDriver driver;
    String longXpath = "/html/body/span[@id='main']/center/span[@id='body']/center/form/table/tbody/tr/td[2]/div[@class='ds']/input[@name='q']";
    String shortXpath = "//input[@name='q']";
    String elementId = "q";

    System.out.println("Using Firefox driver.");
    driver = new FirefoxDriver();
    driver.get("http://google.com");
    start = System.nanoTime();
    driver.findElement(By.xpath(longXpath));
    end = System.nanoTime()-start;
    System.out.println("The long XPath lookup took " + (double)end/1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.xpath(shortXpath));
    end = System.nanoTime() - start;
    System.out.println("The short XPath lookup took " + (double)end / 1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.name(elementId));
    end = System.nanoTime() - start;
    System.out.println("The By.name lookup took " + (double)end / 1000000000.0 + " seconds.");
    driver.close();

    System.out.println("\nUsing Internet Explorer driver.");        
    driver = new InternetExplorerDriver();
    driver.get("http://google.com");
    start = System.nanoTime();
    driver.findElement(By.xpath(longXpath));
    end = System.nanoTime()-start;
    System.out.println("The long XPath lookup took " + (double)end/1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.xpath(shortXpath));
    end = System.nanoTime() - start;
    System.out.println("The short XPath lookup took " + (double)end / 1000000000.0 + " seconds.");
    start = System.nanoTime();
    driver.findElement(By.name(elementId));
    end = System.nanoTime() - start;
    System.out.println("The By.name lookup took " + (double)end / 1000000000.0 + " seconds.");
    driver.close();
}
}

这给出了输出:

使用Firefox驱动程序.
长XPath查找耗时0.13667022秒.
短XPath查找花了0.024628577秒.
By.name查找花了0.025209911秒.

使用Internet Explorer驱动程序.漫长的XPath查找耗时0.196125248秒.短XPath查找耗时0.164044262秒.By.name查找耗时1.005109964秒.

网友评论