使用Selenium2测试含有iframe的Ajax网页

前  言

发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及履带搅拌车等,在成都网站建设成都全网营销、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。

本文主要通过一个简单的例子,来讨论以下两个问题:

  • 使用Selenium对由Ajax动态加载的页面进行测试
  • 测试含有iframe标签的网页

本文不是Selenium2的简单介绍或者入门内容,目标读者是至少使用过Selenium2进行测试的各位朋友。

准备工作

假设你有一项业务,需要在用户进行输入的时候用Ajax弹出辅助输入的窗口,然后再将这些值传回主窗口。

为了叙述简便,这里使用一个简单的iframe标签对弹出窗口进行简化。

首先需要两个网页,一个是主页面main.html:

 
 
 
  1.     
  2.         寸木的Selenium+Ajax测试
  3.         
  4.         function loadFrame() {
  5.             // 取得DIV对象
  6.             var divElement = document.getElementById("TestDiv");
  7.             // 插入iFrame元素
  8.             divElement.innerHTML = "";
  9.             // 取得动态插入的iFrame元素
  10.             var frameElement = document.getElementById("TestFrame");
  11.              
  12.             frameElement.src = "SubPage.html";
  13.         }
  14.         
  15.         
  16.     
  17.     
  18.         

    主页面

  19.         下面的区域是测试用的Frame。
  20.          
  •         
  •         接收到的信息:Waiting mesage
  •     
  • 接着是被弹出的窗口SubPage.html

     
     
     
    1.     
    2.         寸木的Selenium+Ajax测试
    3.         
    4.             function removeDefaultInfo(webElement) {
    5.                 var strDefalutInfo = "请输入信息...";
    6.                 if(webElement.value == strDefalutInfo) {
    7.                     webElement.value = "";
    8.                 }
    9.             }
    10.             function setDefaultInfo(webElement) {
    11.                 var strDefalutInfo = "请输入信息...";
    12.                 if(webElement.value == "") {
    13.                     webElement.value = strDefalutInfo;
    14.                 }
    15.             }
    16.             function sendToMainPage() {
    17.                 parent.document.getElementById("lblMsg").innerHTML = document.getElementById("frameText").value;
    18.             }
    19.         
    20.         
    21.     
    22.     
    23.         

      子页面

    24.         Hi, 这里是子页面,为了和主页面加以区别,我被标注成了绿色。
    25.         
    26.         
    27.     

    关键问题

    我们的目标是用Java编写一段能够自动测试这一完整业务逻辑的测试代码。因此,我们首先想到的可能会是类似下面的代码

     
     
     
    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. }

    实际运行的时候会报告找不到"frameText"元素的错误。这是怎么回事呢?

    原来Selenium2在使用get()方法打开一个网页的时候,是不会继续加载里面的iframe中的内容的(这一点与Selenium有所区别)。

    那么,我们就需要人为的要求Selenium2对iframe中的内容进行加载。

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

    这样就可以找到id为"frameText"的元素了。

    重新运行,发现还是有错,这一次报告的是"lblMsg"元素找不到?奇怪吗?

    不奇怪,因为我们通过上面的switchTo()方法已经切换到了iframe的内部,而subpage中是不存在id为"lblMsg"对象的。

    怎么办?只有重新回到Mainpage上来。

    根据Selenium2的在线文档,有很多方法可以切换回MainPage。(别告诉我你没有耐心的去读那些文档)

    笔者经过摸索发现了一个比较简单的方法:利用getWindowHandle()方法可以快速的进行切换。该方法的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内部的时候,提前记录下Mainpage的句柄就可以在将来的任何时候回到主窗口了。于是我们追加下面两行代码。

     
     
     
    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);

    好了,问题解决了,是不是很简单?***送出完整的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. }

    分享文章:使用Selenium2测试含有iframe的Ajax网页
    当前地址:http://www.shufengxianlan.com/qtweb/news0/394250.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

    猜你还喜欢下面的内容

    App开发知识

    行业网站建设