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.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * checks the player information contained on the question and round pages
 * //from w  w  w.  ja  v a2 s .c om
 * Performed checks:
 * <ul>
 * <li>Player name consistency check</li>
 * <li>Question result check</li>
 * </ul>
 * 
 * @param questionCount
 * @param totalQuestionCount
 */
private void checkPlayerInfo(int questionCount, int totalQuestionCount) {
    // check player name
    errorCollector.checkThat(getTestInfoPrefix() + "Inconsistent player name for player 1",
            driver.findElement(By.id(ID_PLAYER_NAME_1)).getText(), containsString(namePlayer1));
    errorCollector.checkThat(getTestInfoPrefix() + "Inconsistent player name for player 2",
            driver.findElement(By.id(ID_PLAYER_NAME_2)).getText(), containsString(namePlayer2));

    // check answers
    for (int i = 0; i < NUM_QUESTIONS; i++) {
        if (i < questionCount) {
            errorCollector.checkThat(
                    getTestInfoPrefix() + "Invalid answer value for question " + i
                            + " in player statistics of player 1",
                    driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_1 + i)).getText(),
                    not(equalTo(EXPECTED_ANSWER_VALUE_UNKNOWN)));
            errorCollector.checkThat(
                    getTestInfoPrefix() + "Invalid answer value for round question " + i
                            + " in player statistics of player 2",
                    driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_2 + i)).getText(),
                    not(equalTo(EXPECTED_ANSWER_VALUE_UNKNOWN)));

            // check exact value
            Answer answer = answers.get(totalQuestionCount - questionCount + i);

            if (answer.isCorrect()) {
                errorCollector.checkThat(
                        getTestInfoPrefix() + "Invalid answer value for round question " + i
                                + " in player statistics of player 1. Given answer: " + answer,
                        driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_1 + i)).getText(),
                        equalTo(EXPECTED_ANSWER_VALUE_CORRECT));
            } else {
                errorCollector.checkThat(
                        getTestInfoPrefix() + "Invalid answer value for round question " + i
                                + " in player statistics of player 1. Given answer: " + answer,
                        driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_1 + i)).getText(),
                        equalTo(EXPECTED_ANSWER_VALUE_INCORRECT));
            }

        } else {
            errorCollector.checkThat(
                    getTestInfoPrefix() + "Invalid answer value for round question " + i
                            + " in player statistics of player 1",
                    driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_1 + i)).getText(),
                    equalTo(EXPECTED_ANSWER_VALUE_UNKNOWN));
            errorCollector.checkThat(
                    getTestInfoPrefix() + "Invalid answer value for round question " + i
                            + " in player statistics of player 2.",
                    driver.findElement(By.id(ID_PREFIX_PLAYER_ANSWER_2 + i)).getText(),
                    equalTo(EXPECTED_ANSWER_VALUE_UNKNOWN));
        }
    }

}

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

License:Open Source License

/**
 * checks the timer/*from w w  w .ja va 2s  .  c o m*/
 * 
 * Performed checks
 * <ul>
 * <li>time counter format check</li>
 * <li>time counter value check</li>
 * <li>time counter update check</li>
 * </ul>
 * 
 * @param question
 *            the current question
 * 
 */
private void checkTimer(Question question) {

    String timerValue = driver.findElement(By.id(ID_TIMER_VALUE)).getText().trim();
    int minutes = 0;
    int seconds = 0;

    // Format-Check
    java.util.regex.Matcher timerValueMatcher = Pattern.compile(TIMER_REGEX).matcher(timerValue);
    if (timerValueMatcher.matches()) {
        minutes = Integer.parseInt(timerValueMatcher.group(1));
        seconds = Integer.parseInt(timerValueMatcher.group(2));
    } else {
        errorCollector.addError(new Exception(getTestInfoPrefix()
                + "Invalid timer format. Expected format(Regular expression): \"" + TIMER_REGEX + "\""));
    }

    // Value-Check
    if (question != null) {
        if (timerValueMatcher.matches()) {
            minutes = Integer.parseInt(timerValueMatcher.group(1));
            seconds = Integer.parseInt(timerValueMatcher.group(2));

            int timeInSeconds = minutes * 60 + seconds;

            errorCollector.checkThat(getTestInfoPrefix() + "Invalid timer value (in seconds)", timeInSeconds,
                    allOf(lessThanOrEqualTo(timeInSeconds + timeCounterValueTolerance),
                            greaterThanOrEqualTo(timeInSeconds - timeCounterValueTolerance)));
        }
    } else {
        errorCollector.addError(new Exception(getTestInfoPrefix()
                + "Consequential Error: Couldn't check timer value completely because the question could not be determined "));
    }

    // Update-Check
    // wait a random amount of time
    int sleepTime = (int) (Math.random() * maxTimeCounterTestSleep) + 1;
    Sleeper.sleepTightInSeconds(sleepTime);

    // check values again
    int newMinutes = 0;
    int newSeconds = 0;
    timerValue = driver.findElement(By.id(ID_TIMER_VALUE)).getText().trim();
    timerValueMatcher = Pattern.compile(TIMER_REGEX).matcher(timerValue);
    if (timerValueMatcher.matches()) {
        newMinutes = Integer.parseInt(timerValueMatcher.group(1));
        newSeconds = Integer.parseInt(timerValueMatcher.group(2));

        int expectedTime = minutes * 60 + seconds;
        int newTime = newMinutes * 60 + newSeconds;

        errorCollector.checkThat(
                getTestInfoPrefix() + "Invalid timer value (in seconds) after " + sleepTime + "s", newTime,
                allOf(lessThanOrEqualTo(expectedTime - sleepTime + timeCounterValueTolerance),
                        greaterThanOrEqualTo(expectedTime - sleepTime - timeCounterValueTolerance)));

    } else {
        errorCollector.addError(new Exception(getTestInfoPrefix() + "Invalid timer format after " + sleepTime
                + "s. Expected format(Regular expression): \"" + TIMER_REGEX + "\""));
    }
}

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

License:Open Source License

/**
 * checks the question// w  w  w  .j a v a  2  s .  c  o  m
 * 
 * Performed checks
 * <ul>
 * <li>Category name check</li>
 * <li>Question text check</li>
 * <li>Choice text check</li>
 * </ul>
 * 
 * @return
 */
private Question checkQuestion() {
    int id = -1;
    try {
        id = Integer.parseInt(driver.findElement(By.id(ID_QUESTION_ID)).getAttribute("value"));
    } catch (NumberFormatException nfe) {
    }
    if (id >= 0) {
        Question question = questions.get(id);
        if (question != null) {

            // check question text and category

            errorCollector.checkThat(getTestInfoPrefix() + "Unexpected category name",
                    driver.findElement(By.id(ID_CATEGORY_NAME)).getText(),
                    containsString(question.getCategory().getName()));
            errorCollector.checkThat(getTestInfoPrefix() + "Unexpected question text",
                    driver.findElement(By.id(ID_QUESTION_TEXT)).getText(), containsString(question.getText()));

            // check choice labels

            List<String> allChoices = new ArrayList<>();
            for (Choice choice : question.getWrongChoices()) {
                allChoices.add(choice.getText());
            }
            for (Choice choice : question.getCorrectChoices()) {
                allChoices.add(choice.getText());
            }

            for (int i = 0; i < allChoices.size(); i++) {
                errorCollector.checkThat(getTestInfoPrefix() + "Invalid choice label",
                        driver.findElement(By.id(ID_PREFIX_CHOICE_LABEL + i)).getText().trim(),
                        isIn(allChoices));
            }
            return question;
        } else {
            errorCollector.addError(new Exception(getTestInfoPrefix() + "Unknown question id"));
        }
    } else {
        errorCollector.addError(new Exception(getTestInfoPrefix() + "Couldn't determine question by id"));
    }
    return null;
}

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

License:Open Source License

/**
 * answers the question with a specific answer type
 * // w  w w.  j a  v a2  s.  co  m
 * @param question
 *            the question to answer (not null)
 * @param answerType
 *            the answer type
 */
private void answerQuestion(Question question, AnswerType answerType) {
    List<Choice> choices = answerType.createChoices(question.getWrongChoices(), question.getCorrectChoices());
    int totalChoices = question.getCorrectChoices().size() + question.getWrongChoices().size();
    List<Choice> selectedChoices = new ArrayList<>();
    for (int i = 0; i < totalChoices; i++) {
        WebElement choiceLabel = driver.findElement(By.id(ID_PREFIX_CHOICE_LABEL + i));
        WebElement checkbox = driver.findElement(By.id(choiceLabel.getAttribute("for")));
        for (Choice choice : choices) {
            if (choiceLabel.getText().contains(choice.getText())) {
                // click on a potentially invisible checkbox
                ((JavascriptExecutor) driver).executeScript("arguments[0].click()", checkbox);
                selectedChoices.add(choice);
                break;
            }
        }
    }

    if (selectedChoices.size() != choices.size()) {
        errorCollector.addError(
                new Exception(getTestInfoPrefix() + "Couldn't select the following choices: " + choices));
    }
    answers.add(new Answer(question, answerType, choices));
}

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

License:Open Source License

@Test(groups = { "visibility" }, description = "check admin area login")
public void successfulLogin() throws Throwable {
    try {/*from w  w  w.  j a  v  a  2s  .c o m*/
        openAdminLocation();
        assertRegExp(".*Administration - Catroid Website.*", driver().getTitle());
        assertTrue(isTextPresent("Administration Tools"));
        driver().findElement(By.id("aAdminToolsBackToCatroidweb")).click();
        ajaxWait();
        assertRegExp(".*Pocket Code Website.*", driver().getTitle());
        driver().navigate().back();
        if (isTextPresent("Catroid Administration Site") == false) {
            driver().navigate().back();
        }
        assertTrue(isTextPresent("Catroid Administration Site"));
    } catch (AssertionError e) {
        captureScreen("AdminTests.successfulLogin");
        throw e;
    } catch (Exception e) {
        captureScreen("AdminTests.successfulLogin");
        throw e;
    }
}

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

License:Open Source License

@Test(groups = { "visibility" }, description = "clicks all available links in the admin area")
public void clickAllLinks() throws Throwable {
    try {/* www  .j ava 2s.c  om*/
        openAdminLocation();
        assertRegExp(".*Administration - Catroid Website.*", driver().getTitle());
        assertTrue(isTextPresent("Catroid Administration Site"));

        driver().findElement(By.id("aAdministrationTools")).click();
        assertTrue(isTextPresent("Administration Tools"));
        assertTrue(isTextPresent("remove inconsistant project files"));
        assertTrue(isTextPresent("edit projects"));
        assertTrue(isTextPresent("add featured projects"));
        assertTrue(isTextPresent("edit featured projects"));
        assertTrue(isTextPresent("thumbnail uploader"));
        assertTrue(isTextPresent("inappropriate projects"));
        assertTrue(isTextPresent("approve unapproved words"));
        assertTrue(isTextPresent("manage Languages"));
        assertTrue(isTextPresent("block IPs"));
        assertTrue(isTextPresent("block Users"));
        assertTrue(isTextPresent("send e-mail notification"));

        assertRegExp(".*Administration - Catroid Website.*", driver().getTitle());

        driver().findElement(By.id("aAdminToolsRemoveInconsitantProjectFiles")).click();
        assertTrue(isTextPresent("Answer"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsEditProjects")).click();
        assertTrue(isTextPresent("Administration Tools - List of available projects"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsAddFeaturedProjects")).click();
        assertTrue(isTextPresent("Administration Tools - Add Featured Projects"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsEditFeaturedProjects")).click();
        assertTrue(isTextPresent("Administration Tools - Edit Featured Projects"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsThumbnailUploader")).click();
        assertTrue(isTextPresent("Administration Tools - Thumbnail Uploader"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsInappropriateProjects")).click();
        assertTrue(isTextPresent("Administration Tools - List of inappropriate projects"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsApproveWords")).click();
        assertTrue(isTextPresent("Administration Tools - List of unapproved Words"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsLanguageManagement")).click();
        assertTrue(isTextPresent("Administration Tools - Language Management"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsBlockIp")).click();
        assertTrue(isTextPresent("Administration Tools - List of blocked IP-Addresses"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsBlockUser")).click();
        assertTrue(isTextPresent("Administration Tools - List of blocked users"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsUpdateBrowserDetection")).click();
        assertTrue(isTextPresent("Administration Tools - Update browser-detection RegEx-Pattern"));
        driver().navigate().back();
        ajaxWait();

        driver().findElement(By.id("aAdminToolsSendEmailNotification")).click();
        assertTrue(isTextPresent("Administration Tools - Send e-mail notification"));
        driver().navigate().back();
        ajaxWait();

        assertTrue(isTextPresent("- back"));
        driver().findElement(By.id("aAdminToolsBackToCatroidweb")).click();
        assertTrue(isTextPresent("Catroid Administration Site"));
    } catch (AssertionError e) {
        captureScreen("AdminTests.clickAllLinks");
        throw e;
    } catch (Exception e) {
        captureScreen("AdminTests.clickAllLinks");
        throw e;
    }
}

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   www  .j a va  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.BadWordsFilterTests.java

License:Open Source License

@Test(groups = { "functionality", "upload" }, description = "approve an unapproved word as good")

public void approveButtonGood() throws Throwable {
    deleteAllUnapprovedWords();//from   ww  w.j av  a  2s  .  c o m
    try {
        String unapprovedWord = "badwordsfiltertestsapprovebuttongood" + CommonData.getRandomShortString(10);
        String response = projectUploader
                .upload(CommonData.getUploadPayload(unapprovedWord, "", "", "", "", "", "", ""));
        assertEquals("200", CommonFunctions.getValueFromJSONobject(response, "statusCode"));

        openAdminLocation();
        driver().findElement(By.id("aAdministrationTools")).click();
        ajaxWait();
        driver().findElement(By.id("aAdminToolsApproveWords")).click();
        ajaxWait();
        assertTrue(isTextPresent(unapprovedWord));

        (new Select(
                driver().findElement(By.id("meaning" + CommonFunctions.getUnapprovedWordId(unapprovedWord)))))
                        .selectByVisibleText("good");
        clickOkOnNextConfirmationBox();
        driver().findElement(By.id("approve" + CommonFunctions.getUnapprovedWordId(unapprovedWord))).click();
        assertTrue(isTextPresent("The word was succesfully approved!"));

        assertProjectPresent(unapprovedWord);

        deletePreviouslyUploadedProjectAndUnapprovedWord(unapprovedWord);
    } catch (AssertionError e) {
        captureScreen("BadWordsFilterTests.approveButtonGood");
        throw e;
    } catch (Exception e) {
        captureScreen("BadWordsFilterTests.approveButtonGood");
        throw e;
    }
}

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

License:Open Source License

@Test(groups = { "functionality", "upload" }, description = "approve an unapproved word as bad")
public void approveButtonBad() throws Throwable {
    deleteAllUnapprovedWords();/*from  w  w  w  .j a va2s  .  co m*/
    try {
        String unapprovedWord = "badwordsfiltertestsapprovebuttonbad" + CommonData.getRandomShortString(10);
        String response = projectUploader
                .upload(CommonData.getUploadPayload(unapprovedWord, "", "", "", "", "", "", ""));
        assertEquals("200", CommonFunctions.getValueFromJSONobject(response, "statusCode"));

        openAdminLocation();
        driver().findElement(By.id("aAdministrationTools")).click();
        ajaxWait();
        driver().findElement(By.id("aAdminToolsApproveWords")).click();
        assertTrue(isTextPresent(unapprovedWord));
        (new Select(
                driver().findElement(By.id("meaning" + CommonFunctions.getUnapprovedWordId(unapprovedWord)))))
                        .selectByVisibleText("bad");
        clickOkOnNextConfirmationBox();
        driver().findElement(By.id("approve" + CommonFunctions.getUnapprovedWordId(unapprovedWord))).click();
        assertTrue(isTextPresent("The word was succesfully approved!"));

        assertProjectNotPresent(unapprovedWord);

        deletePreviouslyUploadedProjectAndUnapprovedWord(unapprovedWord);
    } catch (AssertionError e) {
        captureScreen("BadWordsFilterTests.approveButtonBad");
        throw e;
    } catch (Exception e) {
        captureScreen("BadWordsFilterTests.approveButtonBad");
        throw e;
    }
}

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

License:Open Source License

@Test(groups = { "functionality", "upload" }, description = "approve an unapproved word with no selection")
public void approveButtonNoSelection() throws Throwable {
    deleteAllUnapprovedWords();/*ww  w .  j  a  v  a 2s .com*/
    try {
        String unapprovedWord = "badwordsfiltertestsapprovebuttonnoselection"
                + CommonData.getRandomShortString(10);
        String response = projectUploader
                .upload(CommonData.getUploadPayload(unapprovedWord, "", "", "", "", "", "", ""));
        assertEquals("200", CommonFunctions.getValueFromJSONobject(response, "statusCode"));

        openAdminLocation();
        driver().findElement(By.id("aAdministrationTools")).click();
        ajaxWait();
        driver().findElement(By.id("aAdminToolsApproveWords")).click();
        assertTrue(isTextPresent(unapprovedWord));
        clickOkOnNextConfirmationBox();
        driver().findElement(By.id("approve" + CommonFunctions.getUnapprovedWordId(unapprovedWord))).click();
        assertTrue(isTextPresent("Error: no word meaning selected!"));

        assertProjectPresent(unapprovedWord);

        deletePreviouslyUploadedProjectAndUnapprovedWord(unapprovedWord);
    } catch (AssertionError e) {
        captureScreen("BadWordsFilterTests.approveButtonNoSelection");
        throw e;
    } catch (Exception e) {
        captureScreen("BadWordsFilterTests.approveButtonNoSelection");
        throw e;
    }
}