selenium自动化测试

来源:互联网 发布:西安理工大学知行 编辑:程序博客网 时间:2024/06/06 09:59

自动化测试主要依赖于浏览器驱动实现。这里以IE为例。

package seleniumTest;import java.io.File;import java.io.IOException;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.ie.InternetExplorerDriver;import org.openqa.selenium.remote.Augmenter;import org.apache.commons.io.FileUtils;import org.openqa.selenium.OutputType;import org.openqa.selenium.TakesScreenshot;public class Selenium {static String  dir="C:\\";/** * 主程序 * * @param args * @throws InterruptedException * @throws IOException */public static void main(String[] args) throws Exception {//设置驱动System.setProperty("webdriver.ie.driver", "C:/Program Files/Internet Explorer/IEDriverServer.exe");WebDriverdriver = new InternetExplorerDriver();// 隐式等待10S(10秒内响应后直接运行,不响应报错)driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// 窗口最大化driver.manage().window().maximize();//进入要测试的网址driver.get("http://www.cnblogs.com/tobecrazy/p/3599568.html");takeScreenShotIE(driver, "123");//点击按钮1driver.findElement(By.id("button")).click();//清空text框1driver.findElement(By.id("text")).clear();//向text框中输入值driver.findElement(By.id("text")).sendKeys("123");//切换进当前画面的frame1driver.switchTo().frame("frame1");//切换进frame1里面的frame2driver.switchTo().frame("frame2");//切换至原始画面driver.switchTo().defaultContent();//切换进当前画面的frame3driver.switchTo().frame("frame3");}//IE截图public static void takeScreenShotIE(WebDriver driver, String name){  WebDriver augmentedDriver = new Augmenter().augment(driver);  File output = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);  File file = new File(dir, name + ".png");  try {      FileUtils.copyFile(output, file);  }catch (IOException e) {      e.printStackTrace();  }}//谷歌和火狐截图public static void takeScreenShotChromeFirefox(WebDriver driver, String name){  File output = null;  File file;  output = ((TakesScreenshot) driver)                  .getScreenshotAs(OutputType.FILE);  file = new File(dir, name + ".png");  try {      FileUtils.copyFile(output, file);  } catch (IOException e) {      e.printStackTrace();  }}}

自动化测试获取元素和操作元素的方式和js相似。

如果有嵌套三层iframe,要回到主画面,再一个一个进。

JAVA的selenium不支持回到父画面。

资源下载地址:http://download.csdn.net/detail/qq_26508409/9925820(不包括驱动)