加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Selenium+java - WebDriver如何模拟复制和粘贴

发布时间:2020-12-14 14:39:18 所属栏目:百科 来源:网络整理
导读:以最简单的例子来说明,我们需要在bing搜索引擎中,输入并查询“Selenium自动化测试”几个字。可以很快就写出如下代码: String queryString = "Selenium自动化测试" ;WebElement element = driver.findElement(By.xpath( "//input[@id='sb_form_q']" )); //

以最简单的例子来说明,我们需要在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;
 java.awt.event.KeyEvent;
import java.io.* java.util.concurrent.TimeUnit;
import org.junit.*import org.openqa.selenium.*import org.openqa.selenium.chrome.* com.thoughtworks.selenium.SeleneseTestBase;
 
public class SearchChineseCharacters extends SeleneseTestBase {
  private static WebDriver driver;
  static final int MAX_TIMEOUT_IN_SECONDS = 5;
 
  @BeforeClass
  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
  void tearDownAfterClass()  Exception {
    if (driver != null) {
      System.out.println("运行结束!");
      driver.quit();
    }
  }
 
  @Test
  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 = ;
    try {
      robot =  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");
 
  }
 
   captureScreenshot(String fileName) {
    String imagePath = System.getProperty("user.dir") + File.separator
        + fileName + ".png" {
      byte[] decodedScreenshot = ((TakesScreenshot) driver)
          .getScreenshotAs(OutputType.BYTES);
      FileOutputStream fos = new FileOutputStream( File(imagePath));
      fos.write(decodedScreenshot);
      fos.close();
      System.out.println("截图保存至" + imagePath);
    }  (Exception e) {
      e.printStackTrace();
    }
  }
 
   setClipboardData(String string) {
    StringSelection stringSelection =  StringSelection(string);
    Toolkit.getDefaultToolkit().getSystemClipboard()
        .setContents(stringSelection,);
  }
}

?

使用场景:
1.上传文件,
2.富文本框都行

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读