Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Shivshive opened this issue Jan 20, 2023 · 6 comments · Fixed by #11598
Labels
C-devtools BiDi or Chrome DevTools related issues C-java I-defect

Comments

@Shivshive
Copy link

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

@github-actions
Copy link

@Shivshive, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@pujagani
Copy link
Contributor

The DevTools session is created by default for a 'page' target. So all the commands issued, including the ones related to script pinning will only work for that particular tab. To create a DevTools session for a particular window handle (tab or window), please use the overloaded method

public void createSession(String windowHandle) {
and use script pinning methods. Thank you!

@Shivshive
Copy link
Author

That is what the issue is that selenium/java/src/org/openqa/selenium/devtools/DevTools.java method does not work. Even after creating a session with it and pining the javascript again, the pinned javascript doesnot work in new tab or window. As you can see from the code given in the description for same.

image

@pujagani
Copy link
Contributor

Apologies. I will reopen the issue. Though the code example shared in the description is not complete, I was able to use a simpler example and recreate the issue. Thank you for bringing this to notice! I will triage it further now.

@pujagani pujagani reopened this Jan 30, 2023
@pujagani
Copy link
Contributor

This happens because we keep a track of pinned script per DevTools instance. So when we pin the same script again, it does not execute the commands to pin it, it returns the already pinned script's id. I think this needs to be extended to handle multiple CDP sessions within the same DevTools instance. I will try to fix this.

@pujagani
Copy link
Contributor

The fix for this is landed. It will be available in the next release.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C-devtools BiDi or Chrome DevTools related issues C-java I-defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants