Looking at the getTitle() method

We can perform operations on a particular window by using the switchTo() command first and then verifying the title. If the title of the window matches the one we want, we can start performing operations on that window or we can invoke the switchTo method again. We can iterate through all open windows using the while loop. Apart from this, we can also search for the existence of elements, we can fire up JavaScript events to check that some JavaScript function exists, and so on.

In the condition of the while loop, we utilize the hasNext() method on the iterator object. The hasNext method keeps on fetching values in the iterator object until it reaches the last value.

The following piece of code can be used to switch to the desired window. The code issues a break after finding the window with the desired title:

public void switchWindowByTitle(String expectedTitle){
Set<String> windowHandles = driver.getWindowHandles();
Iterator it = windowHandles.iterator();
while (it.hasNext()){
driver.switchTo(it.next());
if (driver.getTitle().equalsIgnoreCase(expectedTitle)){
break;
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset