Could you please provide more valid information? Such as your error report or error log, otherwise I can't better understand the problem you are trying to understand, so I tried it myself, I hope it will help you. Here is the code I constructed with reference to your ideas:
public static void main(String[] args) {
TestDriver driver=new TestDriver("null title");
LinkedHashSet<String> windows = (LinkedHashSet<String>)
driver.getWindowHandles();
ArrayList<String> windowsList = new ArrayList<>(windows);
int size = windowsList.size();
String newTitle = windowsList.get(size - 2);
driver.switchTo().window(newTitle);
System.out.println("driver title:"+driver.getTitle());
System.out.println("window title:"+driver.switchTo().getTitle());
}
TestDriver:
class TestDriver{
public String title;
private TestWindow testWindow;
public TestDriver(String title) {
this.title = title;
}
public TestDriver(String title, TestWindow testWindow) {
this.title = title;
this.testWindow = testWindow;
}
public TestDriver(TestWindow testWindow) {
this.title=testWindow.title;
this.testWindow=testWindow;
}
public HashSet<String> getWindowHandles(){
LinkedHashSet<String> retLinkedHashSet = new LinkedHashSet<>(15);
for (int i = 0; i <15 ; i++) {
retLinkedHashSet.add(i+"");
}
return retLinkedHashSet;
}
/**
* switch
* For convenience, I don't use design patterns.
* @return
*/
public TestWindow switchTo() {
return new TestWindow(this);
}
public void window(String title){
this.title=title;
}
public String getTitle() {
return title;
}
}
TestWindow:
class TestWindow{
public String title;
private TestDriver testDriver;
public TestWindow(String title) {
this.title = title;
}
public TestWindow(TestDriver testDriver) {
this.title=testDriver.title;
this.testDriver = testDriver;
}
public TestDriver switchTo() {
return new TestDriver(this);
}
public void window(String title){
this.title=title;
}
public String getTitle() {
return title;
}
}
This is the first to use your idea, and the final output is as expected.
<div align=center><img src="https://i.stack.imgur.com/7tu2q.png"></div>
Next I'll give you the slightly tweaked code:
estDriver driver=new TestDriver("null title");
LinkedHashSet<String> windows = (LinkedHashSet<String>)
driver.getWindowHandles();
ArrayList<String> windowsList = new ArrayList<>(windows);
int size = windowsList.size();
/* String newTitle = windowsList.get(size - 2);
driver.switchTo().window(newTitle);*/
/**
* In the conversion I always create a new object for assignment.
* split a method,you can got a right question.
*/
String newTitle = windowsList.get(size - 2);
TestWindow testWindow = driver.switchTo();
System.out.println("driver title:"+driver.getTitle());
System.out.println("window title:"+driver.switchTo().getTitle());
}
}
Then you will find:
<div align=center><img src="https://i.stack.imgur.com/frIw0.png"></div>
So the real thing that might get you wrong might be in @link{
In method:switchTo()}, if your transition uses a newly created class, you cannot call it again when you finally output the result, which will cause your result to reset.
The information you gave may be too little for me and I may not be able to actually recover your mistakes, hope this helps.
enter image description here
enter image description here