Selenium+Webdriver完整解决方案

发表于:2017-4-27 11:43

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:白灰    来源:51Testing软件测试网采编

  一、等待处理
  1.全局等待
  /*全局设置,当元素识别不到的时候,可以接受的最长等待时间。*/
  driver.manage()timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  /*全局设置,页面加载的最长等待时间。*/
  driver.manage()timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
  /*全局设置,关于JavaScript代码的异步处理的超时时间。AJAX请求。*/
  driver.manage()timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
  2.元素等待
public void waitForElement(By by) throws Exception{
for(int second = 1; second <= 30; second++){
try{
driver.findElement(by);
break;
}catch(Exception e){
System.out.println(e.getLocalizedMessage());
}
Thread.sleep(1000);
}
}
this.waitForElement(By.id("username"));//调用方法
  系统自带的方法:
  WebElement btnLogin = new WebDriverWait(driver, 10).until(
  ExpectedConditions.presenceOfElementLocated(By.id("login")));
  btnLogin.click();
  二、断言
  1.判断页面元素是否存在
public boolean waitForElement(By by) throws Exception{
boolean isExsit = false;
for(int second = 1; second <= 30; second++){
try{
driver.findElement(by);
isExist = true;
break;
}catch(Exception e){
System.out.println(e.getLocalizedMessage());
}
Thread.sleep(1000);
}
return isExist;
}
if(this.waitForElement(By.id("username"))){
}else{
}
  2.判断页面上的元素的值/内容
  String result = driver.findElement(By.id("msg")).getText();
  //对于内容的判断:
  //1.严格匹配:result.equals("")
  //2.模糊匹配:result.startsWith(""),result.endsWith, result.contains
  //3.正则表达式:result.matches("正则表达式 No=.*")
  if(result.contains("aaa")){
  }else{
  }
  3.直接读取数据库的内容
  String sql = "SELECT ID FROM USERNAME ORDER BY ID "
  int maxId =  thisgetMaxId(sql);
  int postEqual = result.indexOf("=");//取=号在字符串中的位置
  String newId= result.subString(postEqual + 1 );//从=号开始截取到最后,+1后移一位
  if(maxId == Integer.parseInt(newId)){
  }else{
  }
  三、新窗口处理
  1.对话框确认框的操作
  Alert alert = driver.switchTo().alert();
  alert.accept();  //点击确定
  alert.dismiss();  //点击取消
  2.新窗口的操作
//windowID切换
String loginID = driver.getWindowHandle();
for(String windowID : driver.getWindowHandles()){
if (!windowID.equals(loginID))
driver.switchTo.().window(windowID);
}
//windowTitle切换
for(String windowID : driver.getWindowHandles()){
driver.switchTo.().window(windowID);
Sring windowTitle =  driver.getTitle();
if(windowTitle.contains("部分标题")){
break;
}
}
  3.弹出窗口和Iframe
  driver.switchTo().frame("frameId");//切换到frame页面
  driver.switchTo().window("windowhandle");//切换回到主页面
  四、鼠标操作事件
private Actions action;
//单击-click
public void click(){
action.moveToElement(drvier.findElement(By.linkText("login"))).perform();
action.click().perform(); //action的调用后面一定要加上perform,才能保证能真正的运行。
}
//双击-
public void doubleClick(){
action.doubleClick(drvier.findElement(By.linkText("login"))).perform();
}
//右键-
public void rightClick(){
action.contextClick(drvier.findElement(By.linkText("login"))).perform();
}
//悬停-
public void mouseHold(){
action.clickAndHold(drvier.findElement(By.linkText("login"))).perform();
}
//拖拽-
public void dragDrop(){
WebElement source = drvier.findElement(By.linkText("login"))
WebElement target = drvier.findElement(By.linkText("login"))
action.dragAndDrop(source, target);
action.dragAndDropBy(source, 200, 300);
}
  五、键盘事件处理
  1.webDriver键盘操作-Action
  //webDriver键盘操作
  action.sendKeys("A").perform(); //按下键盘A
  action.sendKeys(Keys.Delete).perform();
  2.Java键盘操作-Robot
  //Java键盘操作
  //Java内含robot操作对象。throws Exception抛出异常给调用者。try{}catch(Excetion e){}//Excetion所有异常的父类,捕捉所有异常。
  public void robotUsage(){
  Robot robot = new Robot();
  robot.mousePress(InputEvent.BUTTON1_MASK); //鼠标左键点击
  robot.mouseRelease(InputEvent.BUTTON1_MASK); //鼠标左键释放
  robot.mousePress(keyEvent.VK_ENTER); //回车键
  robot.mouseRelease(keyEvent.VK_ENTER);
  }
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号