日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
使用Selenium2測(cè)試含有iframe的Ajax網(wǎng)頁

前  言

發(fā)展壯大離不開廣大客戶長(zhǎng)期以來的信賴與支持,我們將始終秉承“誠信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及履帶攪拌車等,在成都網(wǎng)站建設(shè)成都全網(wǎng)營(yíng)銷、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。

本文主要通過一個(gè)簡(jiǎn)單的例子,來討論以下兩個(gè)問題:

  • 使用Selenium對(duì)由Ajax動(dòng)態(tài)加載的頁面進(jìn)行測(cè)試
  • 測(cè)試含有iframe標(biāo)簽的網(wǎng)頁

本文不是Selenium2的簡(jiǎn)單介紹或者入門內(nèi)容,目標(biāo)讀者是至少使用過Selenium2進(jìn)行測(cè)試的各位朋友。

準(zhǔn)備工作

假設(shè)你有一項(xiàng)業(yè)務(wù),需要在用戶進(jìn)行輸入的時(shí)候用Ajax彈出輔助輸入的窗口,然后再將這些值傳回主窗口。

為了敘述簡(jiǎn)便,這里使用一個(gè)簡(jiǎn)單的iframe標(biāo)簽對(duì)彈出窗口進(jìn)行簡(jiǎn)化。

首先需要兩個(gè)網(wǎng)頁,一個(gè)是主頁面main.html:

 
 
 
  1.     
  2.         寸木的Selenium+Ajax測(cè)試
  3.         
  4.         
  5.     
  6.     
  7.         

    主頁面

  8.         下面的區(qū)域是測(cè)試用的Frame。
  9.          
  •         
  •         接收到的信息:Waiting mesage
  •     
  • 接著是被彈出的窗口SubPage.html

     
     
     
    1.     
    2.         寸木的Selenium+Ajax測(cè)試
    3.         
    4.         
    5.     
    6.     
    7.         

      子頁面

    8.         Hi, 這里是子頁面,為了和主頁面加以區(qū)別,我被標(biāo)注成了綠色。
    9.         
    10.         
    11.     

    關(guān)鍵問題

    我們的目標(biāo)是用Java編寫一段能夠自動(dòng)測(cè)試這一完整業(yè)務(wù)邏輯的測(cè)試代碼。因此,我們首先想到的可能會(huì)是類似下面的代碼

     
     
     
    1. /**
    2.  *
    3.  */
    4. package com.cnblogs.www.hexin0614;
    5.  
    6. import org.openqa.selenium.By;
    7. import org.openqa.selenium.WebDriver;
    8. import org.openqa.selenium.firefox.FirefoxDriver;
    9.  
    10. /**
    11.  * @author hexin
    12.  *
    13.  */
    14. public class TestSa {
    15.      
    16.     /**
    17.      * @param args
    18.      * @throws Exception
    19.      */
    20.     public static void main(String[] args) throws Exception {
    21.          
    22.          
    23.         // Declare
    24.         WebDriver driver = new FirefoxDriver();
    25.          
    26.         // Load page
    27.         driver.get("file:///C:/tmp/Main.html");
    28.          
    29.         Thread.sleep(15000);
    30.          
    31.         // Load subpage
    32.         driver.findElement(By.id("btnLoad")).click();
    33.          
    34.         // Input message
    35.         driver.findElement(By.id("frameText")).sendKeys("Message from Selenium.");
    36.         // Send message back to main page
    37.         driver.findElement(By.id("btnSendBack")).click();
    38.          
    39.         Thread.sleep(2000);
    40.          
    41.         // Go back to main frame
    42.         System.out.println(driver.findElement(By.id("lblMsg")).getText());
    43.          
    44.         driver.close();
    45.     }
    46. }

    實(shí)際運(yùn)行的時(shí)候會(huì)報(bào)告找不到"frameText"元素的錯(cuò)誤。這是怎么回事呢?

    原來Selenium2在使用get()方法打開一個(gè)網(wǎng)頁的時(shí)候,是不會(huì)繼續(xù)加載里面的iframe中的內(nèi)容的(這一點(diǎn)與Selenium有所區(qū)別)。

    那么,我們就需要人為的要求Selenium2對(duì)iframe中的內(nèi)容進(jìn)行加載。

     
     
     
    1. // Step into the subpage
    2. driver.switchTo().frame("TestFrame");

    這樣就可以找到id為"frameText"的元素了。

    重新運(yùn)行,發(fā)現(xiàn)還是有錯(cuò),這一次報(bào)告的是"lblMsg"元素找不到?奇怪嗎?

    不奇怪,因?yàn)槲覀兺ㄟ^上面的switchTo()方法已經(jīng)切換到了iframe的內(nèi)部,而subpage中是不存在id為"lblMsg"對(duì)象的。

    怎么辦?只有重新回到Mainpage上來。

    根據(jù)Selenium2的在線文檔,有很多方法可以切換回MainPage。(別告訴我你沒有耐心的去讀那些文檔)

    筆者經(jīng)過摸索發(fā)現(xiàn)了一個(gè)比較簡(jiǎn)單的方法:利用getWindowHandle()方法可以快速的進(jìn)行切換。該方法的JavaDoc如下

     
     
     
    1. Return an opaque handle to this window that uniquely identifies it within this driver instance.This can be used to switch to this window at a later date

    看來只要在切換至iframe內(nèi)部的時(shí)候,提前記錄下Mainpage的句柄就可以在將來的任何時(shí)候回到主窗口了。于是我們追加下面兩行代碼。

     
     
     
    1. // Back up main page's handler
    2. String strMainHandler = driver.getWindowHandle();
    3.  
    4. // ........
    5.  
    6. // Go back to main frame
    7. driver.switchTo().window(strMainHandler);

    好了,問題解決了,是不是很簡(jiǎn)單?***送出完整的Java代碼。

     
     
     
    1. /*
    2.  * Test
    3.  */
    4. package com.cnblogs.www.hexin0614;
    5.  
    6. import org.openqa.selenium.By;
    7. import org.openqa.selenium.WebDriver;
    8. import org.openqa.selenium.firefox.FirefoxDriver;
    9.  
    10. /**
    11.  * @author hexin
    12.  *
    13.  */
    14. public class TestSa {
    15.      
    16.     /**
    17.      * @param args
    18.      * @throws Exception
    19.      */
    20.     public static void main(String[] args) throws Exception {
    21.          
    22.          
    23.         // Declare
    24.         WebDriver driver = new FirefoxDriver();
    25.          
    26.         // Load page
    27.         driver.get("file:///C:/tmp/Main.html");
    28.          
    29.         Thread.sleep(15000);
    30.          
    31.         // Load subpage
    32.         driver.findElement(By.id("btnLoad")).click();
    33.          
    34.         // Back up main page's handler
    35.         String strMainHandler = driver.getWindowHandle();
    36.         // Step into the subpage
    37.         driver.switchTo().frame("TestFrame");
    38.          
    39.          
    40.         // Input message
    41.         driver.findElement(By.id("frameText")).sendKeys("Message from Selenium.");
    42.         // Send message back to main page
    43.         driver.findElement(By.id("btnSendBack")).click();
    44.          
    45.         Thread.sleep(2000);
    46.          
    47.         // Go back to main frame
    48.         driver.switchTo().window(strMainHandler);
    49.         System.out.println(driver.findElement(By.id("lblMsg")).getText());
    50.          
    51.         driver.close();
    52.     }
    53. }

    網(wǎng)站題目:使用Selenium2測(cè)試含有iframe的Ajax網(wǎng)頁
    鏈接地址:http://www.dlmjj.cn/article/dhoooic.html