收集WebDriver的执行命令和参数信息

发表于:2013-2-16 11:04

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

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

分享:
package com.loggingselenium;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;
 
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.LoggingInternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
 
import com.thoughtworks.selenium.*;
 
public class SearchWebDriver extends SeleneseTestBase {
 private static WebDriver driver;
 static final int MAX_TIMEOUT_IN_SECONDS = 30;
 
 @BeforeClass
 public static void setUpBeforeClass() {
 
////  // IE
  System.setProperty("webdriver.ie.driver",
  System.getProperty("user.dir") + File.separator + "resources"
  + File.separator + "IEDriverServer.exe");
  DesiredCapabilities ieCapabilities = DesiredCapabilities
    .internetExplorer();
  ieCapabilities.setCapability(
  LoggingInternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
      true);
  try {
   driver = new LoggingInternetExplorerDriver(ieCapabilities);
  } catch (Exception e) {
   e.printStackTrace();
  }
  String url = "http://loggingselenium.com/";
  driver.manage().window().maximize();
  driver.manage().timeouts()
    .implicitlyWait(MAX_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
  driver.get(url);
 }
 
 @AfterClass
 public static void tearDownAfterClass() throws Exception {
  if (driver != null) {
   System.out.println("运行结束!");
   driver.quit();
  }
 }
 
 @Test
 public void test() {
  // LoggingSelenium网站查询输入框,输入"selenium"进行查询
  String queryString = "selenium webdriver";
  driver.findElement(By.xpath("//input[@name='s']"))
    .sendKeys(queryString);
  pause(1000);
  // 单击查询按钮,执行查询
  driver.findElement(By.xpath("//button[@name='searchsubmit']")).click();
  pause(1000);
  String title = driver.getTitle();
  System.out.println("获取的页面标题:" + title);
  pause(2000);
  SeleneseTestBase.assertTrue(title.contains(queryString));
  // 测试截图函数
  captureScreenshot("截图测试JUnit");
 
 }
 
 /**
  * fileName 保存截图的文件名
  */
 private void captureScreenshot(String fileName) {
  String imagePath = System.getProperty("user.dir") + File.separator
    + fileName + ".png";
  try {
   byte[] decodedScreenshot = ((TakesScreenshot) driver)
     .getScreenshotAs(OutputType.BYTES);
   FileOutputStream fos = new FileOutputStream(new File(imagePath));
   fos.write(decodedScreenshot);
   fos.close();
   System.out.println("截图保存至" + imagePath);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

  运行该测试类,控制台部分信息如下:

  正在执行的命令为:driverCommand=maximizeWindow 所使用的参数为parameters=windowHandle=current; ms=30000;
  正在执行的命令为:driverCommand=implicitlyWait 所使用的参数为parameters=
  正在执行的命令为:driverCommand=get 所使用的参数为parameters=url=http://loggingselenium.com/; using=xpath; value=//input[@name='s'];
  正在执行的命令为:driverCommand=findElement 所使用的参数为parameters=id=b1c8fbfb-b55d-4507-9e18-7d24a6cf51ce; value=[Ljava.lang.CharSequence;@1a99561;
  正在执行的命令为:driverCommand=sendKeysToElement 所使用的参数为parameters=
  正在执行的命令为:driverCommand=findElement 所使用的参数为parameters=using=xpath; value=//button[@name='searchsubmit'];
  正在执行的命令为:driverCommand=clickElement 所使用的参数为parameters=id=8413f031-94ed-4bbc-a44b-f54760384735;
  正在执行的命令为:driverCommand=getTitle 所使用的参数为parameters=获取的页面标题:selenium webdriver | 搜索结果 | LoggingSelenium
  正在执行的命令为:driverCommand=screenshot 所使用的参数为parameters=截图保存至D:\workspace\eclipse\workspace\SeleniumDemo\截图测试JUnit.png

  本文转载自:http://loggingselenium.com/?p=219

22/2<12
100家互联网大公司java笔试题汇总,填问卷领取~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号