java 我在Selenium中收到消息“类型EventFiringWebDriver已弃用”

sd2nnvve  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(121)

据我所知,在Selenium 4中,“EventFiringWebDriver”已经贬值。还有什么选择呢?我需要对代码进行哪些更改?请参阅截图所附的代码。也粘贴下面的代码:

package testcases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.Test;

import listener.DriverUpdate;

public class TC_001 {
    
    @Test
    public void tc001()
    {
        
        System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver");
        WebDriver driver = new ChromeDriver();
        
        //Step 2
        EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
        
        //Step 3
        DriverUpdate listener = new DriverUpdate();
        eventDriver.register(listener);
        
        
        driver.navigate().to("http://www.theTestingWorld.com");
        
        
    }

}

我希望能够创建EventFiringWebDriver的对象,然后我希望能够用EventDriver注册我的类(我们继承EventDriver接口的地方)。当然,它不允许我这么做,因为它已经贬值了。

izkcnapc

izkcnapc1#

在Selenium 4中,我使用的是4。8.3下面是你如何做的
首先创建一个Listener类来实现WebDriverListener接口现在我们将覆盖它的一些方法,如下所示,您可以根据您的需求覆盖更多方法

import org.openqa.selenium.support.events.WebDriverListener;

public class Listener implements WebDriverListener {

    @Override
    public void beforeAnyWebDriverCall(WebDriver driver, Method method, Object[] args) {
        System.out.println("beforeAnyWebDriverCall");
    }

    @Override
    public void afterGet(WebDriver driver, String url) {
        System.out.println("after get");
    }
}

现在,在创建Webdriver的类中,您需要使用EventFiringDecorator类来注册Listeners并装饰驱动程序

import java.io.FileNotFoundException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringDecorator;

public class EventTest {

    public static void main(String[] args) throws FileNotFoundException {
        System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver");
        WebDriver original = new ChromeDriver();
        Listener listener = new Listener();//Create instance of Listener Class
        EventFiringDecorator<WebDriver> decorator = new EventFiringDecorator<>(listener); //Pass listener to constructor
         
        WebDriver decorated = decorator.decorate(original);
        decorated.get("http://www.theTestingWorld.com");
        decorated.quit();
    }
}

当这段代码运行时,你会得到下面的输出,

beforeAnyWebDriverCall // printed before getURL
after get // After getURL
beforeAnyWebDriverCall // printed before Quit

您可以查看Selenium docs EventFiringDecorator中给出的这个好例子
可以覆盖的与导航相关的方法

// Navigation

  default void beforeAnyNavigationCall(WebDriver.Navigation navigation, Method method, Object[] args)  {}
  default void afterAnyNavigationCall(WebDriver.Navigation navigation, Method method, Object[] args, Object result)  {}

  default void beforeTo(WebDriver.Navigation navigation, String url) {}
  default void afterTo(WebDriver.Navigation navigation, String url) {}

  default void beforeTo(WebDriver.Navigation navigation, URL url) {}
  default void afterTo(WebDriver.Navigation navigation, URL url) {}

  default void beforeBack(WebDriver.Navigation navigation) {}
  default void afterBack(WebDriver.Navigation navigation) {}

  default void beforeForward(WebDriver.Navigation navigation) {}
  default void afterForward(WebDriver.Navigation navigation) {}

  default void beforeRefresh(WebDriver.Navigation navigation) {}
  default void afterRefresh(WebDriver.Navigation navigation) {}
agyaoht7

agyaoht72#

感谢您的回复!这真的很有帮助。因此,我对类进行了排序,在那里我创建了WebDriver。
然而,在Listener类中,我似乎得到了以下内容:

类型为DriverUpdate的afterNavigateTo(String,WebDriver)方法必须重写或实现超类型方法

相关问题