-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpyDriverExample.java
135 lines (115 loc) · 4.99 KB
/
SpyDriverExample.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package examples;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.sudharsan_selvaraj.SpyDriver;
import io.github.sudharsan_selvaraj.SpyDriverListener;
import io.github.sudharsan_selvaraj.SpyDriverOptions;
import io.github.sudharsan_selvaraj.types.driver.DriverCommand;
import io.github.sudharsan_selvaraj.types.driver.DriverCommandException;
import io.github.sudharsan_selvaraj.types.driver.DriverCommandResult;
import io.github.sudharsan_selvaraj.types.element.ElementCommand;
import io.github.sudharsan_selvaraj.types.element.ElementCommandException;
import io.github.sudharsan_selvaraj.types.element.ElementCommandResult;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class SpyDriverExample implements SpyDriverListener {
public static void main(String[] args) {
SpyDriverExample example = new SpyDriverExample();
WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");
java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
SpyDriver spyDriver = new SpyDriver(new FirefoxDriver(), SpyDriverOptions.builder().listener(example).build());
WebDriver driver = spyDriver.getSpyDriver();
example.runTest(driver);
}
public void runTest(WebDriver driver) {
driver.get("https://the-internet.herokuapp.com/");
driver.manage().window().setSize(new Dimension(1000, 400));
driver.findElement(By.partialLinkText("Inputs")).click();
driver.findElement(By.cssSelector("[type=\"number\"]")).sendKeys("1222");
driver.navigate().refresh();
driver.navigate().back();
driver.findElement(By.partialLinkText("Checkboxes")).click();
WebElement checkbox = driver.findElement(By.cssSelector("#checkboxes input"));
checkbox.isSelected();
checkbox.click();
checkbox.isSelected();
driver.quit();
}
@Override
public void beforeDriverCommandExecuted(DriverCommand command) {
try {
System.out.println("[WebDriver-before]" + command.getId());
Thread.sleep(500);
} catch (Exception e) {
}
}
@Override
public void afterDriverCommandExecuted(DriverCommandResult command) {
System.out.println("[WebDriver-After]" + command.getId());
if (command.getMethod().getName().startsWith("findElement")) {
printFindElementCommand(command.getMethod(), command.getArguments());
return;
}
switch (command.getMethod().getName()) {
case "get":
log("Navigated to:" + command.getArguments()[0]);
break;
case "quit":
log("Quiting webdriver session");
break;
case "back":
log("Navigating back");
break;
case "refresh":
log("Refreshing current page");
break;
case "setSize":
Dimension dimension = (Dimension) command.getArguments()[0];
log("Setting browser size as width:" + dimension.width + " and height:" + dimension.height);
break;
}
}
@Override
public void onException(DriverCommandException command) {
}
@Override
public void beforeElementCommandExecuted(ElementCommand command) {
System.out.println("[WebElement-Before]" + command.getId());
}
@Override
public void afterElementCommandExecuted(ElementCommandResult command) {
System.out.println("[WebElement-After]" + command.getId());
switch (command.getMethod().getName()) {
case "click" :
log("Clicked on element " + command.getLocator());
break;
case "sendKeys":
log("Entered " + Arrays.stream(((CharSequence[]) command.getArguments()[0])).collect(Collectors.joining(" ")) + " as value for element " + command.getLocator());
break;
case "isSelected":
log("Checking isSelected on element " + command.getLocator() + ". Result: "+ command.getResult());
break;
}
}
@Override
public void onException(ElementCommandException command) {
}
private void printFindElementCommand(Method method, Object[] arguments) {
if (method.getName().toLowerCase().contains("by")) {
log("Finding element by " + method.getName().split("By")[1] + ": " + arguments[0]);
} else {
log("Finding element by " + arguments[0].toString().split("\\.")[1]);
}
}
public void log(String message) {
System.out.println("[SpyDriver-LOG]: " + message);
}
}