-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRemoteWebDriverAspect.java
More file actions
55 lines (47 loc) · 2.14 KB
/
RemoteWebDriverAspect.java
File metadata and controls
55 lines (47 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class RemoteWebDriverAspect {
@Before("call(org.openqa.selenium.remote.RemoteWebDriver+.new(..))")
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public void remoteWebDriverBeforeAspect(JoinPoint joinPoint) throws Throwable {
System.out.println("Before Creating driver...");
}
@Around("call(org.openqa.selenium.remote.RemoteWebDriver+.new(..))")
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public Object remoteWebDriverAspect(ProceedingJoinPoint point) throws Throwable {
//Code to run before creating the driver
long startTime = System.currentTimeMillis();
System.out.println("\n[" + elapsedTime(startTime) + "] Trying to create a Remote Web Driver");
Object driver = null;
int numOfRetries = 0;
while (driver == null & numOfRetries < Constants.MaxTimesToRetry) {
try {
System.out.println("[" + elapsedTime(startTime) + "] Try number : " + numOfRetries);
driver = point.proceed();
} catch (Throwable throwable) {
System.out.println("[" + elapsedTime(startTime) + "] Device allocation failed");
String message = throwable.getMessage();
System.out.println(message);
numOfRetries++;
Thread.sleep(Constants.WaitOnRetry);
}
}
if (driver != null) {
//Code to run after successfully creating a driver
System.out.println("[" + elapsedTime(startTime) + "] Remote Web Driver initialized successfully");
}
else {
//Code to run when used up retries with no success
System.out.println("[" + elapsedTime(startTime) + "] Failed to initialize a Remote Web Driver");
//Throw exception?
}
return driver;
}
private long elapsedTime(long startTime){
return (System.currentTimeMillis() - startTime) / 1000;
}
}