Home > Mobile >  Print title of 2nd last tab if multiple tabs are open
Print title of 2nd last tab if multiple tabs are open

Time:08-08

I was asked in an interview to print title of 2nd last tab if multiple tabs are open. I tried this later:

LinkedHashSet<String> windows = (LinkedHashSet<String>) driver.getWindowHandles();
ArrayList<String> windowsList = new ArrayList<String>(windows);
int size = windowsList.size();      
driver.switchTo().window(windowsList.get(size-2));
System.out.println(driver.getTitle());

The above one is not working correctly, but the below one is working:

driver.switchTo().window(windowsList.get(2));
System.out.println(driver.getTitle());

It is storing the parent window at 0th index but child windows in reverse order. Can someone explain?

CodePudding user response:

WebDriver specification says:

The order in which the window handles are returned is arbitrary.

Even though RemoteWebDriver internally wraps the response with LinkedHashSet (no idea why) the order of what is coming from the WebDriver itself is not predictable.

I would say that since your browser is under your control you do know which tab you opened in which order hence you could keep some map that would associate a handle with the position of that actual tab.

Then you could pick the proper handle from that map by the position.

CodePudding user response:

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

  • Related