在Mac上使用Selenium WebDriver和Java进行文件上载

时间:2013-10-29 17:11:57

标签: java eclipse selenium testng

我有一个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());

}

上传链接会打开此屏幕: Choose File Modal

当我点击该模式中的“浏览”按钮时,它会打开此屏幕: File Upload Screen

所以我尝试按照建议使用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();

}
}

2 个答案:

答案 0 :(得分:3)

  

我不相信browseBtn.sendKeys(file.getAbsolutePath());要走了   在这种情况下工作,因为我试图在Mac上执行此操作,并且没有光标   对于“浏览”按钮。

任何平台都没有用于文件输入的游标。这不应该阻止你使用它。但是,我建议改变这个:

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

到此

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

看看是否能解决问题。


更新:您添加了

  

我仍然在file = new处获得空指针异常   文件(Documents_UploadDocuments.class.getClassLoader()的getResource( “/用户/ vhaisl salisc /文档/ VINCI_Test_documents / Test_ResReqMemo.pdf”)toURI());

这意味着下一个问题是从路径/Users/vhaisl‌​salisc/Documents/VINCI_Test_documents/Test_ResReqMemo.pdf 加载。这与Selenium的使用完全无关,您需要在继续进行故障排除之前修复路径/文件/文件权限。

尝试更改此

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");

答案 1 :(得分:0)

指南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

解决

希望获得帮助!