SELENIUM2 Learning -015-webui Automation Real instance -013-close extra open browser window via URL

Source: Internet
Author: User

In the routine WebUI automated test script execution process, often open a different Web page, to do the appropriate action, at this time may open a lot of web pages, when there are too many open pages, invalid Web resources to run the script machine caused too many invalid resource waste, Therefore, in the process of daily Web page Automation test script to close too many redundant pages, reduce the system invalid loss.

The method described in this article through the URL to the open window to match, will not match the window page closed, to a certain extent reduce system loss, interested in the small owners, can further optimize the corresponding program source code, or in the daily script to call this method in the process of making the parameter URL accurate enough, Minimizes system invalidation losses.

Many do not chat, small two on the yards ... Please refer to the small master, hope to be able to you in the daily WebUI automated scripting have some inspiration and help. If there are deficiencies or errors, please ask the great God to correct, greatly appreciated!

1     /**2 * Close the window if current URL was not expected, and get expected URL3      * 4      * @authorAARON.FFP5      * @versionV1.0.0:autoseleniumdemo main.aaron.sele.core Seleniumcore.java getUrl, 2015-6-22 12:22:30 Exp $6      * 7      * @paramurl:expected URL8      * @returnWebdriver9      */Ten      Publicwebdriver getUrl (String url) { One         //define variable to store current page title AString Currenturl = ""; -          -         //Get all Windows theset<string> Windows = This. Webdriver.getwindowhandles (); -          -         if(Windows.size () < 1) { -             return  This. Webdriver; +         } -          +         Try { A              for(String window:windows) { at                 //Change window -                  This. Webdriver.switchto (). window (window); -                  -                 //Refresh the page. You can annotation this, because it 's not necessary if network is good - //this.webdriver.navigate (). Refresh (); -                  inThread.Sleep (3000); -                  to                 //get page URL +Currenturl = This. Webdriver.getcurrenturl (). toString (); -                  the                 //Verify the current page was expect or not, return this if correct *                 if(!currenturl.startswith (URL)) { $                      This. Webdriver.close ();Panax Notoginseng                 } -             } the}Catch(Exception e) { + e.printstacktrace (); A}finally { the              This. Webdriver.get (URL); +         } -  $         return  This. Webdriver; $}
to close a mismatched window through a page URL, releasing the system resource method source code

At this point, theWebUI automation function test Script No. 013-through the URL to close the Redundant Open browser window successfully completed, I hope this article can give beginners Selenium you a reference.

Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^

SELENIUM2 Learning -015-webui Automation Real instance -013-close extra open browser window via URL

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.