当前位置:网站首页>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 .
边栏推荐
- Zero of DC learning notes -- overview and basic process introduction
- 测试必备工具-Postman实战教程
- Visual studio 2019 Download
- Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey
- 微生物安全与健康,什么是生物处理?
- Complete software development process
- Basic knowledge II - Basic definitions related to sta
- 直播商城源码,实现左右联动商品分类页面
- Microbial safety and health, what is biological treatment?
- Relationship between ASCII, Unicode, GBK, UTF-8
猜你喜欢

With regard to the white box test, you have to master these skills~

6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!

45 year old programmer tells you: why do programmers want to change jobs? It's too true

医疗HIS行业短信发送解决方案

One of the basics - overview of sta Basics
![[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大](/img/fa/f9bad44147ba9af21183b7bd630e32.png)
[无线通信基础-14]:图解移动通信技术与应用发展-2-第一代移动模拟通信大哥大
![[fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother](/img/fa/f9bad44147ba9af21183b7bd630e32.png)
[fundamentals of wireless communication-14]: illustrated mobile communication technology and application development-2-the first generation mobile analog communication big brother

Gin configuration file

Digital IC design process summary

软件开发中的上游和下游
随机推荐
未来的 Web3会带来什么?
【队列】933. Number of Recent Calls
如何选择券商?另外,手机开户安全么?
[Qt5 basic \u 1] starting from 0, Mr. Detian will study with you - Introduction to the window
软件测试的可持续发展,必须要学会敲代码?
Factory + strategy mode
使用 C# 创造 ASCII 艺术
Sort custom function
短信在企业中的应用有哪些?
农产品换房?“变相”购房补贴!
Handsontable数据网格组件
Understanding and application of Qt5 layout in creation
测试必备工具-Postman实战教程
Some essential differences
数学知识:求组合数 III—求组合数
物业怎么发短信通知给业主?
哪有什么未来可期,不过是打工人临死前最后的幻想罢了
Microbiological health, why is food microbiological testing important
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
PHP converts two-dimensional array elements into key value pairs