• 使用selenium监听每一步操作


    1.创建类LogEventListener.java, 如下:

    package com.demo;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.events.WebDriverEventListener;
    
    
    /**
     * 用一个类扩展web driver自带的事件监听器,可以实现许多有趣的功能。
     * 比如自动log
     * a customer event listener
     */
    public  class LogEventListener implements WebDriverEventListener {
    	
    	public void beforeNavigateTo(String url, WebDriver driver) {
    		System.out.println("转向前URL: '" + url + "'");
    	}
    
    	public void afterNavigateTo(String url, WebDriver driver) {
    		System.out.println("转向后URL:'" + url + "'");
    	}
    
    	public void beforeChangeValueOf(WebElement element, WebDriver driver) {
    		System.out.println("Value of the:" + element.toString()
    				+ " before any changes made");
    	}
    
    	public void afterChangeValueOf(WebElement element, WebDriver driver) {
    		System.out.println("Element value changed to: " + element.toString());
    	}
    
    	public void beforeClickOn(WebElement element, WebDriver driver) {
    		System.out.println("试图单击对象: " + element.toString());
    	}
    
    	public void afterClickOn(WebElement element, WebDriver driver) {
    		System.out.println("单击对象: " + element.toString());
    	}
    
    	public void beforeNavigateBack(WebDriver driver) {
    		System.out.println("Navigating back to previous page");
    	}
    
    	public void afterNavigateBack(WebDriver driver) {
    		System.out.println("Navigated back to previous page");
    	}
    
    	public void beforeNavigateForward(WebDriver driver) {
    		System.out.println("Navigating forward to next page");
    	}
    
    	public void afterNavigateForward(WebDriver driver) {
    		System.out.println("Navigated forward to next page");
    	}
    
    	public void onException(Throwable error, WebDriver driver) {
    		System.out.println("Exception occured: " + error);
    	}
    
    	public void beforeFindBy(By by, WebElement element, WebDriver driver) {
    		System.out.println("试图寻找对象 : " + by.toString());
    	}
    
    	public void afterFindBy(By by, WebElement element, WebDriver driver) {
    		System.out.println("找到对象 : " + by.toString());
    	}
    
    	/*
    	 * non overridden methods of WebListener class
    	 */
    	public void beforeScript(String script, WebDriver driver) {
    	}
    
    	public void afterScript(String script, WebDriver driver) {
    	}
    
    	@Override
    	public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void afterNavigateRefresh(WebDriver arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void beforeNavigateRefresh(WebDriver arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	 	
    }
    

    2.在创建WebEventListener监听类,如下:

    package com.demo;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
    
    public class WebEventListener extends AbstractWebDriverEventListener {
    
    	public void beforeNavigateTo(String url, WebDriver driver) {
    		System.out.println("打开前url: '" + url + "'");
    	}
    
    	public void afterNavigateTo(String url, WebDriver driver) {
    		System.out.println("打开url:'" + url + "'");
    	}
    
    	public void beforeClickOn(WebElement element, WebDriver driver) {
    		System.out.println("试图单击对象: " + element.toString());
    	}
    
    	public void afterClickOn(WebElement element, WebDriver driver) {
    		System.out.println("单击对象: " + element.toString());
    	}
    
    	public void onException(Throwable error, WebDriver driver) {
    		System.out.println("Error occurred: " + error);
    	}
    }
    

     3.使用监听,如下:

    package com.demo;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.events.EventFiringWebDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.Assert;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    public class ListenerTest {
    	
    	private WebDriver driver;
    	private EventFiringWebDriver e_driver;
    	private WebEventListener eventListener;
    	private WebDriverWait wait;
    	private String appURL = "http://www.google.com";
    	public int waitTime = 10;
    	private String headerText = "One account. All of Google.";
    	private String errMessage = "The email and password you entered don't match.";
    
    	@BeforeClass()
    	public void setUp() {
    
    		driver = new FirefoxDriver();
    		wait = new WebDriverWait(driver, waitTime);
    
    		e_driver = new EventFiringWebDriver(driver);
    
    		eventListener = new WebEventListener();
    		e_driver.register(eventListener);
    	
    		e_driver.manage().window().maximize();
    		e_driver.get(appURL);
    	}
    
    	@Test
    	public void testEventsONE() {
    		System.out.println("***** Executing Test ONE ***** ");
    		e_driver.findElement(By.linkText("Gmail")).click();
    		String pageHeaderText = e_driver.findElement(By.tagName("h1")).getText();
    		Assert.assertTrue(pageHeaderText.equalsIgnoreCase(headerText));
    	}
    
    	@Test
    	public void testEventsTWO()  {
    		System.out.println("***** Executing Test Two ***** ");
    		e_driver.findElement(By.id("Email")).sendKeys("username");
    		e_driver.findElement(By.id("next")).click();
    		
    		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
    		e_driver.findElement(By.id("Passwd")).sendKeys("password");
    		e_driver.findElement(By.id("signIn")).click();
    		
    		String pageHeaderText = e_driver.findElement(By.id("errormsg_0_Passwd")).getText();
    		Assert.assertTrue(pageHeaderText.equalsIgnoreCase(errMessage));
    	}
    
    	@AfterClass()
    	public void tearDown() {
    		if (e_driver != null) {
    			e_driver.quit();
    		}
    	}
    
    
    }
    

    运行查看控制台打印的信息

  • 相关阅读:
    Linux-003-Resource temporarily unavailable
    JMeter学习-041-响应数据中文乱码解决方法
    Java学习-050-AES256 之 java.security.InvalidKeyException: Illegal key size or default parameters 解决方法
    Java学习-049-正则工具类
    JMeter学习-040-JMeter图形化 HTML 报表概要说明
    JMeter学习-039-JMeter 3.0 生成 dashboard HTML 报告图表中文乱码
    JMeter学习-038-JMeter Linux 环境配置
    Linux-002-执行命令时,提示: -bash: {命令}: command not found
    JMeter学习-037-JMeter调试工具之四-BeanShell+jmeter.log
    JMeter学习-036-JMeter调试工具之三---Debug Sampler
  • 原文地址:https://www.cnblogs.com/lincj/p/7007428.html
Copyright © 2020-2023  润新知