1. 程式人生 > >selenium測試(Java)--多表單切換(十二)

selenium測試(Java)--多表單切換(十二)

generate ati nqa void char support 編寫 generated key

采用下面的例子來編寫用例

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>frame switch</title>
<link
    href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
    rel="stylesheet" />
</head>
<body>
    <div class
="row-fluid"> <div class="span10 well"> <h3>frame</h3> <iframe id="if" name="nf" src="http://www.baidu.com" width="800" height="300"></iframe> </div> </div> </body> <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script> </html>

技術分享

代碼:

package com.test.frame;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class frameSwitch { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/frame/frame.html"); driver.manage().window().maximize(); // 利用id來切換 waitTime(2000); driver.switchTo().frame("if"); waitTime(2000); if (driver.findElement(By.xpath("//*[@id=‘kw‘]")).isDisplayed()) { driver.findElement(By.xpath("//*[@id=‘kw‘]")).sendKeys("switch successfully"); // 切換到父frame driver.switchTo().parentFrame(); try { driver.findElement(By.xpath("//*[@id=‘kw‘]")); } catch (NoSuchElementException e) { String js = "alert(\"switch to parent\")"; ((JavascriptExecutor) driver).executeScript(js); waitTime(3000); driver.switchTo().alert().dismiss(); } } // 利用name來切換 waitTime(2000); driver.switchTo().frame("nf"); waitTime(2000); if (driver.findElement(By.xpath("//*[@id=‘kw‘]")).isDisplayed()) { driver.findElement(By.xpath("//*[@id=‘kw‘]")).sendKeys("switch successfully"); driver.switchTo().parentFrame(); try { driver.findElement(By.xpath("//*[@id=‘kw‘]")); } catch (NoSuchElementException e) { String js = "alert(\"switch to parent\")"; ((JavascriptExecutor) driver).executeScript(js); waitTime(3000); driver.switchTo().alert().dismiss(); } } // 利用定位元素來切換-xpath waitTime(2000); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id=‘if‘]"))); waitTime(2000); if (driver.findElement(By.xpath("//*[@id=‘kw‘]")).isDisplayed()) { driver.findElement(By.xpath("//*[@id=‘kw‘]")).sendKeys("switch successfully"); driver.switchTo().parentFrame(); try { driver.findElement(By.xpath("//*[@id=‘kw‘]")); } catch (NoSuchElementException e) { String js = "alert(\"switch to parent\")"; ((JavascriptExecutor) driver).executeScript(js); waitTime(3000); driver.switchTo().alert().dismiss(); } } // 利用定位元素來切換-css waitTime(2000); driver.switchTo().frame(driver.findElement(By.cssSelector("#if"))); waitTime(2000); if (driver.findElement(By.xpath("//*[@id=‘kw‘]")).isDisplayed()) { driver.findElement(By.xpath("//*[@id=‘kw‘]")).sendKeys("switch successfully"); driver.switchTo().parentFrame(); try { driver.findElement(By.xpath("//*[@id=‘kw‘]")); } catch (NoSuchElementException e) { String js = "alert(\"switch to parent\")"; ((JavascriptExecutor) driver).executeScript(js); waitTime(3000); driver.switchTo().alert().dismiss(); driver.quit(); } } } static public void waitTime(int time) { try { Thread.sleep(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

selenium測試(Java)--多表單切換(十二)