selenium自动化测试框架-选择窗口篇

本文介绍了Selenium中处理多窗口的技巧,特别是如何通过selectWindow命令来切换窗口,包括使用window.open方法打开新窗口的情况及如何注册这些窗口。还探讨了在特定条件下打开窗口时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.selectWindow问题。

selectWindowTestOpenWindow

如果程序打开了新窗口,那么就要转到新窗口 执行一些操作,这时需要首先选中该新窗口,在selenium中选中新窗口使用selectWindow命令,根据windowID来选择窗口,但是它有一些限制:

a. 只能检测到使用window.open(url, windowName, windowFeatures, replaceFlag)方法打开的窗口。

b. 如果在onLoad事件之前发生的打开窗口,并不能检测到。使用以下命令注册窗口后就能选择。

openWindow TestOpenWindow

源代码如下:

 

<!--File: TestOpenWindowDuringOnLoad.html-->
<html>
<head>
  
<title>Test Local</title>
</head>
<body>

<table cellpadding="1" cellspacing="1" border="1">
  
<tbody>
    
<tr>
      
<td rowspan="1" colspan="3">Test Local<br>
      
</td>
    
</tr>
    
<tr>
        
<td>open</td>
            
<td>http://10.202.15.118:8080/myapp/test_open_window_during_onload.jsp</td>
      
<td>&nbsp;</td>
    
</tr>  
    
<tr>
      
<td>openWindow</td>
            
<td></td>
      
<td>TestOpenWindowDuringOnLoad</td>
    
</tr>
    
<tr>
      
<td>waitForPopUp</td>
            
<td>TestOpenWindowDuringOnLoad</td>
      
<td>5000</td>
    
</tr>
    
<tr>
      
<td>selectWindow</td>
            
<td>TestOpenWindowDuringOnLoad</td>
      
<td>&nbsp;</td>
    
</tr>
    
<tr>
      
<td>verifyTitle</td>
            
<td>Test HTML</td>
      
<td>&nbsp;</td>
    
</tr>

  
</tbody>
</table>
</body>
</html>

<!--File: test_open_window_during_onload.jsp-->
<html>
<head>
  <title>Test Open Window</title>
</head>
<body>
<center>
<text id=text1>Hello, Open a new Window During OnLoad!</text>
</center>
</body>
</html>
<script language=javascript>
 myLoad();
 function myLoad(){
  window.open("test.html","TestOpenWindowDuringOnLoad");
 }
</script>

 


 

c.如果使用window.open(url),未指定窗口ID,那么使用undefined标志窗口ID来选择窗口。

selectWindowundefined

源代码如下:

 

<!--File: TestOpenWindowWithoutID.html-->
<html>
<head>
  
<title>Test Local</title>
</head>
<body>

<table cellpadding="1" cellspacing="1" border="1">
  
<tbody>
    
<tr>
      
<td rowspan="1" colspan="3">Test Local<br>
      
</td>
    
</tr>
    
<tr>
        
<td>open</td>
            
<td>http://10.202.15.118:8080/myapp/test_open_window_without_id.jsp</td>
      
<td>&nbsp;</td>
    
</tr>  
    
<tr>
      
<td>click</td>
            
<td>testopenwindow</td>
      
<td>&nbsp;</td>
    
</tr>
    
<tr>
      
<td>waitForPopUp</td>
            
<td>undefined</td>
      
<td>5000</td>
    
</tr>
    
<tr>
      
<td>selectWindow</td>
            
<td>undefined</td>
      
<td>&nbsp;</td>
    
</tr>
    
<tr>
      
<td>verifyTitle</td>
            
<td>Test HTML</td>
      
<td>&nbsp;</td>
    
</tr>

  
</tbody>
</table>
</body>
</html>
<!--File: test_open_window_without_id.jsp-->
 <html>
 <head>
   <title>Test Open Window</title>
 </head>
 <body>
 <center>
 <text id=text1>Hello, Open a new Window Without ID!</text>
 <button id="testopenwindow" onclick='javascript:myLoad()'>TestOpenWindowWithoutID</button>
 </center>
 </body>
 </html>
 <script language=javascript>
  function myLoad(){
   window.open("test.html");
  }
 </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值