我得到了代码如何在selenium中以正常方式使用WebDriverEventListener。 但是不知道如何将它用于PageFactory模式。
代码如下,首先需要实现WebDriverEventListner接口
public class EventHandler implements WebDriverEventListener{
private Log log = LogFactory.getLog(this.getClass());
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("inside method afterChangeValueOf on " + arg0.toString());
log.info("inside method afterChangeValueOf on " + arg0.toString());
}
public void afterClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("inside method afterClickOn on " + arg0.toString());
}
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
log.info("Find happened on " + arg1.toString()
+ " Using method " + arg0.toString());
}
public void afterNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
log.info("Inside the after navigateback to " + arg0.getCurrentUrl());
}
public void afterNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
log.info("Inside the afterNavigateForward to " + arg0.getCurrentUrl());
}
public void afterNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("Inside the afterNavigateTo to " + arg0);
}
public void afterScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("Inside the afterScript to, Script is " + arg0);
}
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("Inside the beforeChangeValueOf method");
}
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("About to click on the " + arg0.toString());
}
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
log.info("Just before finding element " + arg1.toString());
}
public void beforeNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
log.info("Just before beforeNavigateBack " + arg0.getCurrentUrl());
}
public void beforeNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
log.info("Just before beforeNavigateForward " + arg0.getCurrentUrl());
}
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("Just before beforeNavigateTo " + arg0);
}
public void beforeScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
log.info("Just before beforeScript " + arg0);
}
public void onException(Throwable arg0, WebDriver arg1) {
log.info("Exception occured at " + arg0.getMessage());
}
public void afterNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
public void beforeNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
}
接下来我们需要在脚本中使用它,如下所示:
@BeforeClass()
public void signIn() throws Exception
{
BasicConfigurator.configure();
PropertyConfigurator.configure(PROPERTIES_FILEPATH);
log = Logger.getLogger("SuperTest");
//Delete the test-output directory
try
{
FileUtils.deleteDirectory(new File("./test-output"));
}
catch(Exception e)
{
}
//Get the Browser Type and initiate the Driver
driver = GenericUtilLibrary.initializeBrowser(driver, BROWSER_TYPE);
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.manage().window().maximize();
//create a object for EventFiringWebDriver Class
eventDriver = new EventFiringWebDriver(driver);
//create object of the class (EventHandler) which implemented WebDriverEventListener Interface.
handler = new EventHandler();
//register handler object with EventFiringWebDriver
eventDriver.register(handler);
eventDriver.get(URL);
//CLICK ON SIGNIN BUTTON IN LOGIN PAGE.
LoginPage lpage = new LoginPage(driver);
lpage.clickSignInButton();
//ENTER USER CREDENTIALS AND CLICK ON SIGNON BUTTON IN SIGNIN PAGE.
SignInPage spage = new SignInPage(driver);
spage.clickSignInButton(USERNAME, PASSWORD);
}
一旦注册,当我们对所有WebElements使用eventDriver时,eventDriver将处理记录WebDriver的操作。
直到eventDriver.get(URL);我没有问题但是当谈到下一步时,我正在调用Object Page Classes并调用它们的方法。要发送eventDriver而不是驱动程序对象,我在调用第一个方法本身时会收到NULLPOINTEREXCEPTION。
我不知道如何使用WebDriverEventListener。
请任何人帮助我。
答案 0 :(得分:1)
在您的代码中,您有一个简单的错误。
代替:
//CLICK ON SIGNIN BUTTON IN LOGIN PAGE.
LoginPage lpage = new LoginPage(driver);
lpage.clickSignInButton();
您应该使用:
//CLICK ON SIGNIN BUTTON IN LOGIN PAGE.
LoginPage lpage = new LoginPage(handler);
lpage.clickSignInButton();
答案 1 :(得分:0)
问题出在WebDriverEventListener类中,而不是初始化每个类的构造函数。有了@Denis的建议它会起作用。在WebDriverEventListener类中,错误的是beforeFindBy
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
log.info("Just before finding element " + arg1.toString());
}
它应该是
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
log.info("Just before finding element " + arg0);
}
答案 2 :(得分:0)
我在配置WebDriverEventListener和我的项目时也遇到了同样的问题。我确实通过传递EventFiringWebDriver对象而不是驱动程序来弄清楚。 而且,瞧,它很棒!
public static EventFiringWebDriver eventFiringWebDriver;
public static WebDriverEventListener webDriverEventListener;
eventFiringWebDriver= new EventFiringWebDriver(driver);
webDriverEventListener= new WebEventListener();
eventFiringWebDriver.register(webDriverEventListener);
LoginPage lpage = new LoginPage(eventFiringWebDriver);