selenium2library提供的切换到新窗口的关键字,只有select window,而且也只能根据title、name、url去定位。如下图所示,明显在实际使用中是不够的。

所以这里总结了一下其他的方法。


一、 拓展selenium2library库。

即修改selenium的源码。

1. *\ Python27\Lib\site-packages\Selenium2Library\locators:找到windowmanager.py文件。

打开编辑,增加方法

    def select_by_handle(self, browser, toHandle):
browser.switch_to_window(toHandle) def get_window_handles(self, browser):
return [ window_info[0] for window_info in self._get_window_infos(browser) ] def get_current_window_handle(self, browser):
return browser.get_current_window_handle()

2. *:\Python27\Lib\site-packages\Selenium2Library\keywords:找到_browsermanagement.py文件。

   打开编辑,增加方法

 

    def select_window_by_handle(self, locator=None):
self._window_manager.select_by_handle(self._current_browser(), locator) def get_window_handles(self):
"""Returns and logs handles of all windows known to the browser."""
return self._log_list(self._window_manager.get_window_handles(self._current_browser())) def get_current_window_handle(self):
"""Returns and logs handle of current window known to the browser."""
return self._log_list(self._window_manager.get_current_window_handle(self._current_browser()))

二、编写自己的关键字

使用robotframework:

1. 创建“Index Item From List”关键字

Index Item From List
[Arguments] ${element} @{items}
${index}= Set Variable ${0}
: FOR ${item} IN @{items}
\ Return From Keyword If '${element}'=='${item}' ${index}
Return From Keyword ${-1}

  

2. 创建“get 新窗口”关键字

get新窗口
[Arguments] @{win_all}
@{win_all_curr} Get Window Handles
${idx}= Set Variable ${-1}
: FOR ${win} IN @{win_all_curr}
\ ${idx} Index Item From List ${win} @{win_all}
\ Return From Keyword If ${idx} == ${-1} ${win}
\ Comment Should be True 0 == 1 #No win handle or no new handle
Should Be True ${idx} msg=No Window handles or no new handle

3. 创建“select新窗口/原窗口”关键字

[Arguments]    ${keyword}    ${item}
${win_curr}= Get Current Window Handle
@{win_hds}= Get Window Handles
Run Keyword ${keyword} ${item}
sleep 3
${win_child}= get新窗口 @{win_hds}
Select Window By Handle ${win_child}
[Return] ${win_child} # ${win_curr} |也可以返回这个,就是返回原窗口

  

4. 使用我们写好的关键字吧。

看上面3个关键字,可能有些人会不是很懂,不是很理解到底怎么切换的,所以这里写个调用的方法,方便理解。

102002-随机打开箱包宝贝
click element css=li>a[href='#/list/?category1=热卖箱包']
sleep 3
@{items} Get Webelements css=.item_picture>a[href]
${item}= 随机选择元素赋值 @{items}
select新窗口 click element ${item}

  例子说明:1. 先是打开了一个箱包的新窗口;

2. 第3、4行代码都是随机获得一个href的链接。

3. 最后一行,就是调用我们的“select新窗口”来切换到新打开的“href”的窗口。

里面的逻辑,把代码带入看,

 select新窗口    click element    ${item}带入看

${win_curr}= Get Current Window Handle #获得当前窗口
    @{win_hds}= Get Window Handles    #获得当前所有的窗口
    Run Keyword  click element ${item}  #带入后,这里就是打开我们的href新窗口
    sleep 3
    ${win_child}= get新窗口 @{win_hds} #get新窗口会获取打开href新窗口后的所有窗口,会比@{win_hds}多这么一个href。这样就可以循环得到它了。
    Select Window By Handle ${win_child}
    [Return] ${win_child}

本章大致结束,后面有新的更好的,回慢慢补充。

    

robotframwork的WEB功能测试(一)—切换window窗口的更多相关文章

  1. 使用MVVM DataTriggers在WPF XAML视图之间切换/Window窗口自适应内容大小并居中

    原文 使用MVVM DataTriggers在WPF XAML视图之间切换 相关文章: http://www.technical-recipes.com/2016/switching-between- ...

  2. robotframwork的WEB功能测试(二)—登录

    小结一下截止到目前,我接触的系统的登录模拟. 1. 带token的session:这种用抓包工具很容易抓到,使用这个链接就可以模拟已登录. 2. 使用cookie:有的系统是判断cookie来判断是否 ...

  3. web自动化之浏览器的窗口切换

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  4. Window 窗口类

    窗口类 WNDCLASS 总结 总结为下面的几个问题: . 什么是窗口类 . 窗口类的三种类型 . 窗口类各字段含义 . 窗口类的注册和注销 . 如何使用窗口类,子类化.超类化是什么 下面分别描述: ...

  5. 【转载】Window 窗口层次关系

    相信在Window 下面编程的很多兄弟们都不是很清楚Window 中窗口的层次关系是怎么样的,这个东西很久已经研究过一下,后来又忘记了,今天又一次遇到了这个问题,所以便整理一下.下面就说说Window ...

  6. [转载]ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  7. JavaScript:window窗口对象

    在JavaScript中,window表示的就是一个窗口对象.所以在整个处理过程之中,所有的操作都是以弹框为主 的.范例1:使用警告框 <script type="text/javas ...

  8. (转载)JavaScript中的Window窗口对象

    (转载)http://www.ijavascript.cn/jiaocheng/javascript-window-65.html 例子: <html> <head> < ...

  9. iOS切换window根控制器 (转)

    转自linfengwenyou 在运行过程中更改根控制器的方法:(假设:A为当前根控制器,B为要设的根控制器) 方法一: 1. appdelegate.m中 self.window = [[UIWin ...

随机推荐

  1. http_proxy_module 模块(proxy_pass 指令)

    1. proxy_pass 指令介绍 该指令属于 http_proxy_module, http_proxy_module 模块可以将请求转发到另一台服务器. 在 nginx 反向代理是,会通过 lo ...

  2. OpenStack IceHouse 部署 - 1 - 架构说明

    参考架构 Architecture from OpenStack Install Guide Reference Architecture Network Isolation 在本次部署中,我们采用了 ...

  3. Thymeleaf学习记录(5)--运算及表单

    Thymeleaf文本及预算: 字面 文本文字:'one text','Another one!',... 号码文字:0,34,3.0,12.3,... 布尔文字:true,false 空字面: nu ...

  4. python中logging日志基本用法,和进程安全问题

    低配版 import logging logging.debug('debug message') # 调试模式 logging.info('info message') # 正常运转模式 loggi ...

  5. css3 伪元素和伪类选择器详解

    转自脚本之家:http://www.jb51.net/css/213779.html 无论是伪类还是伪元素,都属于CSS选择器的范畴.所以它们的定义可以在CSS标准的选择器章节找到.分别是 CSS2. ...

  6. is_array判断是否为数组

    if(is_array($arr)){ echo "是数组"; }else{ echo "不是数组"; }

  7. Angular面试题三

    十六.一个 angular 应用应当如何良好地分层? 目录结构的划分 1.对于小型项目,可以按照文件类型组织,比如: css Js  {  controllers  models  services ...

  8. Ubuntu,忘记了root密码,怎么重置?

    进入单用户模式: 1.开机到grub时,用上下键移到第二行的恢复模式,按e(注意不是回车) 即Ubuntu,With Linux 3.2.0-23-generic(recovery mode) 2.把 ...

  9. 微信小程序-04-详解介绍.json 配置文件

    致我自己:小程序开发不是简单一两天的事,一两天只能算是了解,有时候看多了会烦,感觉很熟悉了,其实只是对表面进行了解,对编程却知之甚少,小程序开发不是简单的改模板,一两天很多部分改模板可能都做不到,坚持 ...

  10. Angular架构概览(截取部分)

    原文链接:https://angular.cn/docs/ts/latest/guide/architecture.html 我们是这样写 Angular 应用的:用 Angular 扩展语法编写 H ...