selenium测试(Java)--多表单切换(十二)

采用下面的例子来编写用例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
 5 <title>frame switch</title>
 6 <link
 7     href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
 8     rel="stylesheet" />
 9 </head>
10 <body>
11     <div class="row-fluid">
12         <div class="span10 well">
13             <h3>frame</h3>
14             <iframe id="if" name="nf" src="http://www.baidu.com" width="800" height="300"></iframe>
15         </div>
16     </div>
17 </body>
18 <script
19     src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
20 </html>

代码:

  1 package com.test.frame;
  2 
  3 import org.openqa.selenium.By;
  4 import org.openqa.selenium.JavascriptExecutor;
  5 import org.openqa.selenium.NoSuchElementException;
  6 import org.openqa.selenium.WebDriver;
  7 import org.openqa.selenium.firefox.FirefoxDriver;
  8 import org.openqa.selenium.support.ui.ExpectedConditions;
  9 
 10 public class frameSwitch {
 11 
 12     public static void main(String[] args) {
 13         WebDriver driver = new FirefoxDriver();
 14         driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/frame/frame.html");
 15         driver.manage().window().maximize();
 16 
 17         // 利用id来切换
 18         waitTime(2000);
 19         driver.switchTo().frame("if");
 20 
 21         waitTime(2000);
 22         if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
 23             driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
 24             // 切换到父frame
 25             driver.switchTo().parentFrame();
 26             try {
 27                 driver.findElement(By.xpath("//*[@id='kw']"));
 28             } catch (NoSuchElementException e) {
 29                 String js = "alert(\"switch to parent\")";
 30                 ((JavascriptExecutor) driver).executeScript(js);
 31                 waitTime(3000);
 32                 driver.switchTo().alert().dismiss();
 33             }
 34         }
 35 
 36         // 利用name来切换
 37         waitTime(2000);
 38         driver.switchTo().frame("nf");
 39 
 40         waitTime(2000);
 41         if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
 42             driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
 43             driver.switchTo().parentFrame();
 44             try {
 45                 driver.findElement(By.xpath("//*[@id='kw']"));
 46             } catch (NoSuchElementException e) {
 47                 String js = "alert(\"switch to parent\")";
 48                 ((JavascriptExecutor) driver).executeScript(js);
 49                 waitTime(3000);
 50                 driver.switchTo().alert().dismiss();
 51             }
 52         }
 53 
 54         // 利用定位元素来切换-xpath
 55         waitTime(2000);
 56         driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='if']")));
 57 
 58         waitTime(2000);
 59         if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
 60             driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
 61             driver.switchTo().parentFrame();
 62             try {
 63                 driver.findElement(By.xpath("//*[@id='kw']"));
 64             } catch (NoSuchElementException e) {
 65                 String js = "alert(\"switch to parent\")";
 66                 ((JavascriptExecutor) driver).executeScript(js);
 67                 waitTime(3000);
 68                 driver.switchTo().alert().dismiss();
 69             }
 70         }
 71 
 72         // 利用定位元素来切换-css
 73         waitTime(2000);
 74         driver.switchTo().frame(driver.findElement(By.cssSelector("#if")));
 75 
 76         waitTime(2000);
 77         if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
 78             driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
 79             driver.switchTo().parentFrame();
 80             try {
 81                 driver.findElement(By.xpath("//*[@id='kw']"));
 82             } catch (NoSuchElementException e) {
 83                 String js = "alert(\"switch to parent\")";
 84                 ((JavascriptExecutor) driver).executeScript(js);
 85                 waitTime(3000);
 86                 driver.switchTo().alert().dismiss();
 87                 driver.quit();
 88             }
 89         }
 90     }
 91 
 92     static public void waitTime(int time) {
 93 
 94         try {
 95             Thread.sleep(time);
 96         } catch (InterruptedException e) {
 97             // TODO Auto-generated catch block
 98             e.printStackTrace();
 99         }
100     }
101 
102 }

 

posted @ 2016-07-16 09:19  月色深潭  阅读(1870)  评论(0编辑  收藏  举报