为什么这段代码不处理来自浏览器的事件?

时间:2017-08-13 14:37:52

标签: java selenium

我的代码可以打开浏览器窗口并浏览到URL。我的代码中没有捕获任何错误。我需要处理浏览器生成的事件,特别是当用户关闭浏览器窗口时。我的OverrideClass中没有捕获任何消息。

我有这个:

try {
    System.setProperty("webdriver.gecko.driver", "./geckodriver.exe");
    driver = new FirefoxDriver();

    EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
    OverrideClass oc = new OverrideClass();
    eventDriver.register(oc);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (Exception ex) {
    Log.logError("Browser.initBrowser(): " + ex.getLocalizedMessage());
}

消息处理程序如下所示:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class OverrideClass implements WebDriverEventListener{
//  @Override
//  public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
//
//  }
    @Override
    public void afterClickOn(WebElement arg0, WebDriver arg1) {
        System.out.println("OverrideClass.afterClickOn(): " + arg0.toString());
    }
    @Override
    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        System.out.println("OverrideClass.afterFindBy(): " + arg0.toString());  }
    @Override
    public void afterNavigateBack(WebDriver driver) {
        System.out.println("After clicking back  "+ driver.getCurrentUrl());
    }
    @Override
    public void afterNavigateForward(WebDriver arg0) {

    }
    @Override
    public void afterNavigateTo(String arg0, WebDriver arg1) {

    }
    @Override
    public void afterScript(String arg0, WebDriver arg1) {

    }
//  @Override
//  public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
//
//  }
    @Override
    public void beforeClickOn(WebElement arg0, WebDriver arg1) {

    }
    @Override
    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {

    }
    @Override
    public void beforeNavigateBack(WebDriver driver) {
        System.out.println("OverrideClass.beforeNavigateBack() Before clicking back: " + driver.getCurrentUrl());
    }
    @Override
    public void beforeNavigateForward(WebDriver arg0) {
        System.out.println("OverrideClass.beforeNavigateForward() Before clicking back: " + arg0.getCurrentUrl());
    }
    @Override
    public void beforeNavigateTo(String arg0, WebDriver arg1) {
        System.out.println("OverrideClass.beforeNavigateTo(): " + arg0);
    }
    @Override
    public void beforeScript(String arg0, WebDriver arg1) {

    }
    @Override
    public void onException(Throwable arg0, WebDriver arg1) {
        System.out.println("OverrideClass.onException(): " + arg0.getLocalizedMessage());
    }
    @Override
    public void afterAlertAccept(WebDriver arg0) {


    }
    @Override
    public void afterAlertDismiss(WebDriver arg0) {


    }
    @Override
    public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {


    }
    @Override
    public void afterNavigateRefresh(WebDriver arg0) {


    }
    @Override
    public void beforeAlertAccept(WebDriver arg0) {


    }
    @Override
    public void beforeAlertDismiss(WebDriver arg0) {


    }
    @Override
    public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {


    }
    @Override
    public void beforeNavigateRefresh(WebDriver arg0) {


    }
}

2 个答案:

答案 0 :(得分:0)

因此selenium使用一系列模拟用户交互的命令,将这些命令传递给驱动程序。 Firefox我相信有一个插件允许您通过手动浏览创建命令,然后您可以将其泵回驱动程序进行自动测试。

我没有在上面的示例代码中看到任何“点击”或其他命令。有关以编程方式为驱动程序发出命令的示例,请参阅this

答案 1 :(得分:0)

你必须在这里注意几件事情:

  1. 要查看正在处理的某些浏览器活动,您需要访问url之类的"https://google.com"
  2. 要根据事件处理程序查看更多浏览器事件,您需要访问某些WebElement
  3. 因此,查看某些事件被触发的最小代码将是:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;
    
    public class my_program 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
            OverrideClass oc = new OverrideClass();
            eventDriver.register(oc);
            eventDriver.get("https://google.com");
            WebElement element = eventDriver.findElement(By.linkText("Gmail"));
            element.click();
        }
    
    }
    
  4. 现在,您已经创建了一些Sysouts,以查看正在处理的浏览器事件保持不变。

  5. 最后,当您执行此程序时,您将在控制台上看到以下输出:

    OverrideClass.beforeNavigateTo(): https://google.com
    OverrideClass.afterFindBy(): By.linkText: Gmail
    OverrideClass.afterClickOn(): [[FirefoxDriver: firefox on XP (b1412059-9499-45ff-b49a-2bc69a3ace30)] -> link text: Gmail]