Webdriver package Get method to solve the problem of page jump instability

Source: Internet
Author: User
Tags exception handling

In most test environments, the network or test server host is not always a problem, many times a client's request for a jump because of unstable network or occasional other exceptions hang dead there for half a day, until the advent of manual intervention.
When the Webdriver test executes, the occasional page jump or load time-out exception occurs, which causes the process test to break, which can bring a great loss to the integrity and validity of the test. In fact, this problem is very good to solve, just need to rewrite or encapsulate the Get method to implement the jump on the line.
Before we do this package, we have to tell the difference between the Driver.get (URL) and the Driver.navigate (). to (URL):
Driver.navigate (). to (URL): Jumps to the specified URL, performs only jump actions, does not judge, does not wait for the specified page to load successfully; Driver.get (URL): Jumps to the specified URL and checks to see if the page is loaded. If Pageloadtimeout is specified, and the load is not completed within the specified time, the org.openqa.selenium.TimeoutException is thrown, so we can easily solve the problem of jump instability:

	/** * Rewrite the Get method, adding user defined log</br> * Address jump methods, using the Webdriver native Get method, add the number of failed retries defined. * * @param URL The URL you want to <span id= "2_nwp" style= "Width:auto; Height:auto; Float:none; " ><a target=_blank id= "2_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news &cf=1001&ch=0&di=128&fv=11&is_app=0&jk=69829344341ee6cd&k=open&k0=open& Kdi0=0&luki=2&n=10&p=baidu&q=v77y4_cpr&rb=0&rs=1&seller_id=1&sid= cde61e3444938269&ssp2=1&stid=0&t=tpclicked3_hc&td=2102575&tu=u2102575&u=http%3a%2f% 2fwww%2eylzx8%2ecn%2fzonghe%2fopen%2dsource%2f247951%2ehtml&urlid=0 "target=" _blank "mpid=" 2 "style=" COLOR: RGB (1, 70, 108); Text-decoration:none; " ><span style= "Color:rgb (0, 0, 255); Width:auto; Height:auto; "
	 >open</span></a></span>.
	 * @param actioncount retry times when load timeout occuers. * @throws RuntimeException
	 */protected void get (String url, int actioncount) {Boolean inited = false;
		int index = 0, timeout = 10; while (!inited && Index < actioncount) {timeout = (index = = actionCount-1)? maxloadtime:10;//last jump use Max
			The default timeout time inited = navigateandload (URL, timeout);
		Index + +; if (!inited && index = actioncount) {//Final jump failure throws a run-time exception, exit run throw new RuntimeException ("Can not get the URL

	["+ URL +"] after Retry "+ Actioncount +" times! ");}}
	 /** * Rewrite the Get method, adding user defined log</br> * Address jump method, using the Webdriver native Get methods, default load overweight test "1" times.
	 * * @param URL The URL you want to open.
	* @throws runtimeexception */protected void get (String URL) {get (URL, 2);
	 }/** * Judge if the URL has navigate and page load completed.</br> * Jumps to the specified URL and returns whether to jump to the full result.
	 * * @param URL The URL you want to open.
	 * @param timeout, the timeout for page, load in seconds.
	 * @return if page load completed. */Private Boolean NavIgateandload (String url, int timeout) {try {driver.manage (). Timeouts (). Pageloadtimeout (timeout, timeunit.seconds);
			Driver.get (URL); 
			Return true;//jump and load the page succeeds in returning true} catch (TimeoutException e) {return false;//time out, false} catch (Exception e) { Failvalidation ();///Common exception handling method Log.error (e);//Record error log throw new RuntimeException (e);//Throw run-time exception, exit run}finally{D
		River.manage (). Timeouts (). Pageloadtimeout (Maxloadtime, timeunit.seconds); }
	}

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.