Selenium操作监听
对于UI自动化测试,如果运行测试时,出现接口报错等非常规的问题,无法有效的进行报错的校验和判断,这时候可以通过实现WebDriverEventListener的接口,可以捕获各种事件,例如页面加载、元素点击、输入文本等。
下面是使用WebDriverEventListener的示例
importorg.openqa.selenium.By;importorg.openqa.selenium.OutputType;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.support.events.EventFiringWebDriver;importorg.openqa.selenium.support.events.WebDriverEventListener;importorg.openqa.selenium.support.ui.ExpectedConditions;importorg.openqa.selenium.support.ui.WebDriverWait;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importstaticcom.autotest.utils.Selenium.saveFailureScreenShot;publicclassPageListenerimplementsWebDriverEventListener{privatestaticfinalLogger log =LoggerFactory.getLogger(PageListener.class);@OverridepublicvoidbeforeAlertAccept(WebDriver driver){//编写监听 Alert.accept() 操作前的代码}@OverridepublicvoidafterAlertAccept(WebDriver driver){//编写监听 Alert.accept() 操作后的代码}@OverridepublicvoidafterAlertDismiss(WebDriver driver){//编写监听 Alert.dismiss() 操作前的代码}@OverridepublicvoidbeforeAlertDismiss(WebDriver driver){//编写监听 Alert.dismiss() 操作后的代码}@OverridepublicvoidbeforeNavigateTo(String url,WebDriver driver){//编写监听 navigate().to(String url). 操作前的代码}@OverridepublicvoidafterNavigateTo(String url,WebDriver driver){//编写监听 navigate().to(String url). 操作后的代码}@OverridepublicvoidbeforeNavigateBack(WebDriver driver){//编写监听 navigate().back(). 操作前的代码}@OverridepublicvoidafterNavigateBack(WebDriver driver){//编写监听 navigate().back(). 操作后的代码}@OverridepublicvoidbeforeNavigateForward(WebDriver driver){//编写监听 navigate().forward(). 操作前的代码}@OverridepublicvoidafterNavigateForward(WebDriver driver){//编写监听 navigate().forward(). 操作后的代码}@OverridepublicvoidbeforeNavigateRefresh(WebDriver driver){//编写监听 navigate().refresh(). 操作前的代码}@OverridepublicvoidafterNavigateRefresh(WebDriver driver){//编写监听 navigate().refresh(). 操作后的代码//此示例是切换窗口后,刷新窗口,监听是否有报错弹窗出现try{WebDriverWait wait =newWebDriverWait(driver,1);WebElement errorPopup = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(s)));if(errorPopup.isEnabled()){//保存报错图片,返回路径String addr =saveFailureScreenShot(driver);//输出到结果文档Excel中ExcelData.outExcle(caseName,stepName,"切换窗口后监听页面出现报错弹窗",addr);
log.info(stepName +":切换窗口后监听页面出现报错弹窗");
driver.quit();}}catch(Exception e){
log.info(stepName +":切换窗口后监听页面没有出现报错弹窗");}}@OverridepublicvoidbeforeFindBy(By by,WebElement element,WebDriver driver){//编写监听 WebDriver.findElement(...), or WebDriver.findElements(...), or WebElement.findElement(...), or WebElement.findElements(...). 操作前的代码}@OverridepublicvoidafterFindBy(By by,WebElement element,WebDriver driver){//编写监听 WebDriver.findElement(...), or WebDriver.findElements(...), or WebElement.findElement(...), or WebElement.findElements(...). 操作后的代码}@OverridepublicvoidbeforeClickOn(WebElement element,WebDriver driver){//编写 ebElement.click(). 操作前的代码}@OverridepublicvoidafterClickOn(WebElement element,WebDriver driver){//编写 WebElement.click(). 操作后的代码//此示例是监听点击后,是否出现报错弹窗try{WebDriverWait wait =newWebDriverWait(driver,1);WebElement errorPopup = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(s)));if(errorPopup.isEnabled()){//保存报错图片,返回路径String addr =saveFailureScreenShot(driver);//输出到结果文档Excel中ExcelData.outExcle(caseName,stepName,"点击后监听页面出现报错弹窗",addr);
log.info(stepName +":点击后监听页面出现报错弹窗");
driver.quit();}}catch(Exception e){
log.info(stepName +":点击后监听页面没有出现报错弹窗");}}@OverridepublicvoidbeforeChangeValueOf(WebElement element,WebDriver driver,CharSequence[] keysToSend){//编写 ebElement.clear(), WebElement.sendKeys(...). 操作前的代码}@OverridepublicvoidafterChangeValueOf(WebElement element,WebDriver driver,CharSequence[] keysToSend){//编写 WebElement.clear(), WebElement.sendKeys(...). 操作后的代码}@OverridepublicvoidbeforeScript(String script,WebDriver driver){//编写 org.openqa.selenium.remote.RemoteWebDriver.executeScript(String, Object[]) 操作前的代码}@OverridepublicvoidafterScript(String script,WebDriver driver){//编写 org.openqa.selenium.remote.RemoteWebDriver.executeScript(String, Object[]) 操作后的代码}@OverridepublicvoidbeforeSwitchToWindow(String windowName,WebDriver driver){//编写 WebDriver.TargetLocator.window(String) 操作前的代码}@OverridepublicvoidafterSwitchToWindow(String windowName,WebDriver driver){//编写 WebDriver.TargetLocator.window(String) 操作后的代码}@OverridepublicvoidonException(Throwable throwable,WebDriver driver){//编写抛出异常的操作代码}@Overridepublic<X>voidbeforeGetScreenshotAs(OutputType<X> target){//编写 org.openqa.selenium.TakesScreenshot.getScreenshotAs(OutputType) 操作前的代码}@Overridepublic<X>voidafterGetScreenshotAs(OutputType<X> target,X screenshot){//编写 org.openqa.selenium.TakesScreenshot.getScreenshotAs(OutputType) 操作后的代码}@OverridepublicvoidbeforeGetText(WebElement element,WebDriver driver){//编写 WebElement.getText() 操作前的代码}@OverridepublicvoidafterGetText(WebElement element,WebDriver driver,String text){//编写 WebElement.getText() 操作后的代码}
使用监听时,需要将其注册到WebDriver实例中
WebDriver driver =newChromeDriver();EventFiringWebDriver driverWithEvents =newEventFiringWebDriver(driver);PageListener listener =newPageListener();
driverWithEvents.register(listener);
版权归原作者 可遇_不可求 所有, 如有侵权,请联系我们删除。