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): Jump to the specified URL, only perform jump action, do not judge, do 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, and if Pageloadtimeout is specified, The org.openqa.selenium.TimeoutException will be thrown if it is not loaded within the specified time;
In this way, we can easily solve the problem of jump instability:
[Java]View Plaincopyprint?
- /**
- * Rewrite the Get method, adding user defined log</br>
- * Address Jump method, using the Webdriver native Get method, add the number of failed retries definition.
- *
- * @param url The URL you want to open.
- * @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: ten; Last jump Use maximum default time-out
- inited = navigateandload (URL, timeout);
- Index + +;
- }
- if (!inited && index = = actioncount) {//Final jump failure throws 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 method, the 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 page succeeded in returning true
- } catch (TimeoutException e) {
- return false; Returns false in case of timeout
- } catch (Exception e) {
- Failvalidation (); //Common exception handling methods
- Log.error (e); //Logging error logs
- throw New RuntimeException (e); Throw run-time exception, exit run
- }finally{
- Driver.manage (). Timeouts (). Pageloadtimeout (Maxloadtime, timeunit.seconds);
- }
- }