|
52 | 52 | import org.openqa.selenium.Dimension;
|
53 | 53 | import org.openqa.selenium.HasCapabilities;
|
54 | 54 | import org.openqa.selenium.ImmutableCapabilities;
|
| 55 | +import org.openqa.selenium.JavascriptException; |
55 | 56 | import org.openqa.selenium.JavascriptExecutor;
|
56 | 57 | import org.openqa.selenium.MutableCapabilities;
|
57 | 58 | import org.openqa.selenium.NoAlertPresentException;
|
|
62 | 63 | import org.openqa.selenium.Platform;
|
63 | 64 | import org.openqa.selenium.Point;
|
64 | 65 | import org.openqa.selenium.PrintsPage;
|
| 66 | +import org.openqa.selenium.ScriptKey; |
65 | 67 | import org.openqa.selenium.SearchContext;
|
66 | 68 | import org.openqa.selenium.SessionNotCreatedException;
|
67 | 69 | import org.openqa.selenium.TakesScreenshot;
|
| 70 | +import org.openqa.selenium.UnpinnedScriptKey; |
68 | 71 | import org.openqa.selenium.WebDriver;
|
69 | 72 | import org.openqa.selenium.WebDriverException;
|
70 | 73 | import org.openqa.selenium.WebElement;
|
|
79 | 82 | import org.openqa.selenium.interactions.Sequence;
|
80 | 83 | import org.openqa.selenium.internal.Debug;
|
81 | 84 | import org.openqa.selenium.internal.Require;
|
| 85 | +import org.openqa.selenium.json.TypeToken; |
82 | 86 | import org.openqa.selenium.logging.LocalLogs;
|
83 | 87 | import org.openqa.selenium.logging.LoggingHandler;
|
84 | 88 | import org.openqa.selenium.logging.Logs;
|
@@ -471,6 +475,76 @@ public Object executeAsyncScript(String script, Object... args) {
|
471 | 475 | return execute(DriverCommand.EXECUTE_ASYNC_SCRIPT(script, convertedArgs)).getValue();
|
472 | 476 | }
|
473 | 477 |
|
| 478 | + @Override |
| 479 | + public ScriptKey pin(String script) { |
| 480 | + UnpinnedScriptKey key = (UnpinnedScriptKey) JavascriptExecutor.super.pin(script); |
| 481 | + String browserName = getCapabilities().getBrowserName().toLowerCase(); |
| 482 | + if ((browserName.equals("chrome") || |
| 483 | + browserName.equals("msedge") || |
| 484 | + browserName.equals("microsoftedge")) && this instanceof HasDevTools) { |
| 485 | + |
| 486 | + ((HasDevTools) this).maybeGetDevTools().ifPresent(devTools -> { |
| 487 | + devTools.createSessionIfThereIsNotOne(); |
| 488 | + devTools.send(new org.openqa.selenium.devtools.Command<>("Page.enable", |
| 489 | + ImmutableMap.of())); |
| 490 | + devTools.send(new org.openqa.selenium.devtools.Command<>("Runtime.evaluate", |
| 491 | + ImmutableMap.of("expression", |
| 492 | + key.creationScript()))); |
| 493 | + Map<String, Object> result = devTools.send(new org.openqa.selenium.devtools.Command<>( |
| 494 | + "Page.addScriptToEvaluateOnNewDocument", |
| 495 | + ImmutableMap.of("source", key.creationScript()), |
| 496 | + new TypeToken<Map<String, Object>>() { |
| 497 | + }.getType())); |
| 498 | + key.setScriptId((String) result.get("identifier")); |
| 499 | + }); |
| 500 | + } |
| 501 | + return key; |
| 502 | + } |
| 503 | + |
| 504 | + @Override |
| 505 | + public void unpin(ScriptKey scriptKey) { |
| 506 | + UnpinnedScriptKey key = (UnpinnedScriptKey) scriptKey; |
| 507 | + |
| 508 | + JavascriptExecutor.super.unpin(key); |
| 509 | + |
| 510 | + String browserName = getCapabilities().getBrowserName().toLowerCase(); |
| 511 | + if ((browserName.equals("chrome") || |
| 512 | + browserName.equals("msedge") || |
| 513 | + browserName.equals("microsoftedge")) && this instanceof HasDevTools) { |
| 514 | + ((HasDevTools) this).maybeGetDevTools().ifPresent(devTools -> { |
| 515 | + devTools.send(new org.openqa.selenium.devtools.Command<>("Page.enable", |
| 516 | + ImmutableMap.of())); |
| 517 | + devTools.send(new org.openqa.selenium.devtools.Command<>( |
| 518 | + "Runtime.evaluate", |
| 519 | + ImmutableMap.of("expression", key.removalScript()))); |
| 520 | + devTools.send(new org.openqa.selenium.devtools.Command<>( |
| 521 | + "Page.removeScriptToEvaluateOnLoad", |
| 522 | + ImmutableMap.of("identifier", key.getScriptId()))); |
| 523 | + }); |
| 524 | + } |
| 525 | + } |
| 526 | + |
| 527 | + @Override |
| 528 | + public Object executeScript(ScriptKey key, Object... args) { |
| 529 | + Require.stateCondition( |
| 530 | + key instanceof UnpinnedScriptKey, |
| 531 | + "Script key should have been generated by this driver"); |
| 532 | + |
| 533 | + if (!getPinnedScripts().contains(key)) { |
| 534 | + throw new JavascriptException("Script is unpinned"); |
| 535 | + } |
| 536 | + |
| 537 | + String browserName = getCapabilities().getBrowserName().toLowerCase(); |
| 538 | + |
| 539 | + if ((browserName.equals("chrome") || |
| 540 | + browserName.equals("msedge") || |
| 541 | + browserName.equals("microsoftedge")) && this instanceof HasDevTools) { |
| 542 | + return executeScript(((UnpinnedScriptKey) key).executionScript(), args); |
| 543 | + } |
| 544 | + |
| 545 | + return executeScript(((UnpinnedScriptKey) key).getScript(), args); |
| 546 | + } |
| 547 | + |
474 | 548 | @Override
|
475 | 549 | public TargetLocator switchTo() {
|
476 | 550 | return new RemoteTargetLocator();
|
|
0 commit comments