Example usage for org.openqa.selenium By id

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

Introduction

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

Prototype

public static By id(String id) 

Source Link

Usage

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 ww.j a va 2s.  co 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.ac.tuwien.big.testsuite.impl.selenium.TableTest.java

License:Apache License

private Integer getPlayerPosition(WebDriver driver, int playerNumber) {
    WebElement parentElement = getParentElement(driver, By.id("player" + playerNumber));

    if (parentElement == null) {
        return null;
    }/*from   ww  w.  j a  v  a  2  s  .c o m*/

    String id = parentElement.getAttribute("id");

    if ("start_road".equals(id)) {
        return 0;
    } else if ("finish_road".equals(id)) {
        return 6;
    } else if (id.startsWith("road_")) {
        return Integer.parseInt(id.substring("road_".length()));
    } else {
        return null;
    }
}

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

License:Apache License

private Boolean isOily(WebDriver driver, int roadId) {
    WebElement road = driver.findElement(By.id("road_" + roadId));
    return road == null ? null : road.getAttribute("class").contains("oil_road");
}

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

License:Apache License

private void startNewGame(WebDriver driver) {
    if (exists(By.id("startNewGame"))) {
        clickNonAjax(driver, By.id("startNewGame"));
        waitForJQuery(driver);//from  www. j ava2  s. c  om
    } else if (exists(By.id(navigationFormPrefix + "startNewGame"))) {
        clickNonAjax(driver, By.id(navigationFormPrefix + "startNewGame"));
        waitForJQuery(driver);
    } else {
        checkThat("The start new game link does not exist", true, is(false));
    }
}

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

License:Apache License

private Integer rollDice(WebDriver driver) {
    By diceId = By.id(diceFormPrefix + "dice");
    clickNonAjax(driver, diceId);//w  w  w  .  j  a v a  2  s.  c o m
    waitForJQuery(driver);
    return getDiceValue(driver);
}

From source file:at.ac.tuwien.big.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * tests the game according to some of the specifications of lab2. (Check
 * category: Main)//from   www . j a  va 2s.  c  om
 * 
 * Performed checks:
 * <ul>
 * <li>Game Run checks</li>
 * </ul>
 */
@Test
public void gameTest() {
    currentCheckCategory.push("Main");
    for (currentRun = 0; currentRun < numberOfRuns; currentRun++) {

        if (currentRun == 0) {
            driver.findElement(By.id(ID_START_GAME)).submit();
        }

        checkGameRun();

        // restart game
        driver.findElement(By.id(ID_NEW_GAME)).click();
    }
    currentCheckCategory.pop();
}

From source file:at.ac.tuwien.big.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * checks a single game run (Check Category: GameRun)
 * //from   w  ww.  j ava  2s .  com
 * Performed checks:
 * <ul>
 * <li>Question page checks</li>
 * <li>Round page checks</li>
 * <li>Game over page checks</li>
 * </ul>
 */
private void checkGameRun() {
    currentCheckCategory.push("GameRun");
    answers = new ArrayList<>();
    totalQuestions = 0;

    player1Victories = 0;
    player2Victories = 0;

    namePlayer1 = null;
    namePlayer2 = null;

    determinePlayerNames();

    for (int n = 0; n < NUM_ROUNDS; n++) {

        for (int i = 0; i < NUM_QUESTIONS; i++) {
            // totalQuestions = n * NUM_QUESTIONS + i;
            Question question = checkQuestionPage();
            answerQuestion(question, totalQuestions);
            driver.findElement(By.id(ID_NEXT_QUESTION)).submit();
            totalQuestions++;
        }

        if (n < NUM_ROUNDS - 1) { // round complete

            checkRoundPage();
            driver.findElement(By.id(ID_NEXT_ROUND)).click();
        } else { // game over
            checkGameOverPage();
        }
    }
    currentCheckCategory.pop();
}

From source file:at.ac.tuwien.big.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * checks the game over page. (Check Category: GameOverPage)
 * /*from w  w w.  j a v  a 2s.  co m*/
 * Performed checks:
 * <ul>
 * <li>Player name check</li>
 * <li>Round victories check (derived)</li>
 * <li>Winner check (derived)</li>
 * </ul>
 */
private void checkGameOverPage() {
    currentCheckCategory.push("GameOverPage");

    // check player name
    errorCollector.checkThat(getTestInfoPrefix() + "Invalid Player name",
            driver.findElement(By.id(ID_PLAYER_NAME_1)).getText(), isIn(EXPECTED_PLAYER_NAMES));
    errorCollector.checkThat(getTestInfoPrefix() + "Invalid Player name",
            driver.findElement(By.id(ID_PLAYER_NAME_2)).getText(), isIn(EXPECTED_PLAYER_NAMES));

    // check round victories - derived, as the result of the last
    // round can't be determined in a reliable way

    // try to derive the result of the last round
    if (driver.findElement(By.id(ID_ROUND_VICTORIES_PLAYER_1)).getText().trim()
            .contains("" + (player1Victories + 1))) {
        player1Victories++;
    } else if (driver.findElement(By.id(ID_ROUND_VICTORIES_PLAYER_2)).getText().trim()
            .contains("" + (player2Victories + 1))) {
        player2Victories++;
    }

    errorCollector.checkThat("Invalid number of round victories",
            driver.findElement(By.id(ID_ROUND_VICTORIES_PLAYER_1)).getText(),
            containsString("" + player1Victories));
    errorCollector.checkThat("Invalid number of round victories",
            driver.findElement(By.id(ID_ROUND_VICTORIES_PLAYER_2)).getText(),
            containsString("" + player2Victories));

    // Winner check - derived, as the result of the last
    // round can't be determined in a reliable way
    Matcher<String> player1nameMatcher = containsString(namePlayer1);
    Matcher<String> player2nameMatcher = containsString(namePlayer2);
    Matcher<String> drawMatcher = containsString("Unentschieden");

    if (player1Victories == player2Victories) {
        errorCollector.checkThat(getTestInfoPrefix() + "Invalid winner",
                driver.findElement(By.id(ID_WINNER)).getText(),
                allOf(drawMatcher, not(player1nameMatcher), not(player2nameMatcher)));
    } else if (player1Victories > player2Victories) {
        errorCollector.checkThat(getTestInfoPrefix() + "Invalid winner",
                driver.findElement(By.id(ID_WINNER)).getText(),
                allOf(player1nameMatcher, not(drawMatcher), not(player2nameMatcher)));
    } else {
        errorCollector.checkThat(getTestInfoPrefix() + "Invalid winner",
                driver.findElement(By.id(ID_WINNER)).getText(),
                allOf(player2nameMatcher, not(drawMatcher), not(player1nameMatcher)));
    }

    currentCheckCategory.pop();
}

From source file:at.ac.tuwien.big.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * checks the round winner// ww  w .j a  va  2 s .  c o  m
 * 
 * Performed checks:
 * <ul>
 * <li>Round victories check</li>
 * <li>Round winner name/draw check</li>
 * </ul>
 * 
 * @param totalQuestionCount
 */
private void checkRoundWinner(int totalQuestionCount) {

    int correctPlayer1 = 0;
    int correctPlayer2 = 0;

    for (int i = 0; i < NUM_QUESTIONS; i++) {
        Answer answer = answers.get(totalQuestionCount - NUM_QUESTIONS + i);
        if (answer.isCorrect()) {
            correctPlayer1++;
        }

        String sResultP2 = driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_1 + i)).getText();
        if (sResultP2.equalsIgnoreCase(EXPECTED_ANSWER_VALUE_CORRECT)) {
            correctPlayer2++;
        } else if (!sResultP2.equalsIgnoreCase(EXPECTED_ANSWER_VALUE_INCORRECT)) {
            errorCollector.addError(new Exception(getTestInfoPrefix()
                    + "Could not parse answer value of player 2, conditional errors may occur. Expected: \""
                    + EXPECTED_ANSWER_VALUE_CORRECT + "\" or \"" + EXPECTED_ANSWER_VALUE_INCORRECT + "\""));
        }
    }

    // check winner message
    if (correctPlayer1 == correctPlayer2) {

        // draw - we can't determine the actual winner because we have no
        // reliable time information

        Matcher<String> player1nameMatcher = containsString(namePlayer1);
        Matcher<String> player2nameMatcher = containsString(namePlayer2);
        Matcher<String> drawMatcher = containsString("Unentschieden");
        String roundWinner = driver.findElement(By.id(ID_ROUND_WINNER)).getText();

        errorCollector.checkThat(getTestInfoPrefix() + "Invalid round winner, conditional errors may occur",
                roundWinner, anyOf(player2nameMatcher, player1nameMatcher, drawMatcher));

        // update the round score, assume the winner is correct
        if (player1nameMatcher.matches(roundWinner)) {
            player1Victories++;
        } else if (player2nameMatcher.matches(roundWinner)) {
            player2Victories++;
        }

    } else if (correctPlayer1 > correctPlayer2) {

        errorCollector.checkThat(getTestInfoPrefix() + "Invalid winner",
                driver.findElement(By.id(ID_ROUND_WINNER)).getText(), containsString(namePlayer1));
        player1Victories++;
    } else {

        errorCollector.checkThat(getTestInfoPrefix() + "Invalid winner",
                driver.findElement(By.id(ID_ROUND_WINNER)).getText(), containsString(namePlayer2));
        player2Victories++;
    }

    // check round victory counter
    errorCollector.checkThat("Inavlid number of round victories",
            driver.findElement(By.id(ID_ROUND_VICTORIES_PLAYER_1)).getText(),
            containsString("" + player1Victories));
    errorCollector.checkThat("Inavlid number of round victories",
            driver.findElement(By.id(ID_ROUND_VICTORIES_PLAYER_2)).getText(),
            containsString("" + player2Victories));

}

From source file:at.ac.tuwien.big.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * determines and checks the chosen player names
 * /*ww  w.  ja va2s.c  o  m*/
 * Performed checks:
 * <ul>
 * <li>Player name check</li>
 * </ul>
 */
private void determinePlayerNames() {
    namePlayer1 = driver.findElement(By.id(ID_PLAYER_NAME_1)).getText();
    errorCollector.checkThat(getTestInfoPrefix()
            + "Invalid Player name, tests will use the invalid name for following name dependent checks",
            namePlayer1, isIn(EXPECTED_PLAYER_NAMES));

    namePlayer2 = driver.findElement(By.id(ID_PLAYER_NAME_2)).getText();
    errorCollector.checkThat(getTestInfoPrefix()
            + "Invalid Player name, tests will use the invalid name for following name dependent checks ",
            namePlayer2, isIn(EXPECTED_PLAYER_NAMES));
}