Eventfiringwebdriver Web Event Monitoring (ii)

Source: Internet
Author: User

 Public classSeleniumdemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); Navigation Navigate=driver.navigate (); Eventfiringwebdriver eventdriver =Neweventfiringwebdriver (driver); //Registering Events        Eventdriver.register(New Mywebdriverlistener ()); //Browse specific pagesEventdriver.navigate (). to ("http://www.jd.com/"); //Click the buttonWebelement Loginlink = eventdriver.findelement (By.partiallinktext ("Deng"));        Loginlink.click (); //enter content in the search boxEventdriver.findelement (By.id ("kw")). SendKeys ("the Paladin"); //intentionally write a wrong operation that causes selenium to produce an exception        Try{eventdriver.findelement (by.id ("The Day print")); }Catch(Exception e) {e.printstacktrace (); }    }}

Here is the listener I wrote myself Mywebdriverlistener

 Public class Mywebdriverlistener Implementswebdrivereventlistener{@Override Public voidonexception (throwable throwable, Webdriver driver) {SimpleDateFormat format=NewSimpleDateFormat ("Yyyy_mm_dd_hh_mm_ss"); String datestring= Format.format (NewDate ()); File Srcfile=((takesscreenshot) driver). Getscreenshotas (Outputtype.file); Try{File screenshot=NewFile ("d:\\" +datestring+ ". png");        Fileutils.copyfile (Srcfile, screenshot); }Catch(Exception e) {e.printstacktrace (); } System.out.println ("The reason for the change is:" +throwable.getmessage ()); System.out.println ("KeepAlive:" + "d:\\" +datestring+ ". png"); } @Override Public voidafternavigateto (String URL, webdriver driver) {System.out.println ("Afternavigateto:" +URL); System.out.println ("Afternavigateto by driver:" +Driver.getcurrenturl ()); } @Override Public voidBeforeclickon (webelement element, Webdriver driver) {System.out.println ("Properties of a single page element:" +element.getattribute ("value"))); } @Override Public voidBeforefindby (by by, webelement element, Webdriver driver) {System.out.println ("The criteria for finding an element is:" +by.tostring ()); } @Override Public voidafterchangevalueof (webelement element, Webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidAfterclickon (webelement element, Webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidAfterfindby (by, webelement element, Webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidafternavigateback (webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidAfternavigateforward (webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidAfternavigaterefresh (webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidafterscript (String URL, webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidbeforechangevalueof (webelement element, Webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidbeforenavigateback (webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidBeforenavigateforward (webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidBeforenavigaterefresh (webdriver driver) {//TODO auto-generated Method Stub} @Override Public voidbeforenavigateto (String URL, webdriver driver) {System.out.println ("Beforenavigateto:" +URL); System.out.println ("Beforenavigateto by driver:" +Driver.getcurrenturl ()); } @Override Public voidbeforescript (String URL, webdriver driver) {//TODO auto-generated Method Stub    }}

The listener above can meet the following requirements:

1. Execute the opening page of the operating room, you need to record the pre-opening and after the URL address.
2. When looking for a page element, you need to record the search criteria before and after the search
3. On the page to the single-machine operation room, click before the need to record the elements of the search criteria, a single machine after the record URL address
4. Change the value of the page element to record the value before the change and the changed value, respectively
5. When an exception occurs, it needs to be done, save the file in D:\, and name the file with the current date

Original article Source: http://blog.csdn.net/slowsnowscar/article/details/52507288

Eventfiringwebdriver Web Event Monitoring (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.