zoukankan      html  css  js  c++  java
  • Selenium+java

    以最简单的例子来说明,我们需要在bing搜索引擎中,输入并查询“Selenium自动化测试”几个字。可以很快就写出如下代码:

    String queryString = "Selenium自动化测试";
    WebElement element = driver.findElement(By
    .xpath("//input[@id='sb_form_q']"));
    // 直接输入查询字符串
    element.sendKeys(queryString);
    // 点击查询按钮
    driver.findElement(By.xpath("//input[@id='sb_form_go']")).click();
    // 截图函数
    captureScreenshot("截图测试JUnit");

    但是如果我们想把当前的粘贴板Clipboard中的数据粘贴到bing的搜索输入框,该怎么办呢?Selnium是否支持从从粘贴板中粘贴数据呢?答案是肯定的,直接上代码,代码很简单,并且有注释,不再进行解释。

    import java.awt.*;
    import java.awt.datatransfer.StringSelection;
    import java.awt.event.KeyEvent;
    import java.io.*;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.*;
    import com.thoughtworks.selenium.SeleneseTestBase;
     
    public class SearchChineseCharacters extends SeleneseTestBase {
      private static WebDriver driver;
      static final int MAX_TIMEOUT_IN_SECONDS = 5;
     
      @BeforeClass
      public static void setUpBeforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver",
            System.getProperty("user.dir") + File.separator
                + "chromedriver.exe");
        driver = new ChromeDriver();
        String url = "http://cn.bing.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() {
        String queryString = "Selenium自动化测试";
        WebElement element = driver.findElement(By
            .xpath("//input[@id='sb_form_q']"));
        // 直接输入查询字符串
        // element.sendKeys(queryString);
     
        // 下面的语句模拟复制粘贴功能、copy & paste
        // 向粘贴板中存放数据,还可以注释掉下面的语句,进行手工复制一些东西到粘贴板
        setClipboardData(queryString);
        // 模拟Ctrl+V,进行粘贴
        Robot robot = null;
        try {
          robot = new Robot();
        } catch (AWTException e1) {
          e1.printStackTrace();
        }
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        // 点击查询按钮
        driver.findElement(By.xpath("//input[@id='sb_form_go']")).click();
        // 截图函数
        captureScreenshot("截图测试JUnit");
     
      }
     
      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();
        }
      }
     
      public static void setClipboardData(String string) {
        StringSelection stringSelection = new StringSelection(string);
        Toolkit.getDefaultToolkit().getSystemClipboard()
            .setContents(stringSelection, null);
      }
    }
    使用场景:
    1.上传文件,
    2.富文本框都行
  • 相关阅读:
    51nod 1087 1 10 100 1000(找规律+递推+stl)
    51nod 1082 与7无关的数 (打表预处理)
    51 nod 1080 两个数的平方和
    1015 水仙花数(水题)
    51 nod 1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    51 nod 1001 数组中和等于K的数对
    51 nod 1081 子段求和
    51nod 1134 最长递增子序列 (O(nlogn)算法)
    51nod 1174 区间中最大的数(RMQ)
  • 原文地址:https://www.cnblogs.com/longronglang/p/7272383.html
Copyright © 2011-2022 走看看