Selenium RC For Python:教程5

 处理弹出窗口
在Selenium,弹出窗口是比较棘手的一个问题,下面谈谈利用Python怎么处理弹出窗口。
最简单的方法:创建弹出窗口,然后使用get_all_window_names和select_window方法

 

  1. get_all_window_names(self):  Returns the names of all windows that the browser knows about.
  2. select_window(self,windowID): Selects a popup window using a window locator; once a popup window has been selected, all commands go to that window. To select the main window again, use null as the target.
  3. wait_for_pop_up(self,windowID,timeout): Waits for a popup window to appear and load up.
  4. select_window(self,windowID): Selects a popup window using a window locator; once a popup window has been selected, all commands go to that window. To select the main window again, use null as the target.
  5. close(self): Simulates the user clicking the "close" button in the titlebar of a popup window or tab.
代码
    def test_popup_creation(self):
        sel 
= self.selenium
        sel.open(self.TEST_PAGE_URL)
        sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
        windowNames 
= sel.get_all_window_names()
        
#print windowNames[0]
        
        self.assertEquals(
1, len(windowNames))
        self.assertEquals(
"selenium_main_app_window", windowNames[0])
        
        sel.click(
"link=This link opens a popup")
        sel.wait_for_pop_up(
"popup", self.MAX_WAIT_IN_MS)
        windowNames 
= sel.get_all_window_names()
        
print windowNames
        
        self.assertEquals(
2, len(windowNames))
        self.assertTrue(
"selenium_main_app_window" in windowNames)
        self.assertTrue(
"popup" in windowNames)

关闭弹出窗口,回到最初的窗口

         # close popup

        sel.close()

        

        # select original window

        sel.select_window("null")

代码
    def test_window_selection_closepopup_returntomainwindow(self):
        sel 
= self.selenium
        sel.open(self.TEST_PAGE_URL)
        sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
        self.assertEquals(self.TEST_PAGE_TITLE, sel.get_title())
        
        sel.click(
"link=This link opens a popup")
        sel.wait_for_pop_up(
"popup", self.MAX_WAIT_IN_MS)
        sel.select_window(
"popup")
        self.assertEquals(
"Bit Motif", sel.get_title())
        
        
# close popup
        sel.close()
        
        
# select original window
        sel.select_window("null")
        self.assertEquals(self.TEST_PAGE_TITLE, sel.get_title())

 

 

posted @ 2010-05-26 13:52  小楼  阅读(1662)  评论(0编辑  收藏  举报