2016-10-07 119 views
0

我嘗試登錄點擊事件,但如何聆聽測試中點擊的內容?

例子:

按鈕,在頁面對象類:

public WebElement btnClear() { 
    return driver.findElement(By.name("filter:P_ClearButtonDFID")); 
} 

點擊測試

searchPage.btnClear().click(); 

聽者記錄:

Clicked on: [[InternetExplorerDriver: internet explorer on WINDOWS (1cf610ba-607c-40a2-94bc-745f1e564bde)] -> name: filter:P_ClearButtonDFID] 

點擊監聽器:

public void afterClickOn(WebElement element, WebDriver driver) {    
    System.out.println("clicked on: " + element.toString()); 
} 

,因爲我想:

"Clicked: btnClear()" or "Clicked: btnClear" 

是否有可能實現? 我不需要日誌元素定位器,因爲有些時候它有不可讀的名稱,我需要記錄我自己的頁面對象元素名稱

謝謝。

回答

0

試試這個代碼(考慮您正在使用WebDriverEventListner接口)

public void afterClickOn(WebElement element, WebDriver driver) {    
    System.out.println("clicked on: " + element.getAttribute("name")); 
} 

OR

public void afterClickOn(WebElement element, WebDriver driver) {    
    System.out.println("clicked on: " + element.getAttribute("id")); 
} 
+0

正如我所說我不需要日誌LOCATOR或它的屬性。 我需要頁面對象元素的日誌名稱 btnClear –

+0

嘗試反射像element.getClass(); –

0

我的意思:

import org.openqa.selenium.WebElement; 
import org.openqa.selenium.remote.RemoteWebElement; 
import org.apache.log4j.Logger; 
import com.thoughtworks.selenium.webdriven.commands.IsElementPresent; 

public class WebElem extends RemoteWebElement { 
    private WebElement element; 
    private String  calleeMethod; 
    private String  tascase; 
    private Logger  logger; 

    public WebElem(WebElement elem) { 
     element = elem; 
     Throwable t = new Throwable(); 
     StackTraceElement[] elements = t.getStackTrace(); 
     calleeMethod = elements[1].getMethodName(); 
     tascase = elements[2].getClassName(); 

    } 

    @Override 
    public void click() { 
     logger = Logger.getLogger(tascase); 
     if (IsElementPresent() == true) { 
      if (element.isEnabled() == false) { 
       logger.info("element " + calleeMethod + " is disabled."); 
      } else { 
       element.click(); 
       logger.info("clicked: " + calleeMethod); 
      } 
     } else { 
      logger.error("element " + calleeMethod + " is not found."); 
     } 

    } 

    public void sendKeys(String string) { 
     logger = Logger.getLogger(tascase); 
     if (IsElementPresent() == true) { 
     } 
     if (element.isEnabled() == false) { 
      logger.error("element " + calleeMethod + " is disabled."); 
     } else { 
      element.sendKeys(string); 
      logger.info("Sending keys: " + string + " to: " + calleeMethod); 
     } 
    } 

    public void clear() { 
     element.clear(); 
    } 

    private boolean IsElementPresent() { 
     if (element != null) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

} 

,並報告了需要的值:

Sending keys: 200006862315 to: fldAccountIdFrom 
clicked: btnInvoice 
clicked: btnResource