簡體   English   中英

擴展Webdriver或Selenium2的click()方法以計算網頁響應

[英]Extend click() method of Webdriver or Selenium2 to calculate web page response

我打算使用以下代碼計算頁面響應時間,但這會導致冗余。

  long start;
  start = System.currentTimeMillis();  
  driver.get(url); or click();
  System.out.println (driver.getTitle() + " - " + (System.currentTimeMillis() - start) + " MilliSec"); 

我知道使用Page對象實現上面的代碼,需要幫助擴展使用Webdriver接口的click(),這樣當我調用click()時,代碼會自動執行。

正如我讀過你的評論,還有一種可能性。 構建自己的類,實現WebElement接口。 在這種情況下,您將必須實現WebElement的所有方法,這可能是不值得的。 但是,如果你想采用這種方法,我會建議這樣的事情:

public class MyWebElement implements WebElement{
  private WebElement element

  //... all the WebElement methods must be implemented
  //but I will show only the click:

  @Override
     public void click(){
     long start;
     start = System.currentTimeMillis();  
     element.click();
     System.out.println (driver.getTitle() + " - " + (System.currentTimeMillis() - start) + " MilliSec");

  }
}

后來在代碼中:

@Test
public void testSomething(){
  driver.get("http://foo.bar");
  MyWebElement elem = driver.find(By.id("baz"));
  elem.click();
}

再次說明:通過使用這種方法,您必須實現所有 WebElement方法 - 即使是您在測試中永遠不會使用的方法。 在我的位置,我會使用這個方法,因為我在mz之前的回答中暗示。

我實現它的方式是我編寫自己的Java類,它應該代表頁面。 所以如果我想測試Google,我有一些看起來像這樣的類:

public class GoogleUI{
   private WebElement searchButton;
   private WebDriver driver;

   public GoogleUI(){
     driver = new FirefoxDriver();
   }

  public void clickSearch(){
   long start;
   searchButton = driver.findElement(By.name("btnG"));
   start = System.currentTimeMillis();  
   searchButton.click();
   System.out.println (driver.getTitle() + " - " + (System.currentTimeMillis() - start) + " MilliSec"); 

  }
}

后來在測試中:

@Test
public void testGoogleSearchButton(){
   GoogleUI google = new GoogleUI();
   google.clickSearch();
}

clickSearch方法本身包裝單擊並打印時間。

這顯然是最低限度的,但我希望你能在測試中適應它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM