Example usage for org.openqa.selenium By xpath

List of usage examples for org.openqa.selenium By xpath

Introduction

In this page you can find the example usage for org.openqa.selenium By xpath.

Prototype

public static By xpath(String xpathExpression) 

Source Link

Usage

From source file:ValidSubmission.java

public static void ThenJobsTitle_ShortDesc_FullDescIs() {
    // int j =0;//from ww w  .ja  va 2 s  .  c om
    // Grab the table 
    WebElement table = driver.findElement(By.id("jobListings"));

    //Get number of rows in table 
    int numOfRow = table.findElements(By.tagName("tr")).size();
    List<String> jobLinkUrl = new ArrayList(numOfRow);

    for (int j = 1; j <= (numOfRow - 2); j = (j + 2)) {

        String first_part_jobShortDesc_xpath = "//*[@id='jobListings']/tbody/tr[";
        String second_part_jobShortDesc_xpath = "]/td/span";

        String first_part_jobLinkTitle_xpath = "//*[@id='jobListings']/tbody/tr[";
        String second_part_jobLinkTitle_xpath = "]/td/a";

        String first_part_jobLinkUrl_xpath = "//*[@id='jobListings']/tbody/tr[";
        String second_part_jobLinkUrl_xpath = "]/td/a";

        WebElement webJobLinkUrl = driver
                .findElement(By.xpath(first_part_jobLinkUrl_xpath + (j + 2) + second_part_jobLinkUrl_xpath));
        jobLinkUrl.add(webJobLinkUrl.getAttribute("href"));
        //                   
        String final_jobLinkTitle_xpath = first_part_jobLinkTitle_xpath + (j + 2)
                + second_part_jobLinkTitle_xpath;
        String final_jobShortDesc_xpath = first_part_jobShortDesc_xpath + (j + 3)
                + second_part_jobShortDesc_xpath;

        WebElement webJobLinkTitle = driver.findElement(By.xpath(final_jobLinkTitle_xpath));
        String jobLinkTitle = webJobLinkTitle.getText();

        WebElement webJobShortDesc = driver.findElement(By.xpath(final_jobShortDesc_xpath));
        String jobShortDesc = webJobShortDesc.getAttribute("innerHTML");

        System.out.println("Job Title = " + jobLinkTitle);
        System.out.println();

        System.out.println("Short Description of Job = " + jobShortDesc);
        System.out.println();

        System.out.println("Link URL of Job" + jobLinkUrl);
        System.out.println();
    }

    //        // GoTo 'Full Job Description'
    for (int i = 0; i < jobLinkUrl.size(); i++) {

        driver.navigate().to(jobLinkUrl.get(i));
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //          
        WebElement webJobFullDescData = driver
                .findElement(By.xpath("//*[@id='jobDetails']/div[2]/table/tbody/tr[1]/td"));
        String jobFullDescData = webJobFullDescData.getText();
        //     
        System.out.println("Job Full Description Data" + jobFullDescData);
        System.out.println();

    }
}

From source file:DesktopChosenIT.java

License:Apache License

@Override
protected String getMultiplePlaceHolderText() {
    String xpath = "//div[@id='chosen_container__0_chzn']//input";

    return webDriverWait().until(presenceOfElementLocated(By.xpath(xpath))).getAttribute("value");
}

From source file:DesktopChosenIT.java

License:Apache License

protected void openDropDown() {
    WebElement btn;/*from   ww w.j  a v a  2 s  .c o  m*/

    if (isMultipleChosenComponent()) {
        btn = getInput();
    } else { // single
        String xpath = "//div[@id='chosen_container__0_chzn']";
        btn = webDriverWait().until(elementToBeClickable(By.xpath(xpath)));
    }
    btn.click();
}

From source file:DesktopChosenIT.java

License:Apache License

/**
 * Deselect an option previously selected. Work only with multiple chosen list box.
 *///from  w  w w  .  j  a  v  a2s. c  om
private <T extends Enum<T>> void deselectOption(T val, Renderer<T> renderer) {
    String xpath = String.format("//li[span/text()='%s']/a", renderer.render(val));
    WebElement abbr = webDriverWait().until(presenceOfElementLocated(By.xpath(xpath)));
    abbr.click();
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Execute CM-01 Test// ww  w  . j  av  a 2  s  .com
 */
public void handleCM01Test() {
    String[] scoUrl;
    scoUrl = new String[2];
    String currentUrl = driver.getCurrentUrl();
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    driver.switchTo().frame("scorm_object");
    wait.until(presenceOfElementLocated(By.id("tRadio0")));
    //WebElement elementIR = driver.findElement(By.id("fRadio0"));
    //elementIR.click();
    wait.until(presenceOfElementLocated(By.xpath("//label[contains(.,'Did the LMS')]")));
    driver.findElement(By.id("tRadio0")).click();
    driver.findElement(By.id("fRadio1")).click();
    driver.findElement(By.id("fRadio2")).click();
    driver.findElement(By.id("submit")).click();
    //elementIR.submit();
    /*driver.switchTo().window("moodleWindow");
    //driver.findElement(By.id("ygtvlabelel2")).click();
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    System.out.println("done 1st");
    driver.switchTo().frame("scorm_object");
    //wait.until(presenceOfElementLocated(By.id("submit")));
    System.out.println("done 2nd");*/
    wait.until(presenceOfElementLocated(By.xpath("//div[contains(.,'Completed.')]")));
    System.out.println("Testing Activity 2. Please Wait...");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    driver.switchTo().window("moodleWindow");
    scoUrl[0] = driver.findElement(By.xpath("//span[contains(.,'Activity 2')]")).getAttribute("title");
    driver.get(currentUrl + '?' + scoUrl[0]);
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    driver.switchTo().frame("scorm_object");
    wait.until(presenceOfElementLocated(By.xpath("//label[contains(.,'Did the LMS')]")));
    driver.findElement(By.id("fRadio0")).click();
    driver.findElement(By.id("tRadio1")).click();
    driver.findElement(By.id("submit")).click();
    driver.switchTo().window("moodleWindow");
    System.out.println("Testing Activity 3. Please Wait...");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    scoUrl[1] = driver.findElement(By.xpath("//span[contains(.,'Activity 3')]")).getAttribute("title");
    driver.get(currentUrl + '?' + scoUrl[1]);
    wait.until(presenceOfElementLocated(By.id("scorm_object")));
    wait.until(presenceOfElementLocated(By.xpath("//div[contains(.,'Completed.')]")));
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Handle / Launch SCO's//  w ww  .ja  va  2  s.  com
 */
public void defaultSCOHandler() {
    // Handle Selenium IE Bug by explicit wait
    int scoCount = fetchAllSCO();
    int scoNo = 1;
    String userInstruction, activityName = null;
    // Not infinite loop, testing based on TestSuite Instrcutions
    while (scoCount > 0) {
        //First Activity does not need to be launched, launch after first
        driver.switchTo().window("moodleWindow");
        driver.switchTo().frame("scorm_object");
        wait.until(presenceOfElementLocated(By.id("teststatus")));
        //wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'progress...')]")));
        // Allow loading time, specially for UI tests
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        userInstruction = readUserInstructions();
        if (userInstruction.contains("LMS User Interface")) {
            answerDefaultUI(scoNo);
            driver.switchTo().window("moodleWindow");
        } else {
            // Print only for 1st Activity.
            if (scoNo == 1) {
                System.out.println("Testing Activity. Please Wait... ");
            }
            driver.switchTo().window("moodleWindow");
            driver.switchTo().frame("scorm_object");
            //Check only for "Completed." as some packages contain extra spaces
            wait.until(presenceOfElementLocated(By.xpath("//div[contains(.,'Completed.')]")));
            System.out.println("Testing of Activity Completed.");
            driver.switchTo().window("moodleWindow");
        }
        // Read instructions to launch new activity
        // First Activity does not need to be launched.

        userInstruction = readUserInstructions();
        if (!userInstruction.contains("relaunch")
                && !(userInstruction.contains("please exit the package/course"))
                && userInstruction.contains("launch")) {
            activityName = userInstruction.substring((userInstruction.lastIndexOf("nch ") + 4),
                    userInstruction.lastIndexOf("."));
            activityName = activityName.trim();
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.xpath("//span[contains(.,'" + activityName + "')]")).click();
            System.out.println("Testing Activity " + activityName + ". Please Wait... ");
            // Allow loading time of new Activity
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else if (userInstruction.contains("trigger a Continue Navigation Event")) {
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.id("nav_next-button")).sendKeys(Keys.ENTER);
        } else if (userInstruction.contains("trigger a Previous Navigation Event")) {
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.id("nav_prev-button")).sendKeys(Keys.ENTER);
        } else if (userInstruction.contains("relaunch")) {
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.linkText("ADL 2004")).sendKeys(Keys.ENTER);
            launchTestPackage(userInstruction.substring((userInstruction.lastIndexOf("age ") + 4),
                    (userInstruction.lastIndexOf("to") - 1)));
        } else if (userInstruction.contains("please exit the package/course")) {
            if (userInstruction.contains("launch")) {
                driver.switchTo().window("moodleWindow");
                driver.findElement(By.linkText("ADL 2004")).sendKeys(Keys.ENTER);
                launchTestPackage(userInstruction.substring((userInstruction.lastIndexOf("age ") + 4),
                        userInstruction.lastIndexOf(".")));
            } else {
                System.out.println("Testing of Package Completed.");
                break;
            }
        } else if (userInstruction.isEmpty()) {
            // Launch last activity again.
            driver.switchTo().window("moodleWindow");
            driver.findElement(By.xpath("//span[contains(.,'" + activityName + "')]")).click();
        }
        scoNo++;
    }
    // Old way of handling tests.
    /*for (int i = 1; i <= scoCount; i++) {
       //First Activity does not need to be launched.
       if(i != 1) {
    driver.findElement(By.id("ygtvlabelel"+i)).click();
       }
       driver.switchTo().frame("scorm_object");
       wait.until(presenceOfElementLocated(By.id("teststatus")));
       wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'progress...')]")));
       boolean uiTestPresent = isInstructionForUIPresent();
       if (uiTestPresent) {
    answerDefaultUI(i);
       } else {
    System.out.println("Testing Activity "+i+". Please Wait... ");
    //Check only for "Completed." as some packages contain extra spaces
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Completed.')]")));
    System.out.println("Testing of Activity "+i+" Completed.");
    driver.switchTo().window("moodleWindow");
       }
    }
    // Some test packages need to launch 
    System.out.println("Testing Completed.");
    driver.switchTo().window("");
    driver.switchTo().frame("controls");
    wait.until(presenceOfElementLocated(By.id("continue")));
    driver.findElement(By.id("continue")).click(); */
}

From source file:scormADL2004.java

License:Open Source License

public void launchTestPackage(String testPackageName) {
    // Check to see which user should launch the package.
    checkLoginSession();// ww w . j a va 2s.c o m
    driver.switchTo().window("moodleWindow");
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Recent activity')]")));
    if (testPackageName.equalsIgnoreCase("DDM")) {
        driver.findElement(By.partialLinkText(testPackageName + "a SCORM package")).sendKeys(Keys.ENTER);
    } else {
        driver.findElement(By.partialLinkText(testPackageName + " SCORM package")).sendKeys(Keys.ENTER);
    }
    // Workaround for WebDriver bug, Seleniu, IE.
    wait.until(presenceOfElementLocated(By.id("page-footer")));
    // Refresh Driver to crawl page again.
    driver.switchTo().window("moodleWindow");
    if (isElementPresent(By.id("n"))) {
        WebElement element = driver.findElement(By.id("n"));
        element.click();
        element.submit();
        //driver.findElement(By.xpath("//input[@value='Enter']")).click();
    }
}

From source file:scormADL2004.java

License:Open Source License

/**
 * Handle / Launches test packages in order they are specified.
 * @param String[] testsToExecute//  w w w.  j  av a  2  s. com
 */
public void testsHandler(String[] testsToExecute) {
    if (testsToExecute[0] == "") {
        // No tests uploaded
        throw new NullPointerException("No specified tests found or uploaded.");
    }
    initialiseTestSuite(testsToExecute);
    driver.switchTo().window("");
    driver.switchTo().frame("currentInstructions");
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Login to the LMS')]")));
    // Check if current user login is the required user logged in
    checkLoginSession();
    driver.switchTo().window("moodleWindow");
    // Wait till Page completely loads - Selenium Bug
    wait.until(presenceOfElementLocated(By.xpath("//*[contains(.,'Recent activity)]")));
    for (int i = 0; i < testsToExecute.length; i++) {
        System.out.println("Testing ADL SCORM 2004 " + testsToExecute[i] + " package...");
        launchTestPackage(testsToExecute[i]);
        //switch cannot compare string
        // TODO add handle for CM-01 Test
        if (testsToExecute[i].equalsIgnoreCase("CM-01")) {
            handleCM01Test();
        }
        // default case
        else {
            defaultSCOHandler();
        }
        driver.switchTo().window("moodleWindow");
        driver.findElement(By.linkText("Exit activity")).click();
        wait.until(presenceOfElementLocated(By.partialLinkText(testsToExecute[i] + " SCORM package")));
        driver.switchTo().window("");
        driver.switchTo().frame("controls");
        wait.until(presenceOfElementLocated(By.id("continue")));
        driver.findElement(By.id("continue")).sendKeys(Keys.ENTER);
    }
    driver.findElement(By.linkText("Logout")).click();
    System.out.println("Tested All Packages. The Script will now exit.");
}

From source file:CarTest1.java

@Test
public void test4() throws Exception {
    WebElement element = driver.findElement(By.id("h_year"));
    element.click();// ww w  .  java2s .c  o  m
    int firstRow = driver.findElements(By.xpath("//tbody/tr[1 and td = '938']")).size();
    int secondRow = driver.findElements(By.xpath("//tbody/tr[5 and td = '940']")).size();

    System.out.println(driver.findElements(By.xpath("//tbody/tr[5 and td = '940']")));

    Assert.assertThat(firstRow, is(1));
    Assert.assertThat(secondRow, is(1));
}

From source file:CarTest1.java

@Test
public void test5() throws Exception {

    WebElement row = driver.findElement(By.xpath("//tbody/tr[td='938']"));
    WebElement a = row.findElements(By.tagName("a")).get(0);
    a.click();//from   www.j av a2s  .co  m
    WebElement desc = driver.findElement(By.name("description"));
    desc.clear();
    desc.sendKeys("Cool car");

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

    (new WebDriverWait(driver, WAIT_MAX)).until((ExpectedCondition<Boolean>) (WebDriver d) -> {
        WebElement newRow = d.findElement(By.xpath("//tbody/tr[td = '938']"));
        String b = newRow.findElements(By.tagName("td")).get(5).getText();
        Assert.assertEquals("Cool car", b);
        return true;
    });
}