当前位置:网站首页>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 .
边栏推荐
- [deepin] common sets
- For the sustainable development of software testing, we must learn to knock code?
- Open3d point cloud color rendering
- Log4j2 threadcontext log link tracking
- Basic knowledge 3 - standard unit library
- Sun Yuchen told Swiss media Bilan that the bear market will not last long
- Draw some interesting figures with flutter's canvas
- Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
- 【qt5-tab标签精讲】Tab标签及内容分层解析
- System. Csrebot for commandline
猜你喜欢

Understanding and application of Qt5 layout in creation

Zero of DC learning notes -- overview and basic process introduction

小程序中实现excel数据的批量导入

哪有什么未来可期,不过是打工人临死前最后的幻想罢了

Construction and beautification of personal blog

The argument type 'function' can't be assigned to the parameter type 'void function()‘

Unknown database连接数据库错误

微生物健康,食品微生物检测为什么很重要

Necessary tools for testing - postman practical tutorial

使用 C# 创造 ASCII 艺术
随机推荐
测试必备工具-Postman实战教程
Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey
Matlab farthest point sampling (FPS improved version)
1500w播放下还藏着什么热点?B站2个未来趋势你不得错过
Using recyclerreview to show banner is very simple
laravel Carbon 时间处理类使用
php将二维数组元素转为键值对
The personal test is effective, and the JMeter desktop shortcut is quickly created
3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明
gin_ gorm
PHP数组拼接MySQL的in语句
gin_gorm
短信在企业中的应用有哪些?
Sort custom function
electron之坑addon
Why build a personal blog
Strictmode analysis activity leakage -strictmode principle (3)
Visual studio 2019 Download
PHP通过第三方插件爬取数据
Some items of OCR