您的位置:首页 > 其它

WebDriverUtil

2016-03-10 16:05 295 查看
介绍一个火狐的插件firepath

我做的自动化测试是在火狐上运行的,因为我找到一个对于自动化测试比较有帮助的插件,那就是firepath,具体用法,就是先安装该插件,它会在firebug那么调试的窗口最右边出现。firepath截图和firepath安装后的截图如下所示。(优点:点哪个元素,哪个元素的xpath路径立马显示,看图吧)

package com.cyyun.weibogettoken;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

import org.openqa.selenium.By;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

/**
*
* WebDriver帮助类
*/

public class WebDriverUtil {

/**
*
* 写在前面的话:
*
* 我写的这个WebDriver帮助类法仅仅针对于xpath访问的
*
* 为什么这么写呢?有两点理由
*
* 其一:xpath获取方便,我用的是firefox浏览器,只要用firepath这个插件,我们就可以正确的定位到每一个节点,
* 并且firepath支持查询功能,值得大家使用
*
* 其二:使用统一的xpath,给编码带来了一定的规范
*/
/**
*
* 没有验证码的的登录
*
* @param wd
*            WebDriver对象
*
* @param unameXpath
*            用户名的xpath路径
*
* @param uname
*            用户名
*
* @param pwdXpath
*            密码xpath路径
*
* @param pwdValue
*            密码
*
* @param loginBtnXpath
*            登录按钮xpath
*/

public static void login(WebDriver wd, String url, String unameXpath,
String uname, String pwdXpath, String pwd, String loginBtnXpath) {

wd.get(url);

inputs(wd, unameXpath, uname);

inputs(wd, pwdXpath, pwd);

click(wd, loginBtnXpath);

}

/**
*
* 登录可能放在一个frame里了:我是因为遇到过,所以才加了个方法的
*
* @param wd
*            WebDriver对象
*
* @param unameXpath
*            用户名的xpath路径
*
* @param uname
*            用户名
*
* @param pwdXpath
*            密码xpath路径
*
* @param pwdValue
*            密码
*
* @param loginBtnXpath
*            登录按钮xpath
*
* @param frame
*            第几个框架
*/

public static void loginFrame(WebDriver wd, String url, String unameXpath,
String uname, String pwdXpath, String pwd, String loginBtnXpath,
int frame) {

wd.get(url);

wd.switchTo().frame(frame);

inputs(wd, unameXpath, uname);

inputs(wd, pwdXpath, pwd);

click(wd, loginBtnXpath);

}

/**
*
* 有验证码的登录
*
* @param wd
*            WebDriver对象
*
* @param unameXpath
*            用户名的xpath路径
*
* @param uname
*            用户名
*
* @param pwdXpath
*            密码xpath路径
*
* @param pwdValue
*            密码
*
* @param loginBtnXpath
*            登录按钮xpath
*
* @param seconds
*            输入验证码的间隔
*/

public static void loginVerify(WebDriver wd, String url, String unameXpath,
String uname, String pwdXpath, String pwd, String loginBtnXpath,
int seconds) {

wd.get(url);

inputs(wd, unameXpath, uname);

inputs(wd, pwdXpath, pwd);

try {

Thread.sleep(seconds * 1000); // 这段时间内请输入验证码

} catch (InterruptedException e) {

e.printStackTrace();

}

click(wd, loginBtnXpath);

}

/**
*
* 获取页面单个元素
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static WebElement getElement(WebDriver wd, String xpath) {

return wd.findElement(By.xpath(xpath));

}

/**
*
* 获取页面的一组元素
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static List<WebElement> getElements(WebDriver wd, String xpath) {

return wd.findElements(By.xpath(xpath));

}

/**
*
* 获取元素节点的文本值
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static String getText(WebDriver wd, String xpath) {

return wd.findElement(By.xpath(xpath)).getText();

}

/**
*
* 获取元素节点的文本值
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return 没有找到该元素时会有个提示,并且不会报错,建议使用
*/

public static String getExistText(WebDriver wd, String xpath) {

if (isExist(wd, xpath)) {

return getText(wd, xpath);

}

return "-1";

}

/**
*
* 获取元素节点的属性值
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @attribute 要获取目标节点的哪个属性
*
* @return
*/

public static String getAttribute(WebDriver wd, String xpath,
String attribute) {

return wd.findElement(By.xpath(xpath)).getAttribute(attribute);

}

/**
*
* 点击节点
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static void click(WebDriver wd, String xpath) {

wd.findElement(By.xpath(xpath)).click();

}

/**
*
* 输入文本
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static void inputs(WebDriver wd, String xpath, String value) {

wd.findElement(By.xpath(xpath)).sendKeys(value);

}

/**
*
* 判断是否选中
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static boolean isChecked(WebDriver wd, String xpath) {

return wd.findElement(By.xpath(xpath)).isSelected();

}

/**
*
* 判断是否可用
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static boolean isEnabled(WebDriver wd, String xpath) {

return wd.findElement(By.xpath(xpath)).isEnabled();

}

/**
*
* 判断是否存在元素
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*
* @return
*/

public static boolean isExist(WebDriver wd, String xpath) {

try {

wd.findElement(By.xpath(xpath));

return true;

} catch (NoSuchElementException e) {

return false;

}

}

/**
*
* 选中复选框,其实和点击一样,只是重新起了个名字
*
* @param wd
*            WebDriver对象
*
* @param xpath
*            目标节点的xpath
*/

public static void check(WebDriver wd, String xpath) {

click(wd, xpath);

}

/**
*
* 点击那种隐藏的下拉框
*
* @param wd
*            WebDriver对象
*
* @param xpath1
*            事件源节点的xpath
*
* @param xpath2
*            目标节点的xpath
*/

public static void clickHidden(WebDriver wd, String xpath1, String xpath2) {

click(wd, xpath1);

click(wd, xpath2);

}

/**
*
* 获取隐藏的文本,原理同上
*
* @param wd
*            WebDriver对象
*
* @param xpath1
*            事件源节点的xpath
*
* @param xpath2
*            目标节点的xpath
*/

public static void getHiddenText(WebDriver wd, String xpath1, String xpath2) {

click(wd, xpath1);

getText(wd, xpath2);

}

/**
*
* 获取隐藏节点的属性值
*
* @param wd
*            WebDriver对象
*
* @param xpath1
*            事件源节点的xpath
*
* @param xpath2
*            目标节点的xpath
*
* @param attribute
*            要获取目标节点的哪个属性
*/

public static String getHiddenAttribute(WebDriver wd, String xpath1,
String xpath2, String attribute) {

click(wd, xpath1);

return getAttribute(wd, xpath2, attribute);

}

/**
*
* 切换窗口
*
* @param wd
*            WebDriver对象
*
* @param title
*            要切换窗口的标题
*/

public static void changeWindow(WebDriver wd, String title) {

String current = wd.getWindowHandle();

Set<String> all = wd.getWindowHandles();

Iterator<String> iterator = all.iterator();

while (iterator.hasNext()) {

String handle = iterator.next();

if (handle.equals(current)) {

continue;

}

else {

wd.switchTo().window(handle);

if (wd.getTitle().contains(title)) {

System.out.println("窗口成功跳转");

break;

}

else {

continue;

}

}

}

}

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: