Example usage for org.openqa.selenium WebElement getAttribute

List of usage examples for org.openqa.selenium WebElement getAttribute

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getAttribute.

Prototype

String getAttribute(String name);

Source Link

Document

Get the value of the given attribute of the element.

Usage

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  w ww .j  a v a2 s . co 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.we14.lab2.tests.SeleniumTest.java

License:Open Source License

/**
 * answers the question with a specific answer type
 * //  www  .  j a  va 2 s  .c om
 * @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.SendEmailNotificationTests.java

License:Open Source License

@Test(groups = { "functionality",
        "sendEmail" }, description = "select checkboxes one by one and by using select all checkbox")
public void checkCheckboxes() throws Throwable {
    try {//from   w  w  w .j  a v a  2  s.c o m
        int checked = 0;
        openAdminLocation();
        ajaxWait();
        driver().findElement(By.id("aAdministrationTools")).click();
        ajaxWait();
        driver().findElement(By.id("aAdminToolsSendEmailNotification")).click();
        ajaxWait();

        assertTrue(isTextPresent(" (" + checked + " user(s) selected)"));

        List<WebElement> checkboxes = driver().findElements(By.className("chkBoxEmail"));
        for (WebElement checkbox : checkboxes) {
            assertFalse(checkbox.isSelected());
        }

        for (WebElement checkbox : checkboxes) {
            driver().findElement(By.id(checkbox.getAttribute("id"))).click();
            checked++;
            assertTrue(isTextPresent(" (" + checked + " user(s) selected)"));
            assertTrue(checkbox.isSelected());
        }

        assertTrue(isTextPresent("unselect all"));
        // unselect all checkboxes by clicking on unselect all checkbox
        checked = 0;
        driver().findElement(By.id("chkboxSelectAll")).click();
        ajaxWait();

        for (WebElement checkbox : checkboxes) {
            assertFalse(checkbox.isSelected());
        }
        assertTrue(isTextPresent("select all"));
        assertTrue(isTextPresent(" (" + checked + " user(s) selected)"));

        // select all checkboxes by clicking on select all checkbox
        driver().findElement(By.id("chkboxSelectAll")).click();
        for (WebElement checkbox : checkboxes) {
            checked++;
            assertTrue(checkbox.isSelected());
        }
        assertTrue(isTextPresent("unselect all"));
        assertTrue(isTextPresent(" (" + checked + " user(s) selected)"));

    } catch (AssertionError e) {
        captureScreen("SendEmailNotificationTests.checkCheckboxes");
        throw e;
    } catch (Exception e) {
        captureScreen("SendEmailNotificationTests.checkCheckboxes");
        throw e;
    }
}

From source file:automation.CenturyLink.java

@After
public void tearDown() throws Exception {
    Thread.sleep(60 * 1000);//from   w  w w  .j  ava2s  .  c o  m
    try {
        WebElement resultElement = driver
                .findElement(By.id("ctl00_ContentPlaceHolder1_BackboneActiveTestControl_ActiveTestResultsTxb"));
        result = resultElement.getAttribute("innerHTML");
        result = result.replaceAll("[\u0000-\u0008]", "");
        result = result.replaceAll("[\u000B-\u001f]", "");
        result = replaceBrTag(result);
        result = removeTags(result);
        result = replaceHtmlString(result);

    } catch (Exception e) {
        Logger.getLogger(BT.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        super.store();
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}

From source file:be.rubus.web.jerry.interceptor.RendererInterceptorTest.java

License:Apache License

@Test
@RunAsClient/*from   ww w  . j a  v a  2  s  .  c  o  m*/
public void testInterceptor() throws Exception {
    driver.get(new URL(contextPath, "page.xhtml").toString());

    WebElement inputField = driver.findElement(By.id("test:valueInput"));
    String styleClass = inputField.getAttribute("class");
    assertThat(styleClass).isEqualTo("inputField");
}

From source file:be.rubus.web.jerry.validation.CombinedTest.java

License:Apache License

@Test
@RunAsClient//from ww w. j av  a2s.  c  om
public void testCombined() throws Exception {
    driver.get(new URL(contextPath, "combined.xhtml").toString());

    WebElement element = driver.findElement(By.id("test:value"));
    assertThat(element.getAttribute("maxlength")).isNull();

    element = driver.findElement(By.id("test:valueLabel"));
    String text = element.getText();
    assertThat(text).isEqualTo("value");

    element = driver.findElement(By.id("test:required"));
    assertThat(element.getAttribute("maxlength")).isNull();

    element = driver.findElement(By.id("test:requiredLabel"));
    text = element.getText();
    assertThat(text).isEqualTo("required*");

    element = driver.findElement(By.id("test:maxLength"));
    assertThat(element.getAttribute("maxlength")).isEqualTo("14");

    element = driver.findElement(By.id("test:maxLengthLabel"));
    text = element.getText();
    assertThat(text).isEqualTo("max length");

    element = driver.findElement(By.id("test:combined"));
    assertThat(element.getAttribute("maxlength")).isEqualTo("14");

    element = driver.findElement(By.id("test:combinedLabel"));
    text = element.getText();
    assertThat(text).isEqualTo("combined*");

}

From source file:be.rubus.web.jerry.validation.CustomTest.java

License:Apache License

@Test
@RunAsClient/* w  ww . j ava 2s .c  om*/
public void testCustom() throws Exception {
    driver.get(new URL(contextPath, "custom.xhtml").toString());

    WebElement element = driver.findElement(By.id("test:zipCodeMaskLabel"));
    String text = element.getText();
    assertThat(text).isEqualTo("Zip code (mask)*");

    element = driver.findElement(By.id("test:zipCode"));
    assertThat(element.getAttribute("maxlength")).isEqualTo("4");

    element = driver.findElement(By.id("test:zipCodeMask"));

    element.sendKeys("azer");
    // Only numbers allowed in the mask
    assertThat(element.getAttribute("value")).isEqualTo("____");

    element.clear();
    element.sendKeys("12345");
    // Only 4 numbers allowed in the mask
    assertThat(element.getAttribute("value")).isEqualTo("1234");

}

From source file:be.rubus.web.jerry.validation.FutureDateProviderTest.java

License:Apache License

@Test
@RunAsClient/*from  www  . j a  v a 2 s .  co  m*/
public void testFuture() throws Exception {
    driver.get(new URL(contextPath, "future.xhtml").toString());

    //By default, the date provider is using system date, so failures
    WebElement date1 = driver.findElement(By.id("test:date1"));
    date1.sendKeys("23/02/2015");

    WebElement date2 = driver.findElement(By.id("test:date2"));
    date2.sendKeys("23/02/2015");

    assertThat(date1.getAttribute("class")).doesNotContain("ui-state-error");
    assertThat(date2.getAttribute("class")).doesNotContain("ui-state-error");

    WebElement btn = driver.findElement(By.id("test:submit"));
    btn.click();

    WebElement errors = driver.findElement(By.id("errors"));

    List<WebElement> errorMessages = errors.findElements(By.tagName("li"));
    assertThat(errorMessages).hasSize(2);

    // Page is reloaded so elements are detached

    date1 = driver.findElement(By.id("test:date1"));
    date2 = driver.findElement(By.id("test:date2"));

    assertThat(date1.getAttribute("class")).contains("ui-state-error");
    assertThat(date2.getAttribute("class")).contains("ui-state-error");

    // Set date for DateProvider
    WebElement fixedNow = driver.findElement(By.id("date:fixedNow"));
    fixedNow.clear();
    fixedNow.sendKeys("20/02/2015");

    WebElement dateBtn = driver.findElement(By.id("date:setDate"));
    dateBtn.click();

    date1 = driver.findElement(By.id("test:date1"));
    date1.sendKeys("23/02/2015");

    date2 = driver.findElement(By.id("test:date2"));
    date2.sendKeys("23/02/2015");

    btn = driver.findElement(By.id("test:submit"));
    btn.click();

    errors = driver.findElement(By.id("errors"));

    errorMessages = errors.findElements(By.tagName("li"));
    assertThat(errorMessages).hasSize(1); // Only 1 error now

    // Page is reloaded so elements are detached

    date1 = driver.findElement(By.id("test:date1"));
    date2 = driver.findElement(By.id("test:date2"));

    assertThat(date1.getAttribute("class")).contains("ui-state-error");
    assertThat(date2.getAttribute("class")).doesNotContain("ui-state-error"); // The one withValFuture

}

From source file:be.rubus.web.jerry.validation.FutureTest.java

License:Apache License

@Test
@RunAsClient/*from w ww  .j  ava2s. c om*/
public void testFuture() throws Exception {
    driver.get(new URL(contextPath, "future_NoProvider.xhtml").toString());

    WebElement date1 = driver.findElement(By.id("test:date1"));
    date1.sendKeys("23/02/2015");

    WebElement date2 = driver.findElement(By.id("test:date2"));
    date2.sendKeys("23/02/2015");

    WebElement btn = driver.findElement(By.id("test:submit"));
    btn.click();

    WebElement errors = driver.findElement(By.id("errors"));

    List<WebElement> errorMessages = errors.findElements(By.tagName("li"));
    assertThat(errorMessages).hasSize(2);

    // Page is reloaded so elements are detached

    date1 = driver.findElement(By.id("test:date1"));
    date2 = driver.findElement(By.id("test:date2"));

    assertThat(date1.getAttribute("class")).contains("ui-state-error");
    assertThat(date2.getAttribute("class")).contains("ui-state-error");
}