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