SoFunction
Updated on 2024-12-20

Selenium Multi-Window Switching Solution

In the process of writing web-based automated test cases, have you ever encountered the case of window switching, such as the following screenshot shows the product list page, click on any product to switch to the product details page of this piece of the situation.

在这里插入图片描述

In this case, there are two windows stacked on top of each other in the browser, as shown in the following screenshot;

图片

Or it may appear that two browser windows are open, as shown in the following screenshot.

图片

How are people's automated test scripts written for these two window switching scenarios?

Or do you not need to handle window switching separately? If you are writing automated test cases involving window switching using the selenium framework, you definitely need to handle window switching separately.

So why do we need to handle the case of a new window separately? Because the url we access through the browser driver object corresponds to a window X. When an action (such as a click) is performed in window X that results in the opening of a new page or a new window Y, selenium doesn't handle this intelligently and leaves it up to the script writer to decide whether the browser driver object simulates the user's action in window X or Y.

Then we need to realize how to make the window switch to our expected window, or take the example of the beginning of our article, I need to switch from the product list page to the product details page, there are four solutions, respectively: traversal to find the expected window and then switch, traversal of the process of switching the window and then determine the expected window, the specified window switching, more than one condition to determine the expected window. Next you need to solve how to find the execution of an operation before and after the window unique identity, like the difference between people and people is the identity card, there is a list of products page, product details page in the case of the two windows how to distinguish it?

Through the handle, each window has a unique identification handle, if interested readers can print out the handle to see, in fact, is a string as a unique identification of the string.

So selenium framework web automation case, how do we get the handle? Through the getWindowHandle () method to get the handle of the current page. Next we try to traverse through the first solution to find the expected window and then switch to get the specific code implementation logic for the implementation of clicking on a product to get the window handle, and then locate a product element and call the click method, and then call the getWindowHandles () method to get a collection of handles of all the current open windows, that is, the product list page and the product details page of the handle collection. Details page of the collection of handles, the next thing to do is to traverse the collection of handles of the elements, traversal of the process to determine whether the current handle and the click on the handle before the operation is the same, if not the same as the handle to switch to the window corresponding to this handle, then the reader may ask, switch the window of the method is which? It is switchTo(). Here is a sample code block for the readers.

图片

Readers usually write test cases involving multi-window switching, generally use this method can be fixed. Of course, if you are very sure that the implementation of the click and other operations only after the opening of a new window, you can also use the specified window to switch this solution, the code implementation logic is relatively simple, differentiated from the first solution is to handle the collection into an array, and then specify the array subscripts to achieve the window switching, the following is the implementation of the code to provide you with the code block:

图片

If there are multiple stacking operations lead to open multiple windows, you can also use the traversal process to switch the window and then determine the expected window program, the program is different from the program is the first implementation of the window switching, and then determine the current window handle and the expected window handle is the same, if it is the same, then stop traversing, still give you a reference to the implementation of the code, as shown below:

图片

The above block of code to stop switching windows is realized in the condition that the current switch to the window title attribute value is the value of the title attribute of the window I expect, if it is then stop to continue to switch windows. See here some readers may ask, if there are more than one window title attribute value are the same, this program does not work, right? Yes, but we can try to replace the judgment conditions, such as judging the current switch to the window url and the expected url is the same, this judgment is more secure, different windows corresponding to the url is generally different, but if the front-end code version of the update to modify the url, at this time, we need to follow the part of the information in the test case to modify, do not be depressed there are other alternative solutions. Judgment conditions can be one into more than one, the above is a common solution for multi-window switching and application scenarios, I hope to help our readers and friends. More Selenium multi-window switching content, please search my previous posts or continue to browse the following related articles I hope you will support me more in the future!