在Mac上使用Selenium WebDriver和Java上传文件

mwyxok5s  于 2023-02-11  发布在  Java
关注(0)|答案(2)|浏览(165)

我有一个Web应用程序,我试图使用Java,TestNG和Selenium WebDriver实现自动化。应用程序通过几个阶段导航,然后用户需要上传一个文档。
我已经尝试了here列出的建议
而且一直无法让它工作。我不相信browseBtn.sendKeys(file.getAbsolutePath());会在这种情况下工作,因为我试图在Mac上这样做,并且没有浏览按钮的光标。
下面是我的代码到目前为止,只是想知道是否有人已经能够成功地做到这一点,以及如何做到这一点我自己(屏幕截图我正在尝试做什么是下面):

private void documents_UploadDocuments(String driverName) throws SQLException, InterruptedException
{
    /*
     * Upload documents to a request
     */
            Login.login_Requester(driverName);
            WebDriver driver = Login.driver;

            //Click My Requests Tab
            WebElement myRequestTab = driver.findElement(By.xpath("/html/body/div/ul/li[2]/a"));
            myRequestTab.click();

            Thread.sleep(1000);

            //Click Create a New Request
            WebElement createRequestBtn = driver.findElement(By.id("submit"));
            createRequestBtn.click();

            WebElement officialNameTB = driver.findElement(By.id("new-official-name"));
            WebElement createRqtBtn = driver.findElement(By.id("continue"));
            WebElement researchDataAccess = driver.findElement(By.xpath("/html/body/div[3]/div[2]/form/div[2]/div/select/optgroup/option"));

            Thread.sleep(1000);

            officialNameTB.sendKeys("TEST_UploadDocuments");
            researchDataAccess.click();
            createRqtBtn.click();

            Thread.sleep(2500);

            //Enter an IRB Number
            WebElement irbNumberTB = driver.findElement(By.id("irbNumber"));
            irbNumberTB.click();
            irbNumberTB.sendKeys("This is an IRB #");

            //Enter an IRB Expiration Date
            WebElement irbExpCP = driver.findElement(By.id("irbExpDate"));
            irbExpCP.click();
            LocalDate irbDate = LocalDate.now().plusYears(5);
            DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy");
            String irbDate2 = formatter.print(irbDate);
            irbExpCP.sendKeys(irbDate2);

            //Enter a Start Date
            WebElement startDateCP = driver.findElement(By.id("startDate"));
            startDateCP.click();
            LocalDate startDate = LocalDate.now();
            String startDate2 = formatter.print(startDate);
            startDateCP.sendKeys(startDate2);

            //Enter an End Date
            WebElement endDateCP = driver.findElement(By.id("endDate"));
            endDateCP.click();
            LocalDate endDate = LocalDate.now().plusYears(10);
            String endDate2 = formatter.print(endDate);
            endDateCP.sendKeys(endDate2);

            //Click Next Button
            WebElement nextBtn = driver.findElement(By.xpath("/html/body/div/div/div[2]/ul[2]/li[2]/button"));
            nextBtn.click();

            Thread.sleep(1000);

            //Click Find Participant button
            WebElement findPartBtn = driver.findElement(By.id("findPeopleButton"));
            findPartBtn.click();
            Thread.sleep(1000);

            //Search for a participant
            WebElement nameField = driver.findElement(By.id("name"));
            nameField.click();
            nameField.clear();
            String [] namesArray = {"Salisbury, Curtis J. (FVFT)", "Hanson, Shannon H. (FVFT)", "Dinerstein, Jared A. (FVFT)", "Dinerstein, Sabra A. (FVFT)", "Ford, Lee J. (FVFT)"};
            String names = PickRand.get(namesArray);
            nameField.sendKeys(names);

            //Find Now Button
            WebElement findBtn = driver.findElement(By.id("findNow"));
            findBtn.click();

            //Choose the Participant
            WebElement fullName = driver.findElement(By.id("fullName"));
            fullName.click();

            //Choose a location
            String location = PickRand.get(locations.getLocationArray());
            WebElement locationChooser = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[2]/div[2]/div/table/tbody/tr[2]/td[3]/div/label/div/a/div/b"));
            locationChooser.click();
            WebElement locationSrch = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[2]/div[2]/div/table/tbody/tr[2]/td[3]/div/label/div/div/div/input"));
            locationSrch.clear();
            locationSrch.sendKeys(location);
            locationSrch.sendKeys(Keys.RETURN);
            Thread.sleep(500);
            WebElement addBtn = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div[2]/div[2]/div/table/tbody/tr[2]/td[6]/button"));
            addBtn.click();
            Thread.sleep(500);

            nextBtn.click();
            Thread.sleep(500);

            //Click RealSSN Identifier
            WebElement realSSNIdentifier = driver.findElement(By.id("realSSNIdentifier"));
            realSSNIdentifier.click();

            //Click CDW Production Domains Data Source
            WebElement cdwProdDomainsCheck = driver.findElement(By.id("1022"));
            cdwProdDomainsCheck.click();

            //Click the HIPAA questions
            WebElement hipaaConsentYesRdio = driver.findElement(By.id("HIPAAAuthorization1"));
            WebElement hipaaWaiverYesRdio = driver.findElement(By.id("HIPAAWaiver1"));
            hipaaConsentYesRdio.click();
            hipaaWaiverYesRdio.click();

            //Click Next Button
            nextBtn.click();
            Thread.sleep(1000);

            //Upload Documents
            WebElement uploadDoc1 = driver.findElement(By.id("uploadDocumentLink"));
            uploadDoc1.click();
            Thread.sleep(1000);
            WebElement browseBtn = driver.findElement(By.xpath("id('fileField')"));
            File file = null;
            try
            {
                file = new File(Documents_UploadDocuments.class.getClassLoader().getResource("/Users/vhaislsalisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf").toURI());
            }
            catch(URISyntaxException e)
            {
                e.printStackTrace();
            }
            browseBtn.sendKeys(file.getAbsolutePath());

}

上传链接打开此屏幕:

当我在该模式中单击Browse按钮时,将打开此屏幕:

因此,我尝试按照建议使用Robot类,但它实际上并没有选择文件。我需要它来选择文件,然后返回Upload a Document屏幕,这样我就可以单击Upload按钮来完成该过程。当我运行完整测试时,它只是停留在Mac屏幕上进行文件上传。我做错了什么?
下面是我使用Robot类时所做的工作:

//Upload Documents
            WebElement uploadDoc1 = driver.findElement(By.id("uploadDocumentLink"));
            uploadDoc1.click();
            Thread.sleep(1000);
            WebElement browseBtn = driver.findElement(By.xpath("id('fileField')"));
            browseBtn.click();
            Robot r = new Robot();
            /*
             * use the Robot class to type out the path of the document
             * "/Users/vhaislsalisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf"
             */
            r.keyPress(KeyEvent.VK_SLASH); 
            r.keyRelease(KeyEvent.VK_SLASH);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_U);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_U);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_R);
            r.keyRelease(KeyEvent.VK_R);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_SLASH);
            r.keyRelease(KeyEvent.VK_SLASH);
            r.keyPress(KeyEvent.VK_V);
            r.keyRelease(KeyEvent.VK_V);
            r.keyPress(KeyEvent.VK_H);
            r.keyRelease(KeyEvent.VK_H);
            r.keyPress(KeyEvent.VK_A);
            r.keyRelease(KeyEvent.VK_A);
            r.keyPress(KeyEvent.VK_I);
            r.keyRelease(KeyEvent.VK_I);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_L);
            r.keyRelease(KeyEvent.VK_L);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_A);
            r.keyRelease(KeyEvent.VK_A);
            r.keyPress(KeyEvent.VK_L);
            r.keyRelease(KeyEvent.VK_L);
            r.keyPress(KeyEvent.VK_I);
            r.keyRelease(KeyEvent.VK_I);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_C);
            r.keyRelease(KeyEvent.VK_C);
            r.keyPress(KeyEvent.VK_SLASH);
            r.keyRelease(KeyEvent.VK_SLASH);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_D);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_D);
            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);
            r.keyPress(KeyEvent.VK_C);
            r.keyRelease(KeyEvent.VK_C);
            r.keyPress(KeyEvent.VK_U);
            r.keyRelease(KeyEvent.VK_U);
            r.keyPress(KeyEvent.VK_M);
            r.keyRelease(KeyEvent.VK_M);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_N);
            r.keyRelease(KeyEvent.VK_N);
            r.keyPress(KeyEvent.VK_T);
            r.keyRelease(KeyEvent.VK_T);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_SLASH);
            r.keyRelease(KeyEvent.VK_SLASH);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_V);
            r.keyRelease(KeyEvent.VK_V);
            r.keyPress(KeyEvent.VK_I);
            r.keyRelease(KeyEvent.VK_I);
            r.keyPress(KeyEvent.VK_N);
            r.keyRelease(KeyEvent.VK_N);
            r.keyPress(KeyEvent.VK_C);
            r.keyRelease(KeyEvent.VK_C);
            r.keyPress(KeyEvent.VK_I);
            r.keyRelease(KeyEvent.VK_I);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_UNDERSCORE);
            r.keyRelease(KeyEvent.VK_UNDERSCORE);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_T);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_T);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_T);
            r.keyRelease(KeyEvent.VK_T);
            r.keyPress(KeyEvent.VK_UNDERSCORE);
            r.keyRelease(KeyEvent.VK_UNDERSCORE);
            r.keyPress(KeyEvent.VK_D);
            r.keyRelease(KeyEvent.VK_D);
            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);
            r.keyPress(KeyEvent.VK_C);
            r.keyRelease(KeyEvent.VK_C);
            r.keyPress(KeyEvent.VK_U);
            r.keyRelease(KeyEvent.VK_U);
            r.keyPress(KeyEvent.VK_M);
            r.keyRelease(KeyEvent.VK_M);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_N);
            r.keyRelease(KeyEvent.VK_N);
            r.keyPress(KeyEvent.VK_T);
            r.keyRelease(KeyEvent.VK_T);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_SLASH);
            r.keyRelease(KeyEvent.VK_SLASH);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_T);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_T);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_T);
            r.keyRelease(KeyEvent.VK_T);
            r.keyPress(KeyEvent.VK_UNDERSCORE);
            r.keyRelease(KeyEvent.VK_UNDERSCORE);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_R);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_R);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_S);
            r.keyRelease(KeyEvent.VK_S);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_R);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_R);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_Q);
            r.keyRelease(KeyEvent.VK_Q);
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_M);
            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_M);
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);
            r.keyPress(KeyEvent.VK_M);
            r.keyRelease(KeyEvent.VK_M);
            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);
            r.keyPress(KeyEvent.VK_PERIOD);
            r.keyRelease(KeyEvent.VK_PERIOD);
            r.keyPress(KeyEvent.VK_P);
            r.keyRelease(KeyEvent.VK_P);
            r.keyPress(KeyEvent.VK_D);
            r.keyRelease(KeyEvent.VK_D);
            r.keyPress(KeyEvent.VK_F);
            r.keyRelease(KeyEvent.VK_F);
            r.keyPress(KeyEvent.VK_ENTER);
            r.keyRelease(KeyEvent.VK_ENTER);

            WebElement uploadBtn = driver.findElement(By.id("submit"));
            uploadBtn.click();

Login.driver实现如下所示:

public class Login 
{
static Browser browser = new Browser();
HighlightElement highlight = new HighlightElement();
static WebDriver driver;
static String user = "UserName";
static String pwd = "password";
static int userID;

/*
 * Connection Strings:
 * testDB = "jdbc:jtds:sqlserver:Connection String"
 * devDB = "jdbc:jtds:sqlserver:Connection String"
 */

public static void login_Page(String driverName)
{
    driver = browser.getDriver(driverName);

    //navigate to VINCI login page
    driver.get("http://URL/vinci_dart_client/dart9/index.html");
    driver.manage().window().maximize();
}
public static void login_Requester(String driverName) throws InterruptedException, SQLException
{
    //Change user role to requester
    Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver:Connection String");
    PreparedStatement ps1, ps2, ps3;
    ps1 = con.prepareStatement("select id from hib.person where name = ?");
    ps2 = con.prepareStatement("delete from hib.personrole where personid = ?");
    ps3 = con.prepareStatement("delete from hib.persongroup where personid = ?");

    ps1.setString(1, user);

    ResultSet rs = ps1.executeQuery();
    while(rs.next())
    {
        userID = rs.getInt(1);
    }
    rs.close();
    ps1.close();

    ps2.setInt(1, userID);
    ps3.setInt(1, userID);

    ps2.execute();
    ps3.execute();

    driver = browser.getDriver(driverName);

    //navigate to VINCI login page
    driver.get("http://URL");
    driver.manage().window().maximize();

    //Enter User Name
    WebElement userNameField = driver.findElement(By.id("username"));
    userNameField.clear();
    userNameField.sendKeys(user);

    //Enter Password
    WebElement pwdField = driver.findElement(By.id("pword"));
    pwdField.clear();
    pwdField.sendKeys(pwd);

    //Click Login Button
    WebElement loginBtn = driver.findElement(By.xpath("/html/body/div/div/form/div[4]/div/input"));
    loginBtn.click();

}
}
3mpgtkmj

3mpgtkmj1#

我不相信browseBtn.sendKeys(文件.getAbsolutePath());是将工作在这种情况下,因为我试图这样做的Mac和没有光标的浏览按钮。
在任何平台上都没有用于文件输入的光标。这不应该阻止你使用它。但是,我建议改变这一点:

WebElement browseBtn = driver.findElement(By.xpath("id('fileField')"));

到这个

WebElement browseBtn = driver.findElement(By.id("fileField"));

看看这是否能解决问题。
更新:您添加了
我仍然在文件=新文件(文档_上传文档.class.getClassLoader().getResource(“/用户/vhaisl salisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf”).toURI())处收到空指针异常;
这意味着**下一个问题是从路径/Users/vhaisl‌​salisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf**加载。这与Selenium使用完全无关,您需要在继续故障排除之前修复路径/file/file权限。
尝试改变这一点

file = new File(Documents_UploadDocuments.class.getClassLoader().getResource("/Users/vhaislsalisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf").toURI());

到这个

file = new File("/Users/vhaislsalisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf");
atmip9wb

atmip9wb2#

指南here为我工作。我的代码

public static void uploadMediaByRobot(String fileName) {
    //File Need to be imported
    File file = new File("src/main/resources/media/" + fileName);
    StringSelection stringSelection = new StringSelection(file.getAbsolutePath());
    //Copy to clipboard
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

    Robot robot = null;
    try {
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }

    // Cmd + Tab is needed since it launches a Java app and the browser looses focus
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.delay(500);

    //Open Goto window
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_SHIFT);
    robot.keyPress(KeyEvent.VK_G);
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_SHIFT);
    robot.keyRelease(KeyEvent.VK_G);

    //Paste the clipboard value
    robot.keyPress(KeyEvent.VK_META);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_V);

    //Press Enter key to close the Goto window and Upload window
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.delay(500);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

顺便说一句,如果你遇到错误
ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
你可以用here来解
希望这能有所帮助!

相关问题