Go: "Webdriver" encapsulates the Get method to solve the problem of page jump instability

Source: Internet
Author: User

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?
    1. /**
    2. * Rewrite the Get method, adding user defined log</br>
    3. * Address Jump method, using the Webdriver native Get method, add the number of failed retries definition.
    4. *
    5. * @param url The URL you want to open.
    6. * @param actioncount retry times when load timeout occuers.
    7. * @throws RuntimeException
    8. */
    9. protected void get (String URL, int actioncount) {
    10. Boolean inited = false;
    11. int index = 0, timeout = 10;
    12. While (!inited && Index < actioncount) {
    13. Timeout = (Index = = actioncount- 1)? Maxloadtime: ten; Last jump Use maximum default time-out
    14. inited = navigateandload (URL, timeout);
    15. Index + +;
    16. }
    17. if (!inited && index = = actioncount) {//Final jump failure throws run-time exception, exit run
    18. throw New RuntimeException ("Can not get the URL" ["+ URL + "] After Retry "+ Actioncount + " times! ")  ;
    19. }
    20. }
    21. /**
    22. * Rewrite the Get method, adding user defined log</br>
    23. * Address Jump method, using the Webdriver native Get method, the default load overweight test "1" times.
    24. *
    25. * @param url The URL you want to open.
    26. * @throws RuntimeException
    27. */
    28. protected void get (String url) {
    29. Get (URL, 2);
    30. }
    31. /**
    32. * Judge if the URL has navigate and page load completed.</br>
    33. * Jumps to the specified URL and returns whether to jump to the full result.
    34. *
    35. * @param url The URL you want to open.
    36. * @param timeout, the timeout for page, load in seconds.
    37. * @return if page load completed.
    38. */
    39. Private boolean navigateandload (String URL, int timeout) {
    40. try {
    41. Driver.manage (). Timeouts (). Pageloadtimeout (timeout, timeunit.seconds);
    42. Driver.get (URL);
    43. return true; Jump and Load page succeeded in returning true
    44. } catch (TimeoutException e) {
    45. return false; Returns false in case of timeout
    46. } catch (Exception e) {
    47. Failvalidation (); //Common exception handling methods
    48. Log.error (e); //Logging error logs
    49. throw New RuntimeException (e); Throw run-time exception, exit run
    50. }finally{
    51. Driver.manage (). Timeouts (). Pageloadtimeout (Maxloadtime, timeunit.seconds);
    52. }
    53. }

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.