Issue -- switch to multiple browser

Hi there,

I am facing issue with switch to windows. The sequence of window order is shuffled which cause failure of my script.

// Parent Window -Below is the window id
[8db7dbd4-ede3-4d64-a4f0-7b255f300393]

// Click to open a popup window -Below is the window id's
[8db7dbd4-ede3-4d64-a4f0-7b255f300393, c6abfc0f-8411-4548-8738-4d00bc9e51c2]

// Click to open a popup window -Below is the window id's
[8db7dbd4-ede3-4d64-a4f0-7b255f300393, c6abfc0f-8411-4548-8738-4d00bc9e51c2, d2832676-2b9a-4e51-a885-a8ac523ece1a]

// Click to open a popup window -Below is the window id's
[8db7dbd4-ede3-4d64-a4f0-7b255f300393, c6abfc0f-8411-4548-8738-4d00bc9e51c2, d2832676-2b9a-4e51-a885-a8ac523ece1a, 9ea58e5e-c799-4463-902b-747169a6a5c1]

// Click to open a popup window -Below is the window id's --issue since sequence of windows id is changed
[8db7dbd4-ede3-4d64-a4f0-7b255f300393, 55d18ac6-07b6-434e-8077-3c753e081e19, c6abfc0f-8411-4548-8738-4d00bc9e51c2, d2832676-2b9a-4e51-a885-a8ac523ece1a, 9ea58e5e-c799-4463-902b-747169a6a5c1]

// Click to open a popup window -Below is the window id's --issue since sequence of windows id is changed
[8db7dbd4-ede3-4d64-a4f0-7b255f300393, 55d18ac6-07b6-434e-8077-3c753e081e19, c6abfc0f-8411-4548-8738-4d00bc9e51c2, d2832676-2b9a-4e51-a885-a8ac523ece1a, c366708f-2773-4a81-8b25-a9ce6be6fd80, 9ea58e5e-c799-4463-902b-747169a6a5c1]

Below is my approach :

// CLICKING ON A BUTTON -->opens a new pop window

WebDriver driver = DriverFactory.getWebDriver()
Set<String> winHandles = driver.getWindowHandles()
ArrayList<String> popupWindow = new ArrayList<String>(winHandles)
System.err.println(popupWindow)
driver.switchTo().window(popupWindow.get(1))

// CLICKING ON A BUTTON -->opens a new pop window
winHandles = driver.getWindowHandles()
popupWindow = new ArrayList<String>(winHandles)
System.err.println(popupWindow)
driver.switchTo().window(popupWindow.get(2))

// CLICKING ON A BUTTON -->opens a new pop window
winHandles = driver.getWindowHandles()
popupWindow = new ArrayList<String>(winHandles)
System.err.println(popupWindow)
driver.switchTo().window(popupWindow.get(3))

Any help would be greatly appreciated.

Is that first block of stuff supposed to be an array of window ids?

@Russ_Thomas @kazurayam

The first block is an arrayList of window ids

I have casted the Set<String> object into a ArrayList<String> of window id’s.

WebDriver driver = DriverFactory.getWebDriver()
Set<String> winHandles = driver.getWindowHandles()
ArrayList<String> popupWindow = new ArrayList<String>(winHandles)
System.err.println(popupWindow) --> output [8db7dbd4-ede3-4d64-a4f0-7b255f300393, c6abfc0f-8411-4548-8738-4d00bc9e51c2]
driver.switchTo().window(popupWindow.get(1))
```

See the following Stackoverflow discussion for your reference:

Solution:
In this case, if we need to shift Selenium’s focus from first window to second window, then subsequently to the third window and traverse back, we can implement a logic to retain all the window handles in the Set and simply compare the window handle values before invoking driver.switchTo().window(win_handle);


Taking this discussion into my account, I have made a test case for demonstration how to find the Window Handle of the new window opened by click operation:

// https://www.baeldung.com/java-set-operations
String setDifference(Set<String> beforeClick, Set<String> afterClick) {
	Set handles = new HashSet(afterClick)
	handles.removeAll(beforeClick)
	if (handles.size() == 0) {
		throw new IllegalArgumentException("no difference found")
	} else if (handles.size() == 1) {
		return handles[0]
	} else {
		throw new IllegalArgumentException("more than 1 handles as difference found")
	}
}

// Parent Window -Below is the window id
Set<String> winHandles0 =
	new HashSet(['8db7dbd4-ede3-4d64-a4f0-7b255f300393'])

// Click to open a popup window -Below is the window id's
Set<String> winHandles1 = 
	new HashSet(['8db7dbd4-ede3-4d64-a4f0-7b255f300393',
					'c6abfc0f-8411-4548-8738-4d00bc9e51c2'])

// Click to open a popup window -Below is the window id's
Set<String> winHandles2 = 
	new HashSet(['8db7dbd4-ede3-4d64-a4f0-7b255f300393',
					'c6abfc0f-8411-4548-8738-4d00bc9e51c2',
					'd2832676-2b9a-4e51-a885-a8ac523ece1a'])

// Click to open a popup window -Below is the window id's
Set<String> winHandles3 = 
	new HashSet(['8db7dbd4-ede3-4d64-a4f0-7b255f300393',
					'c6abfc0f-8411-4548-8738-4d00bc9e51c2',
					'd2832676-2b9a-4e51-a885-a8ac523ece1a',
					'9ea58e5e-c799-4463-902b-747169a6a5c1'])

// Click to open a popup window -Below is the window id's --issue since sequence of windows id is changed
Set<String> winHandles4 = 
	new HashSet(['8db7dbd4-ede3-4d64-a4f0-7b255f300393',
					'55d18ac6-07b6-434e-8077-3c753e081e19',
					'c6abfc0f-8411-4548-8738-4d00bc9e51c2',
					'd2832676-2b9a-4e51-a885-a8ac523ece1a',
					'9ea58e5e-c799-4463-902b-747169a6a5c1'])


println("winHandles0 --> winHandles1 : opened a new window ${setDifference(winHandles0, winHandles1)}")
println("winHandles1 --> winHandles2 : opened a new window ${setDifference(winHandles1, winHandles2)}")
println("winHandles2 --> winHandles3 : opened a new window ${setDifference(winHandles2, winHandles3)}")
println("winHandles3 --> winHandles4 : opened a new window ${setDifference(winHandles3, winHandles4)}")

When I ran this, I got the follwoing output in the Console:

2020-07-30 14:47:50.313 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2020-07-30 14:47:50.316 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC1
winHandles0 --> winHandles1 : opened a new window c6abfc0f-8411-4548-8738-4d00bc9e51c2
winHandles1 --> winHandles2 : opened a new window d2832676-2b9a-4e51-a885-a8ac523ece1a
winHandles2 --> winHandles3 : opened a new window 9ea58e5e-c799-4463-902b-747169a6a5c1
winHandles3 --> winHandles4 : opened a new window 55d18ac6-07b6-434e-8077-3c753e081e19
2020-07-30 14:47:50.706 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC1

The following tutorial for Set operations in Java may be helpful

1 Like

@kazurayam Sincerely appreciate your help. :handshake: