我正在用selenium编写自动化,需要跟踪所有内容,我发现 AbstractWebDriverEventListener 带有 EventFiringWebDriver 示例,按照手册但事件没有火。下面有一些工作示例可以看到或出现问题吗?
//+++++++++++++++++++++++++++++++++++++++++++++++++
package fooautoweb;
import foowebtools.EFooBrowserType;
import foowebtools.FooBrowser;
public class FooAutoWeb {
public static void main(String[] args) throws Exception {
FooBrowser bw = FooBrowser.getInstance(EFooBrowserType.FIREFOX);
if(bw.gotoUrl("https://www.google.com/ncr")){
System.out.println("equal!");
}
System.out.println(bw.getCurrentUrl());
bw.close();
bw = null;
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++
package foowebtools;
public enum EFooBrowserType {
FIREFOX
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++
package foowebtools;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
public class FooBrowserListener extends AbstractWebDriverEventListener {
//-------------------------------------------------
@Override
public void beforeNavigateTo(String url, WebDriver driver) { System.out.println("Before navigating to: '" + url + "'"); }
//-------------------------------------------------
@Override
public void afterNavigateTo(String url, WebDriver driver) { System.out.println("Navigated to:'" + url + "'"); }
//-------------------------------------------------
@Override
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
String msg = "Value of the:" + element.toString() + " before any changes made"
System.out.println(msg);
}
//-------------------------------------------------
@Override
public void afterChangeValueOf(WebElement element, WebDriver driver) { System.out.println("Element value changed to: " + element.toString());}
//-------------------------------------------------
@Override
public void beforeClickOn(WebElement element, WebDriver driver) { System.out.println("Trying to click on: " + element.toString()); }
//-------------------------------------------------
@Override
public void afterClickOn(WebElement element, WebDriver driver) { System.out.println("Clicked on: " + element.toString()); }
//-------------------------------------------------
@Override
public void beforeNavigateBack(WebDriver driver) { System.out.println("Navigating back to previous page"); }
//-------------------------------------------------
@Override
public void afterNavigateBack(WebDriver driver) { System.out.println("Navigated back to previous page"); }
//-------------------------------------------------
@Override
public void beforeNavigateForward(WebDriver driver) { System.out.println("Navigating forward to next page"); }
//-------------------------------------------------
@Override
public void afterNavigateForward(WebDriver driver) { System.out.println("Navigated forward to next page"); }
//-------------------------------------------------
@Override
public void onException(Throwable error, WebDriver driver) { System.out.println("Exception occured: " + error); }
//-------------------------------------------------
@Override
public void beforeFindBy(By by, WebElement element, WebDriver driver) { System.out.println("Trying to find Element By : " + by.toString()); }
//-------------------------------------------------
@Override
public void afterFindBy(By by, WebElement element, WebDriver driver) { System.out.println("Found Element By : " + by.toString()); }
//-------------------------------------------------
@Override
public void beforeScript(String script, WebDriver driver) { /* non overridden methods of WebListener class */ }
//-------------------------------------------------
@Override
public void afterScript(String script, WebDriver driver) { /* non overridden methods of WebListener class */ }
//-------------------------------------------------
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++
package foowebtools;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class FooBrowser implements AutoCloseable {
//-------------------------------------------------
private WebDriver _browser;
private FooBrowserListener _bevtlistener;
private EventFiringWebDriver _bevtfirer;
//-------------------------------------------------
public static FooBrowser getInstance(EFooBrowserType eFooBrowserType) {
FooBrowser bwi = new FooBrowser();
bwi._browser = new FirefoxDriver();
bwi._bevtfirer = new EventFiringWebDriver(bwi._browser);
bwi._bevtlistener = new FooBrowserListener();
bwi._bevtfirer.register(bwi._bevtlistener);
return bwi;
}
//-------------------------------------------------
public boolean gotoUrl(String url) {
boolean reached;
this._browser.get(url);
reached = (url.equals(this.getCurrentUrl()));
return reached;
}
//-------------------------------------------------
public String getCurrentUrl() {
return this._browser.getCurrentUrl();
}
//-------------------------------------------------
@Override
public void close() throws Exception {
if (this._browser != null) {
this._browser.close();
this._browser = null;
}
}
//-------------------------------------------------
}
//+++++++++++++++++++++++++++++++++++++++++++++++++
答案 0 :(得分:0)
只需在 this._browser.get(url); 行和事件被触发时使用 this._bevtfirer 而不是 this._browser 。只需使用 this._bevtfirer.get(url);