新聞中心
前 言

發(fā)展壯大離不開廣大客戶長期以來的信賴與支持,我們將始終秉承“誠信為本、服務(wù)至上”的服務(wù)理念,堅持“二合一”的優(yōu)良服務(wù)模式,真誠服務(wù)每家企業(yè),認(rèn)真做好每個細(xì)節(jié),不斷完善自我,成就企業(yè),實現(xiàn)共贏。行業(yè)涉及履帶攪拌車等,在成都網(wǎng)站建設(shè)、成都全網(wǎng)營銷、WAP手機(jī)網(wǎng)站、VI設(shè)計、軟件開發(fā)等項目上具有豐富的設(shè)計經(jīng)驗。
本文主要通過一個簡單的例子,來討論以下兩個問題:
- 使用Selenium對由Ajax動態(tài)加載的頁面進(jìn)行測試
- 測試含有iframe標(biāo)簽的網(wǎng)頁
本文不是Selenium2的簡單介紹或者入門內(nèi)容,目標(biāo)讀者是至少使用過Selenium2進(jìn)行測試的各位朋友。
準(zhǔn)備工作
假設(shè)你有一項業(yè)務(wù),需要在用戶進(jìn)行輸入的時候用Ajax彈出輔助輸入的窗口,然后再將這些值傳回主窗口。
為了敘述簡便,這里使用一個簡單的iframe標(biāo)簽對彈出窗口進(jìn)行簡化。
首先需要兩個網(wǎng)頁,一個是主頁面main.html:
寸木的Selenium+Ajax測試 主頁面
- 下面的區(qū)域是測試用的Frame。
- 接收到的信息:
接著是被彈出的窗口SubPage.html
寸木的Selenium+Ajax測試 子頁面
- Hi, 這里是子頁面,為了和主頁面加以區(qū)別,我被標(biāo)注成了綠色。
關(guān)鍵問題
我們的目標(biāo)是用Java編寫一段能夠自動測試這一完整業(yè)務(wù)邏輯的測試代碼。因此,我們首先想到的可能會是類似下面的代碼
- /**
- *
- */
- package com.cnblogs.www.hexin0614;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- /**
- * @author hexin
- *
- */
- public class TestSa {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // Declare
- WebDriver driver = new FirefoxDriver();
- // Load page
- driver.get("file:///C:/tmp/Main.html");
- Thread.sleep(15000);
- // Load subpage
- driver.findElement(By.id("btnLoad")).click();
- // Input message
- driver.findElement(By.id("frameText")).sendKeys("Message from Selenium.");
- // Send message back to main page
- driver.findElement(By.id("btnSendBack")).click();
- Thread.sleep(2000);
- // Go back to main frame
- System.out.println(driver.findElement(By.id("lblMsg")).getText());
- driver.close();
- }
- }
實際運行的時候會報告找不到"frameText"元素的錯誤。這是怎么回事呢?
原來Selenium2在使用get()方法打開一個網(wǎng)頁的時候,是不會繼續(xù)加載里面的iframe中的內(nèi)容的(這一點與Selenium有所區(qū)別)。
那么,我們就需要人為的要求Selenium2對iframe中的內(nèi)容進(jìn)行加載。
- // Step into the subpage
- driver.switchTo().frame("TestFrame");
這樣就可以找到id為"frameText"的元素了。
重新運行,發(fā)現(xiàn)還是有錯,這一次報告的是"lblMsg"元素找不到?奇怪嗎?
不奇怪,因為我們通過上面的switchTo()方法已經(jīng)切換到了iframe的內(nèi)部,而subpage中是不存在id為"lblMsg"對象的。
怎么辦?只有重新回到Mainpage上來。
根據(jù)Selenium2的在線文檔,有很多方法可以切換回MainPage。(別告訴我你沒有耐心的去讀那些文檔)
筆者經(jīng)過摸索發(fā)現(xiàn)了一個比較簡單的方法:利用getWindowHandle()方法可以快速的進(jìn)行切換。該方法的JavaDoc如下
- 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)部的時候,提前記錄下Mainpage的句柄就可以在將來的任何時候回到主窗口了。于是我們追加下面兩行代碼。
- // Back up main page's handler
- String strMainHandler = driver.getWindowHandle();
- // ........
- // Go back to main frame
- driver.switchTo().window(strMainHandler);
好了,問題解決了,是不是很簡單?***送出完整的Java代碼。
- /*
- * Test
- */
- package com.cnblogs.www.hexin0614;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- /**
- * @author hexin
- *
- */
- public class TestSa {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // Declare
- WebDriver driver = new FirefoxDriver();
- // Load page
- driver.get("file:///C:/tmp/Main.html");
- Thread.sleep(15000);
- // Load subpage
- driver.findElement(By.id("btnLoad")).click();
- // Back up main page's handler
- String strMainHandler = driver.getWindowHandle();
- // Step into the subpage
- driver.switchTo().frame("TestFrame");
- // Input message
- driver.findElement(By.id("frameText")).sendKeys("Message from Selenium.");
- // Send message back to main page
- driver.findElement(By.id("btnSendBack")).click();
- Thread.sleep(2000);
- // Go back to main frame
- driver.switchTo().window(strMainHandler);
- System.out.println(driver.findElement(By.id("lblMsg")).getText());
- driver.close();
- }
- }
網(wǎng)頁標(biāo)題:使用Selenium2測試含有iframe的Ajax網(wǎng)頁
文章路徑:http://www.5511xx.com/article/dhoooic.html


咨詢
建站咨詢
