-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex7_ScriptThings.java
58 lines (43 loc) · 1.88 KB
/
ex7_ScriptThings.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
package examples;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import utils.Config;
public class ex7_ScriptThings {
private static final Logger logger = Config.getLogger(ex7_ScriptThings.class);
public static void main(String[] args) {
new ex7_ScriptThings();
}
public ex7_ScriptThings() {
Config cfg = new Config();
// Look at util.Config.createInstance for details
WebDriver drv = cfg.createInstance();
drv.navigate().to(cfg.get("site_url"));
logger.info("At the test site...");
this.scriptStuff(drv);
drv.close();
}
public void scriptStuff(WebDriver drv) {
logger.info("Show kitty, using JS");
((JavascriptExecutor)drv).executeScript("Kitty.show()");
// run a throw-away find just to confirm he is there (no need to keep the object)
drv.findElement(By.id("kitty"));
// set Kittys name
WebElement input = drv.findElement(By.id("nameInput"));
String jsCode = "arguments[0].value = arguments[1];";
((JavascriptExecutor)drv).executeScript(jsCode, input, "Jones");
logger.info("Changed kittys name to Jones (Xenomorph does not like this).");
// check Kittys name
jsCode = "return arguments[0].value;";
String name = (String)((JavascriptExecutor)drv).executeScript(jsCode, input);
logger.info("Confirm that kittys name is now: " + name);
// obviously we can choose whether we want to use WebDriver or JS to locate the element
jsCode =
"let kitty = document.getElementById('kitty');"
+ "kitty.style.display = 'none';";
((JavascriptExecutor)drv).executeScript(jsCode);
logger.info("Jones has gone stealth. Sneaky.");
}
}