Selenium+java - 經過Robot對象上傳文件

Selenium+java - 經過Robot對象上傳文件

思路:html

一、將文件路徑複製到剪切板java

二、用robot對象模擬鍵盤操做便可web

複製文件代碼:chrome

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();
    }

}

效果:
Selenium+java - 經過Robot對象上傳文件ide

EOF測試

本文做者:久曲建的測試窩
本文連接:https://www.cnblogs.com/longronglang/p/11312413.html
關於博主:評論和私信會在第一時間回覆。或者直接私信我。
版權聲明:本博客全部文章除特別聲明外,均採用 BY-NC-SA 許可協議。轉載請註明出處!
聲援博主:若是您以爲文章對您有幫助,能夠點擊文章右下角【推薦】一下。您的鼓勵是博主的最大動力!
優秀不夠,你是否無可替代ui

軟件測試交流QQ羣:721256703,期待你的加入!!3d

歡迎關注個人微信公衆號:軟件測試君code

Selenium+java - 經過Robot對象上傳文件

相關文章
相關標籤/搜索