Selenium Web 自动化 - Selenium常用API

发表于:2018-10-12 10:39

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

 作者:明-Ming    来源:博客园

分享:
  4.1 等待元素加载
  超时设置
  WebDriver driver = new FirefoxDriver();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);      //识别元素时的超时时间
  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  //页面加载时的超时时间
  driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);  //异步脚本的超时时间
  硬性等待  Thread.sleep(int sleeptime);
  智能等待
  设置等待页面加载完毕
  private static void waitElementTest(WebDriver driver) {
  //设置等待页面完全加载的时间是10秒,如果在10秒内加载完毕,剩余时间不在等待
  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
  driver.get("https://www.baidu.com/");
  By inputBox = By.id("kw");
  By searchButton = By.id("su");
  //智能等待元素加载出来
  intelligentWait(driver, 10, inputBox);
  //智能等待元素加载出来
  intelligentWait(driver, 10, searchButton);
  //输入内容
  driver.findElement(inputBox).sendKeys("JAVA");
  //点击查询
  driver.findElement(searchButton).click();
  }
  public static void intelligentWait(WebDriver driver,int timeOut, final By by){
  try {
  (new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {
  public Boolean apply(WebDriver driver) {
  WebElement element = driver.findElement(by);
  return element.isDisplayed();
  }
  });
  } catch (TimeoutException e) {
  Assert.fail("超时!! " + timeOut + " 秒之后还没找到元素 [" + by + "]",e);
  }
  }

  4.2 执行JS脚本
  selenium常用的js总结
  有时候我们需要JS脚本来辅助我们进行测试,比如我们用JS赋值或者用js执行点击操作等。执行JS脚本比较适用某些元素不易点击的情况下使用,比如网页内容太长,当前窗口太长,想要点击那些不在当前窗口可以看到元素可以用此方法。
  private static void runJSTest1(WebDriver driver) throws InterruptedException {
  String js ="alert(\"hello,this is a alert!\")";
  ((org.openqa.selenium.JavascriptExecutor) driver).executeScript(js);
  Thread.sleep(2000);
  }
  private static void runJSTest2(WebDriver driver)
  throws InterruptedException {
  driver.get("https://www.baidu.com/");
  String js ="arguments[0].click();";
  driver.findElement(By.id("kw")).sendKeys("JAVA");
  WebElement searchButton = driver.findElement(By.id("su"));
  ((org.openqa.selenium.JavascriptExecutor) driver).executeScript(js,searchButton);
  Thread.sleep(2000);
  }
  4.3 模拟键盘操作
  有时候有些元素不便点击或者做其他的操作,这个时候可以借助selenium提供的Actions类,它可以模拟鼠标和键盘的一些操作,比如点击鼠标右键,左键,移动鼠标等操作。对于这些操作,使用perform()方法进行执行。
  private static void actionsTest(WebDriver driver)
  throws InterruptedException {
  // 设置等待页面完全加载的时间是10秒,如果在10秒内加载完毕,剩余时间不在等待
  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
  driver.get("https://www.baidu.com/");
  By inputBox = By.id("kw");
  By searchButton = By.id("su");
  // 智能等待元素加载出来
  intelligentWait(driver, 10, inputBox);
  // 智能等待元素加载出来
  intelligentWait(driver, 10, searchButton);
  // 实例化action对象
  Actions action = new Actions(driver);
  // 通过action模拟键盘输入java关键字到 输入框,只有使用了perform方法才会输入进去
  action.sendKeys(driver.findElement(searchButton), "java").perform();
  // 鼠标模拟移动到搜索按钮
  action.moveToElement(driver.findElement(searchButton)).perform();
  // 模拟点击操作
  action.click().perform();
  Thread.sleep(2000);
  }

   上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
44/4<1234
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号