Skip to content

[🐛 Bug]: DevTools Javascript Pin Does not work on new Tab or Window in same browser session. #11573

@Shivshive

Description

@Shivshive

What happened?

I pined a sample javascript to a browser session on win1 as -> devTools.getDomains().javascript().pin("alert_script", alertScript);

where as the alert_script is =>
String alertScript = """

			function showAlert(msg){
				alert(msg);
			}
			
			function showLog(msg){
				console.log(msg);
			}
			
			""";

When I am calling this showAlert function using Javascript executor as given below it works fine on 1st tab, but when trying to use the same function on the next tab as given in the script below it show showAlert function does not present.

I tried creating the devTools session again using devTools.createSession(driver.getWindowHandle()); but still it does not work.

Sample Code >>

@Test(enabled = true)
public void amazonTestWithHightlight() throws InterruptedException {
	
	final String URL = "https://www.amazon.in";
	
	WebDriver driver = new ChromeDriver();
	
	DevTools devTools = ((ChromeDriver)driver).getDevTools();
	devTools.createSession();

	this.pinJavaScriptToDevTool(devTools);
	
	driver.navigate().to(URL);
	
	enterText(driver, By.cssSelector("[id=\"twotabsearchtextbox\"]"), "macbook");
	click(driver, By.cssSelector("[id=\"nav-search-submit-button\"]"));
	click(driver, By.xpath("(//span[contains(text(),'2020 Apple MacBook Air Laptop: Apple M1 chip, 13.3-inch/33.74 cm Retina Display, 8GB RAM, 256GB SSD Storage, Backlit Keyboard, FaceTime HD Camera, Touch ID. ')])[1]"));
	
	Thread.sleep(1000);
	String current = driver.getWindowHandle();
	String newwin = driver.getWindowHandles().stream().filter(h -> !(h.equalsIgnoreCase(current))).collect(Collectors.joining());
	
	driver.switchTo().window(newwin);
	Thread.sleep(3000);
	
	devTools.createSession(driver.getWindowHandle());
	this.pinJavaScriptToDevTool(devTools);
	
	Thread.sleep(3000);
	click(driver, By.cssSelector("div#imgTagWrapperId"));
	Thread.sleep(5000);
	driver.quit();
}

public void pinJavaScriptToDevTool(DevTools devTools) {
		
	String alertScript = """
			
			function showAlert(msg){
				alert(msg);
			}
			
			function showLog(msg){
				console.log(msg);
			}
			
			""";
	devTools.getDomains().javascript().pin("alert_script", alertScript);
}

	public void click(WebDriver driver, By locator) throws InterruptedException {
	WebElement ele = this.waitForElement(driver, locator);
	Thread.sleep(2000);
	showAlert("Clicking button", driver);
	driver.switchTo().alert().accept();
	ele.click();
	
}

public void enterText(WebDriver driver, By locator, String text) throws InterruptedException {
	WebElement ele = this.waitForElement(driver, locator);
	Thread.sleep(2000);
	showAlert("entering text"+text, driver);
	driver.switchTo().alert().accept();
	ele.sendKeys(text);
}

public void showAlert(String msg, WebDriver driver) {
	((JavascriptExecutor)driver).executeScript("showAlert(arguments[0])", msg);
}

How can we reproduce the issue?

Run the Code given above in description. Would be able to reproduce it.

Relevant log output

Log Output
FAILED: amazonTestWithHightlight
org.openqa.selenium.JavascriptException: javascript error: showAlert is not defined
  (Session info: chrome=109.0.5414.75)
Build info: version: '4.7.0', revision: '0a5b49d16f'
System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.4.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [a2f606ad7ca6579f8ca94069d1a13ec0, executeScript {script=showAlert(arguments[0]), args=[Clicking button]}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.75, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: C:\Users\CHANDR~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:13935}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:13935/devtoo..., se:cdpVersion: 109.0.5414.75, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: a2f606ad7ca6579f8ca94069d1a13ec0

Operating System

Window 11

Selenium version

4.7.0

What are the browser(s) and version(s) where you see this issue?

109

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 109

Are you using Selenium Grid?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-devtoolsIncludes everything BiDi or Chrome DevTools relatedC-javaJava BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions