4

Java、TestNG、および Selenium WebDriver を使用して自動化しようとしている Web アプリケーションがあります。アプリケーションはいくつかのステージをナビゲートし、ユーザーはドキュメントをアップロードする必要があります。

ここにリストされている提案を試しました

そして、それを機能させることができませんでした。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());

}

アップロード リンクをクリックすると、次の画面が開きます。 ファイルモーダルを選択

そのモーダルの [参照] ボタンをクリックすると、次の画面が開きます。 ファイルアップロード画面

そのため、提案どおりにクラスを使用しようとしまし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();

}
}
4

2 に答える 2

0

The guide here work for me. My code

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

Btw, if you meet the error

ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!

You can solve with the answer here

Hope this help!

于 2019-05-13T04:50:42.567 に答える