赞
踩
前言:selenium是目前最主流的免费、开源web端自动化测试框架。经历了selenium1、selenium2(selenium+webdrive)、selenium3,一共三个版本迭代,目前selenium2应该还有少部分人用,主要用selenium3。因为最新版本的浏览器,比如谷歌、火狐已经不支持selenium2了。
1、工程目录
2、KeyWord类代码(封装seleniumAPI)
- import org.apache.commons.io.FileUtils;
- import org.openqa.selenium.*;
- import org.openqa.selenium.NoSuchElementException;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.interactions.Actions;
- import org.openqa.selenium.safari.SafariDriver;
- import org.openqa.selenium.support.ui.Select;
- import org.testng.Assert;
-
-
- import java.awt.*;
- import java.awt.datatransfer.StringSelection;
- import java.awt.event.KeyEvent;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- import java.util.List;
-
-
- class KeyWord {
-
-
- static WebDriver driver;
- private static int max_sleep_total = 20; // 超时最大值
-
- // 打开浏览器
- static void openBrowser(String browserName) {
- try {
- switch (browserName) {
- case "chrome":
- System.setProperty("webdriver.chrome.driver", Constants.Path_chrome);
- driver = new ChromeDriver();
- // driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //隐式等待
- System.out.println("打开谷歌浏览器");
- break;
- case "firefox":
- if (FileTools.getPropertyOsName().contains("Mac")) {
- System.setProperty("webdriver.firefox.bin", Constants.Path_firefox);
- } else if (FileTools.getPropertyOsName().contains("Windows")) {
- System.setProperty("webdriver.gecko.driver", Constants.Path_firefox);
- }
- driver = new FirefoxDriver();
- //driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); //隐式等待,全局生效,调用findElement方法时没有立刻找到定位元素,程序会每隔一定时间不断尝试判断页面的DOM中是否出现被查找元素,超过设定的等待时间依旧没有找到,则抛出异常:NoSuchfindElementException,代码会继续往下执行
- System.out.println("打开火狐浏览器");
- break;
- case "safari":
- driver = new SafariDriver();
- //driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); //隐式等待
- System.out.println("打开safari浏览器");
- break;
- default:
- RunExcelCase.judge = "fail";
- System.out.println("没有匹配到浏览器");
- snapshot("打开浏览器失败");
- break;
- }
- } catch (Exception e) {
- RunExcelCase.judge = "fail";
- snapshot("打开浏览器失败");
- }
- }
-
- // 浏览器最大化
- static void maxMize() {
- driver.manage().window().maximize();
- System.out.println("浏览器最大化");
-
- }
-
- // 打开网址
- static void gotoUrl(String url) {
- try {
- sleep(2000);
- driver.get(url);
- System.out.println("访问网址" + url);
- } catch (Exception e) {
- RunExcelCase.judge = "fail";
- snapshot("打开网址失败");
- }
- }
-
- //清除元素框内容
- static void clear(By by) {
- boolean Location = false;
- int sleep_total = 0;
- while ((!Location) && (sleep_total <= max_sleep_total)) {
- try {
- driver.findElement(by).clear();
- System.out.println("清除框内容: " + by + "成功");
- Location = true;
- } catch (Exception e) {
- System.out.println("清除框内容: " + by + "失败");
- sleep(2000);
- sleep_total += 2;
- System.out.println("总共已等待 " + sleep_total + " 秒");
- }
- }
- if (!Location) {
- RunExcelCase.judge = "fail";
- System.out.println("最终清除框" + by + "失败");
- snapshot("清除框内容失败:" + by);
- }
- }
-
-
- //根据xpath,在元素框内输入内容
- static void input(By by, String string) {
- boolean Location = false;
- int sleep_total = 0;
- while ((!Location) && (sleep_total <= max_sleep_total)) {
- try {
- driver.findElement(by).clear();
- System.out.println("清除框内容: " + by + "成功");
- driver.findElement(by).sendKeys(string);
- System.out.println("在框: " + by + "输入: " + string + "成功");
- Location = true;
- } catch (Exception e) {
- System.out.println("在框: " + by + "输入: " + string + "时出现异常");
- sleep(2000);
- sleep_total += 2;
- System.out.println("总共已等待 " + sleep_total + " 秒");
- }
- }
- if (!Location) {
- RunExcelCase.judge = "fail";
- System.out.println("最终输入" + by + "页面元素失败:");
- snapshot("输入内容失败");
- }
- }
-
-
- //单击方法
- static void click(By by) {
- boolean Location = false;
- int sleep_total = 0;
- while ((!Location) && (sleep_total <= max_sleep_total)) {
- try {
- driver.findElement(by).click();
- Location = true;
- System.out.println("单击成功:" + by);
- } catch (Exception e) {
- System.out.println("单击成功:" + by);
- sleep(2000);
- sleep_total += 2;
- System.out.println("总共已等待 " + sleep_total + " 秒");
- }
- }
- if (!Location) {
- RunExcelCase.judge = "fail";
- System.out.println("最终单击失败:" + by);
- snapshot("点击失败");
- }
- }
-
- // 双击方法
- static void doubleClick(By by) {
- boolean Location = false;
- int sleep_total = 0;
- while ((!Location) && (sleep_total <= max_sleep_total)) {
- try {
- WebElement inputBox = driver.findElement(by);
- Actions builder = new Actions(driver);// 声明Action对象
- builder.doubleClick(inputBox).build().perform();// 使用doubleClick方法在元素上进行双击操作
- Location = true;
- System.out.println("双击成功:" + by);
- } catch (Exception e) {
- System.out.println("双击失败:" + by + e.getMessage());
- sleep(2000);
- sleep_total += 2;
- System.out.println("总共已等待 " + sleep_total + " 秒");
- }
- }
- if (!Location) {
- RunExcelCase.judge = "fail";
- System.out.println("最终双击失败" + by);
- snapshot("双击失败");
- }
- }
-
- // 关闭浏览器进程
- static void close_browser() {
- try {
- driver.quit();
- System.out.println("关闭浏览器窗口 ");
- } catch (Exception e) {
- RunExcelCase.judge = "fail";
- System.out.println("关闭浏览器时出现异常: " + e.getMessage());
- snapshot("关闭浏览器失败");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。