Selenium+java - 通过Robot对象(键盘操作)上传文件

思路:

1、将文件路径复制到剪切板

2、用robot对象模拟键盘操作即可

复制文件代码:

 public void setClipboardData(String data) {
        StringSelection stringSelection = new StringSelection(data);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

实现上传代码

package com.brower.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;

/**
 * @author rongrong
 */
public class TestUploadByRobot {
    WebDriver driver;

    @BeforeClass
    public void beforeClass() {
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test
    public void testUploadByRobot() throws AWTException {
        driver.get("file:///C:/Users/Administrator/Desktop/upload.html");
        driver.manage().window().maximize();
        //选择文件
        driver.findElement(By.id("upload")).click();
        setClipboardData("F:\\robotium-solo-5.6.1.jar");
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_TAB);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.keyPress(KeyEvent.VK_TAB);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_TAB);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(100);
        robot.keyRelease(KeyEvent.VK_ENTER);

    }

    public void setClipboardData(String data) {
        StringSelection stringSelection = new StringSelection(data);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }


    @AfterClass
    public void afterClass() {
        //driver.quit();
    }

}

效果:

 

转载于:https://www.cnblogs.com/longronglang/p/11312413.html

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

以最简单的例子来说明,我们需要在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.富文本框都行
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值