当前位置:网站首页>Selenium classic interview question - multi window switching solution
Selenium classic interview question - multi window switching solution
2022-07-01 01:42:00 【Code classmate software test】
Doing it web End automation test case writing process , Have you ever encountered window switching , For example, the product list page shown in the following screenshot , Click any product and switch to the product details page .

Two windows are superimposed in the browser , The following screenshot shows this situation ;

Or two browser windows open , The following screenshot shows this situation .

How do you write your automated test scripts when these two windows are switched ?

There is still no need to handle window switching separately ? If using selenium The framework writes automated test cases involving window switching , Be sure to handle window switching separately .
So why deal with the situation of new windows separately ? Because we use browser driven objects to access the specified url This corresponds to a window X, When in the window X Perform an operation ( For example, click ) Cause a new page or window Y, In this case selenium No intelligent processing , The initiative is left to the script writer to decide the browser driven object in X still Y Simulate the user's operation behavior on the window .
Then we need to implement how to switch the window to the window we expect , Take the example at the beginning of our article , I need to switch from the product list page to the product details page , It's about 4 Kind of solution , Namely : Traverse to find the expected window and then switch 、 In the process of traversal, first switch the window and then judge the expected window 、 Specify window switching 、 Multiple conditions determine the expected window . Next, I need to solve how to find the unique ID of the window before and after a certain operation , Just like the difference between people is ID card , The product list page appears 、 How to distinguish between the two windows in the case of the product details page ?
Through handle , Each window has a unique identification handle , If interested readers can print out the handle and have a look , In fact, it is a string as a unique identifier .
that selenium frame web In case of automation , How do we get the handle ? adopt getWindowHandle() Method to get the handle of the current page . Next, we try to find the expected window through the first solution traversal and then switch to get , The specific code implementation logic is to obtain the window handle before clicking on a product , Then locate a product element and call the click method , And then call getWindowHandles() Method to get the handle collection of all currently open windows , That is, the handle set of the product list page and the product details page , The next thing to do is to traverse the elements in the handle collection , In the process of traversal, judge whether the current handle is the same as the handle before the click operation , If not, switch to the window corresponding to this handle , Then the reader may ask , What is the method of switching windows ? yes switchTo(). Here is a sample code block for readers .

Readers usually write test cases involving multi window switching , Generally, it can be done in this way . Of course, if you are sure that only a new window will be opened after clicking and other operations , You can also use the specified window to switch this solution , The implementation logic of the code is relatively simple , The difference from scheme 1 is to convert the handle set into an array , Then by specifying the array subscript to achieve window switching , The following is the implementation code block provided for you :

If multiple superimposing operations cause multiple windows to open , You can also use the scheme of switching windows first and then judging the expected windows in the traversal process , The difference between this scheme and the scheme is that window switching is performed first , Then judge whether the current window handle is consistent with the expected window handle , If the same , Then stop traversing , Still give you a reference to the implementation code , As shown below :

The condition to stop switching windows implemented in the above code block is , The window you are currently switching to title Whether the attribute value is the value of my expected window title Property value , If yes, stop switching windows . See here, some readers may ask , If there are multiple windows title Property values are the same , This plan doesn't work ? Yes , But we can try to replace the judgment conditions , For example, determine the window you are currently switching to url And expected url Is it consistent , This judgment is safer , Different windows correspond to url It's usually different , However, if the front-end code is modified when the version is updated url, At this time, this part of the information of our test case also needs to be modified , Don't be depressed. There are other alternative solutions , A judgment condition can be changed from one to more , The above are common solutions and application scenarios for multi window switching , Hope to help our readers .
边栏推荐
- Some essential differences
- 工作6年,来盘点一下职场人混迹职场的黄金法则
- Necessary tools for testing - postman practical tutorial
- 短信在企业中的应用有哪些?
- Ks009 implementation of pet management system based on SSH
- Microbiological health, why is food microbiological testing important
- laravel Carbon 时间处理类使用
- Creating ASCII art with C #
- Factory + strategy mode
- Use strictmode strictmode principle (1)
猜你喜欢
随机推荐
More pragmatic in business
KS009基于SSH实现宠物管理系统
1175. Prime Arrangements
For the sustainable development of software testing, we must learn to knock code?
[stack] 921 Minimum Add to Make Parentheses Valid
System settings large page
Thinking brought by strictmode -strictmode principle (5)
微生物安全與健康,什麼是生物處理?
[simulation] 922 Sort Array By Parity II
Unknown database连接数据库错误
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
[Office PDF] PDF merging and splitting will free us from the functional limitations of paid software, OK
微生物检测,土壤微生物的作用有哪些?
Use of laravel carbon time processing class
Use strictmode strictmode principle (1)
QT5-布局在创作中的理解应用
Ks009 implementation of pet management system based on SSH
PHP crawls data through third-party plug-ins
Mathematical knowledge: 01 sequence satisfying conditions - find combinatorial number
Applet Custom Grid








