如何听取测试中点击的内容?

时间:2016-10-07 11:34:35

标签: java selenium event-handling webdriver

我尝试记录点击事件,但

示例:

页面对象类中的

按钮:

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

点击测试

searchPage.btnClear().click();

Listner记录:

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"

是否可以实施? 我不需要log元素定位器,因为有时候它有不可读的名字,我需要为页面对象元素记录我自己的名字

感谢。

2 个答案:

答案 0 :(得分: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"));
}

答案 1 :(得分: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