2010-08-19 3 views
3

Y a-t-il une différence majeure dans le temps nécessaire pour évaluer un chemin x long plutôt qu'un chemin x court?
Ex. Y at-il une différence de performance entre
/div[@id = 'id1']/label[contains(text(), 'Hello')/../../descendant::input
et
//inputWebdriver Xpath Performance

Qu'en est-il la différence entre l'utilisation By.id("id1")
et
By.Xpath("//*[@id='id1']")

Répondre

13

Je suis heureux que vous ayez demandé, j'ai trouvé les réponses surprenantes .

  • court XPath est plus rapide que longue XPath, mais pas par beaucoup
  • sur Firefox recherche par nom est plus rapide que long XPath, mais une chaleur morte avec court XPath (parfois plus rapide)
  • Sur Internet Explorer, par .name est beaucoup plus lent que XPath

Cela semble voler face à la direction Simon Stewart a donné des re: la performance XPath IE, donc je le prendre avec un grain de sel, mais dans le code ci-dessous, c'est assez cohérent.

J'ai écrit un test rapide qui illustre cela. Il recherche la boîte de recherche sur 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(); 
} 
} 

Cela donne la sortie:

l'aide du pilote Firefox.
La recherche XPath longue a pris 0.13667022 secondes.
La recherche XPath courte a pris 0,024628577 secondes.
La recherche By.name a pris 0,025209911 secondes.

Utilisation du pilote Internet Explorer.
La recherche XPath longue a pris 0.196125248 secondes.
La recherche XPath courte a pris 0,164044262 secondes.
La recherche By.name a duré 1,005109964 secondes.

Questions connexes