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}

   

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

    

 

posted on 2016-11-25 10:18  testhua  阅读(8661)  评论(0编辑  收藏  举报