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:applango.common.services.Applango.genericApplangoWebsiteActions.java

private static void clickOnApplicationLink(WebDriver driver) throws IOException {
    driver.findElement(By.xpath(applangoObject.APPLICATION_LINK.getValue())).click();
}

From source file:applango.common.services.Applango.genericApplangoWebsiteActions.java

private static String getLicenseCostInAccountPage(RemoteWebDriver driver, WebDriverWait wait)
        throws IOException {
    //        genericApplangoWebsiteActions.openUserAccount(driver, wait);
    //            Click on SF in account page
    driver.findElement(By.xpath("//*[@id=\"applist\"]/div[3]")).click();
    waitUntilWaitForServerDissappears(wait);
    return driver.findElement(By.id("licenses")).getText();
}

From source file:AQ.TeamsUnitTest.java

@Test
public void hello() {
    WebDriver driver = webDriver;//w ww.  j  a v  a2  s .  c  o m
    int noTeams = 3;

    driver.get(baseUrl + "/champgen/faces/index.xhtml");
    driver.findElement(By.linkText("Login")).click();
    driver.findElement(By.id("login-form:username")).sendKeys("admin");
    driver.findElement(By.id("login-form:password")).sendKeys("admin");
    driver.findElement(By.id("login-form:btnSubmit")).click();
    driver.findElement(By.linkText("New Championship")).click();
    driver.findElement(By.id("new-championship-form:name")).clear();
    driver.findElement(By.id("new-championship-form:name")).sendKeys("TestChamp1");
    driver.findElement(By.id("new-championship-form:btnSubmit")).click();
    assertTrue(driver.findElement(By.id("msg:info")).getText().matches("^[\\s\\S]*added ![\\s\\S]*$"));

    for (int i = 0; i < noTeams; i++) {
        driver.findElement(By.linkText("New Team")).click();
        driver.findElement(By.id("newteam-form:team")).clear();
        driver.findElement(By.id("newteam-form:team")).sendKeys("Team" + i);
        driver.findElement(By.id("newteam-form:leader")).clear();
        driver.findElement(By.id("newteam-form:leader")).sendKeys("Leader" + i);
        driver.findElement(By.id("newteam-form:btnSubmit")).click();
    }

    this.pause(TIME_TO_PAUSE);
    this.pause(TIME_TO_PAUSE);

    driver.get(baseUrl + "/champgen/faces/index.xhtml");

    int rowCount = webDriver.findElements(By.xpath("//table/tbody/tr")).size();
    System.out.println(rowCount);
    assertTrue(rowCount == noTeams + 1); // + 1 because title is counted as row

    this.pause(TIME_TO_PAUSE);
}

From source file:assignment2.Task4DtmlxInvalidDetails.java

@Test
public void abc() throws InterruptedException {
    Thread.sleep(3000);/*from www.j  ava  2 s  .  co  m*/
    getDriver().manage().window().maximize();

    WebElement ele1 = getDriver()
            .findElement(By.xpath("html/body/div[1]/header/div[1]/div/div/div/div/nav/ul/li[1]/a"));

    Actions action = new Actions(getDriver());
    action.moveToElement(ele1).build().perform();
    Thread.sleep(2000);

    WebElement ele2 = getDriver()
            .findElement(By.xpath("html/body/div[1]/header/div[1]/div/div/div/div/nav/ul/li[1]/ul/li[3]/a"));
    action.moveToElement(ele2).build().perform();
    Thread.sleep(2000);

    getDriver().findElement(By.xpath(
            "html/body/div[1]/header/div[1]/div/div/div/div/nav/ul/li[1]/ul/li[3]/ul/li[6]/ul[2]/li[2]/a"))
            .click();
    Thread.sleep(2000);

    getDriver().findElement(By.xpath("html/body/div[4]/header/div[1]/div/div/div/div/nav/ul/li[4]/a")).click();

    getDriver().findElement(By.id("email")).sendKeys("abc@apple.com");
    getDriver().findElement(By.id("license")).sendKeys("Abcabc123!");

    String output = getDriver().findElement(By.xpath(".//*[@class='submit-error m-bottom-30']")).getText();
    String input = "ERROR: Invalid e-mail or license number";
    Assert.assertEquals(output, input);
    Thread.sleep(1000);

}

From source file:at.ac.tuwien.big.testsuite.impl.selenium.BaseSeleniumTest.java

License:Apache License

protected static WebElement getParentElement(WebDriver driver, By by) {
    try {/*from w ww.ja  v a  2s. c  o m*/
        WebElement element = driver.findElement(by);
        return element.findElement(By.xpath(".."));
    } catch (NoSuchElementException ex) {
        return null;
    }
}

From source file:at.ac.tuwien.big.testsuite.impl.selenium.TableTest.java

License:Apache License

/**
 * Gameplay test - tests and simulates multiple game passes and exports the
 * page content at startup, after the first step and after a player has won
 * for the first time. Performed checks: - player and computer movement -
 * round count - leader - oily field reachable - player wins - computer wins
 * - restart// ww w. j  a  v  a2  s  . c  om
 *
 * @throws Exception
 */
@Test
public void testFeatures_shouldVerifyEveryFeatureAtLeastOnce() throws Exception {
    exportCurrentHTML(driver, "startup.html");
    boolean firstStepExportDone = false;
    boolean finishExportDone = false;

    checkInitialState(driver, "Normal test");

    OUTER: for (int i = 0; i < MAX_TRIES && (!jumpedOnOilyField || !onceLost || !onceWon); i++) {
        boolean finished = false;
        int noMovementRounds = 0;
        Integer expectedPosition = 0;
        Integer expectedComputerPosition = 0;
        Integer expectedRound = getRound(driver);

        checkThat("Round is wrong", expectedRound, isIn(Arrays.asList(0, 1)));

        if (expectedRound == null) {
            checkThat("Unexpected value for round", expectedRound, notNullValue());
            expectedRound = 1;
        }

        for (int n = 0; n < MAX_MOVES && !finished; n++) {
            if (noMovementRounds >= NO_MOVEMENT_ROUNDS_THRESHOLD) {
                checkThat(
                        "Can't play any more since the dice has either been disabled or can't be clicked anymore",
                        true, is(false));
                break OUTER;
            }

            Integer diceValue = rollDice(driver);
            Integer computerDiceValue = getComputerScore(driver);

            assertNotNull(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Unexpected value for diceValue", diceValue);
            assertNotNull(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Unexpected value for computerDiceValue",
                    computerDiceValue);

            expectedRound++;

            // check dice value
            checkThat("Dice value '" + diceValue + "' is none of the allowed values {1, 2, 3}", diceValue,
                    isIn(allowedDiceValues));

            // check player move

            expectedPosition += diceValue;
            if (expectedPosition > 0 && expectedPosition < 6 && isOily(driver, expectedPosition) != null
                    && isOily(driver, expectedPosition)) {
                jumpedOnOilyField = true;
                expectedPosition = 0;
            }
            if (expectedPosition > 6) {
                expectedPosition = 6;
            }

            // check computer move

            expectedComputerPosition += computerDiceValue;
            if (expectedComputerPosition > 0 && expectedComputerPosition < 6
                    && isOily(driver, expectedComputerPosition) != null
                    && isOily(driver, expectedComputerPosition)) {
                jumpedOnOilyField = true;
                expectedComputerPosition = 0;
            }
            if (expectedComputerPosition > 6) {
                expectedComputerPosition = 6;
            }

            // Wait for the elements to appear in the expected positions
            if (!waitForJQuery(driver,
                    By.xpath("//li[@id='" + getFieldId(expectedPosition) + "']//span[@id='player1']"))) {
                checkThat(
                        "Could not retrieve position of player 1 by id 'player1' which might be caused by a concurrency error",
                        true, is(false));
            }
            if (!waitForJQuery(driver, By
                    .xpath("//li[@id='" + getFieldId(expectedComputerPosition) + "']//span[@id='player2']"))) {
                checkThat(
                        "Could not retrieve position of player 2 by id 'player2' which might be caused by a concurrency error",
                        true, is(false));
            }

            // check round
            Integer round = getRound(driver);
            assertNotNull(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Unexpected value for round", round);
            checkThat("Round count of " + round + " is not as expected (" + expectedRound + ")", round,
                    is(expectedRound));

            if (!firstStepExportDone) {
                exportCurrentHTML(driver, "first_step.html");
                firstStepExportDone = true;
            }

            Integer position = getPlayerPosition(driver, 1);
            Integer computerPosition = getPlayerPosition(driver, 2);

            assertNotNull(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Unexpected value for position", position);
            assertNotNull(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Unexpected value for computerPosition",
                    computerPosition);

            checkThat("Incorrect Player position: " + position + " (expected: " + expectedPosition + ")",
                    position, is(expectedPosition));
            checkThat("Incorrect Computer position: " + computerPosition + " (expected: "
                    + expectedComputerPosition + ")", computerPosition, is(expectedComputerPosition));

            if (((position == expectedPosition - diceValue && isOily(driver, position + diceValue) != null
                    && !isOily(driver, position + diceValue))
                    || (expectedPosition == 0 && isOily(driver, position + diceValue) != null
                            && isOily(driver, position + diceValue)))
                    && ((computerPosition == expectedComputerPosition - computerDiceValue
                            && isOily(driver, computerPosition + computerDiceValue) != null
                            && !isOily(driver, computerPosition + computerDiceValue))
                            || (expectedComputerPosition == 0
                                    && isOily(driver, computerPosition + computerDiceValue) != null
                                    && isOily(driver, computerPosition + computerDiceValue)))
                    && round == expectedRound - 1) {
                noMovementRounds++;
            }

            // do this so that even if students do something wrong, we don't have to wait for coming timeouts
            expectedPosition = position;
            expectedComputerPosition = computerPosition;
            expectedRound = round;

            // check leader

            String expectedLeader = "mehrere";
            if (position > computerPosition) {
                expectedLeader = getPlayerName(driver, 1).toLowerCase();
            } else if (position < computerPosition) {
                expectedLeader = getPlayerName(driver, 2).toLowerCase();
            }
            String leader = getLeader(driver);
            checkThat("Incorrect Leader: " + leader + " (expected: " + expectedLeader
                    + ") because player position is " + position + " and computer position is "
                    + computerPosition, leader, is(expectedLeader));

            if (position == 6) {
                onceWon = true;
                finished = true;
            }
            if (computerPosition == 6) {
                onceLost = true;
                finished = true;
            }
            if (!multipleTested && (position != 0 || computerPosition != 0)) {
                multipleTested = true;
                WebDriver secondDriver = null;

                try {
                    secondDriver = createDriver();
                    checkInitialState(secondDriver, "Concurrent test");
                } finally {
                    if (secondDriver != null) {
                        secondDriver.quit();
                    }
                }
            }
        }

        if (!finishExportDone && finished) {
            exportCurrentHTML(driver, "finish.html");
            finishExportDone = true;
        }

        // try to restart

        startNewGame(driver);

        Integer player1Position = getPlayerPosition(driver, 1);
        Integer player2Position = getPlayerPosition(driver, 2);

        checkThat("Player position not 0 after restart", player1Position, is(0));
        checkThat("Computer position not 0 after restart", player2Position, is(0));

        if (player1Position == null || player1Position != 0 || player2Position == null
                || player2Position != 0) {
            break;
        }
    }

    checkThat("Oily field never reached", jumpedOnOilyField, is(true));
    checkThat("Concurrent test could not be executed", multipleTested, is(true));
    checkThat("Player couldn't win at least once after " + MAX_TRIES + " games", onceLost, is(true));
    checkThat("Computer couldn't win at least once after " + MAX_TRIES + " games", onceWon, is(true));
}

From source file:at.ac.tuwien.big.testsuite.impl.selenium.TableTest.java

License:Apache License

private Integer getDiceValue(WebDriver driver) {
    String src = null;//from w  w w . ja  v a  2s .c o m

    if (exists(driver, By.id(diceFormPrefix + "diceImage"))) {
        src = driver.findElement(By.id(diceFormPrefix + "diceImage")).getAttribute("src");
    } else if (exists(driver, By.id(diceFormPrefix + "dice"))) {
        WebElement diceElement = driver.findElement(By.id(diceFormPrefix + "dice"));
        src = diceElement.getAttribute("src");

        if (src == null) {
            List<WebElement> imgElems = diceElement.findElements(By.xpath(".//img"));

            if (!imgElems.isEmpty()) {
                diceElement = imgElems.get(0);
                src = diceElement.getAttribute("src");
            }
        }
    }

    if (src == null) {
        assertFalse(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Could not retrieve dice value", true);
    }

    Matcher matcher = DICE_VALUE_PATTERN.matcher(src);

    if (!matcher.matches()) {
        assertFalse(TestsuiteConstants.KNOWN_ERROR_PREFIX + " Could not retrieve dice value", true);
    }

    return Integer.parseInt(matcher.group(1));
}

From source file:at.tugraz.ist.catroweb.admin.AdminTests.java

License:Open Source License

@Test(groups = { "functionality", "upload",
        "popupwindows" }, description = "check report as inappropriate functionality")
public void inappropriateProjects() throws Throwable {
    try {/*from ww  w  .ja  v a  2s  . c  o m*/
        login("details/1");

        assertTrue(isElementPresent(By.id("reportAsInappropriateButton")));
        driver().findElement(By.id("reportAsInappropriateButton")).click();
        driver().findElement(By.id("reportInappropriateReason")).sendKeys("my selenium reason");
        driver().findElement(By.id("reportInappropriateReportButton")).click();
        ajaxWait();
        assertTrue(isTextPresent("You reported this project as inappropriate!"));
        openAdminLocation("/tools/inappropriateProjects");
        assertTrue(isTextPresent("1"));

        clickAndWaitForPopUp(By.xpath("//a[@id='detailsLink1']"));
        assertTrue(isTextPresent("testproject".toUpperCase()));
        closePopUp();

        clickOkOnNextConfirmationBox();
        driver().findElement(By.id("resolve1")).click();
        assertTrue(isTextPresent("The project was succesfully restored and set to visible!"));
        assertFalse(isTextPresent("1"));
    } catch (AssertionError e) {
        captureScreen("AdminTests.inappropriateProjects");
        throw e;
    } catch (Exception e) {
        captureScreen("AdminTests.inappropriateProjects");
        throw e;
    }
}

From source file:at.tugraz.ist.catroweb.admin.LanguageManagementTests.java

License:Open Source License

@Test(groups = { "functionality" }, description = "update language packs")
public void updateLanguagePack() throws Throwable {
    try {//from ww  w  . j  a  v  a  2  s . co  m
        openAdminLocation("?userLanguage=" + Config.SITE_DEFAULT_LANGUAGE);
        driver().findElement(By.id("aAdministrationTools")).click();
        ajaxWait();
        driver().findElement(By.id("aAdminToolsLanguageManagement")).click();
        assertTrue(isTextPresent("Administration Tools - Language Management"));
        assertTrue(isElementPresent(By.xpath("//select[@id='supportedLanguageSelect']")));
        assertTrue(isElementPresent(By.xpath("//a[@id='doUpdateLink']")));
        (new Select(driver().findElement(By.id("supportedLanguageSelect")))).selectByValue("de");
        driver().findElement(By.id("doUpdateLink")).click();
        ajaxWait();
        assertTrue(isTextPresent("The language de was successfully updated!"));
        driver().findElement(By.id("aAdminToolsBackToTools")).click();
        assertTrue(isTextPresent("Administration Tools"));
    } catch (AssertionError e) {
        captureScreen("LanguageManagementTests.updateLanguagePack");
        throw e;
    } catch (Exception e) {
        captureScreen("LanguageManagementTests.updateLanguagePack");
        throw e;
    }
}

From source file:at.tugraz.ist.catroweb.catroid.BlockedIpTests.java

License:Open Source License

@Test(dataProvider = "blockedIps", groups = { "functionality" }, description = "test blocked ips")
public void blockedIps(String projectId, String blockedIp) throws Throwable {
    try {/*  w  w  w.j  a va 2  s.co m*/
        unblockAllIPs();
        openLocation();
        blockIp(blockedIp);
        openLocation("details/" + projectId);
        ajaxWait();
        assertTrue(containsElementText(By.xpath("//*[@id='wrapper']/article/div"), "Error".toUpperCase()));
        assertTrue(isTextPresent("Your IP-Address has been blocked."));

        openLocation();
        ajaxWait();
        assertFalse(isTextPresent("Your IP-Address has been blocked."));
        unblockIp(blockedIp);
    } catch (AssertionError e) {
        captureScreen("BlockedIpTests.blockedIps." + blockedIp);
        throw e;
    } catch (Exception e) {
        captureScreen("BlockedIpTests.blockedIps." + blockedIp);
        throw e;
    }
}