Skip to content

Latest commit

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Retry Mechanism for Java

The following Java project presents a method to identify problems when trying to allocate a device or web vm from Perfecto CQ Lab and retry the request. Common problems in allocating a device include:

  • The device is busy,
  • Connection failure,
  • No available license

Quick start:

  • Clone the project and import to Intellij (Recommended) / Eclipse IDE.

  • Add your Perfecto Lab credentials at Constants.java :

publicclassConstants {
//User passstaticfinalStringPERFECTO_HOST = "MyHost";
staticfinalStringPERFECTO_USER = "MyUser";
staticfinalStringPERFECTO_PASSWORD = "MyPassword";
... 

Configure AspectJ for your IDE

Hooking driver creation RemoteWebDriverAspect.java :

We use @Aspect annotations to control the RemoteWebDriver instance creation:

  • By using Before, After, Around annotations we are able to wrap the RemoteWebDriver constructor and add some control operations.
    For example, in the following code:
@Before("call(org.openqa.selenium.remote.RemoteWebDriver+.new(..))")
@SuppressWarnings("PMD.AvoidCatchingThrowable")
publicvoidremoteWebDriverBeforeAspect(JoinPointjoinPoint) throwsThrowable {
System.out.println("Before Creating driver...");
}

The remoteWebDriverBeforeAspect method will be executed before calling the constructor of every RemoteWebDriver instance.
The @Around annotation enables us to control what happens both before and after the method we wrap with the annotation:

@Around("call(org.openqa.selenium.remote.RemoteWebDriver+.new(..))")
@SuppressWarnings("PMD.AvoidCatchingThrowable")
publicObjectremoteWebDriverAspect(ProceedingJoinPointpoint) throwsThrowable {
... //Code before method try{
proceed = point.proceed();
} catch (Throwablethrowable) {
... // Code on failure 
}
... // Code to call after the method 
}

Advanced Options (Retry count, delay ....)

We use Constants to determine the retry mechanism settings such as: Maximum times to retry and delay between each retry.
For example in Constants file:

// Java code ...//General constantsstaticfinalintImplicitlyWait = 25;
staticfinalintPageLoadTimeout = 25;
staticfinalintWebDriverWait = 10;
staticfinalintMaxTimesToRetry = 10;
staticfinallongWaitOnRetry = 10000L;
// Java code

The following code snippet includes the variables:
MaxTimesToRetry - Maximum number of retries to create WebDriver instance.
WaitOnRetry - How long (milliseconds) to wait between each retry.

RemoteWebDriverAspect overview

In this project we wrap the RemoteWebDriver constructor in an @Around annotation and execute the try-catch clause within a while loop. If the constructor throws an exception, the catch clause increments the count of retries and then lets the while loop retry the constructor.

About

Java sample of retry mechanism on driver creation failure

Resources

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages