Example usage for org.openqa.selenium WebElement findElements

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

Introduction

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

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current context using the given mechanism.

Usage

From source file:au.unick.testing.oos.lazylocators.BaseLazyLocator.java

License:Apache License

/**
 * Create and return list of WebElement available from WebDriver by locator
 * @param wd//  w ww .java  2  s.c o  m
 * @return
 */
public List<WebElement> getElements(WebDriver wd) {
    WebElement parentWebElement = getParentElement(wd);
    By by = by();
    if (null == by) {
        return null; // shall it return parent element in this case? Arrays.asList( parentWebElement );
    }
    if (null == parentWebElement) {
        return wd.findElements(by);
    } else {
        return parentWebElement.findElements(by);
    }
}

From source file:automation.HE.java

@After
public void tearDown() throws Exception {
    try {//from   ww w. ja  va 2s .  com
        //         result = driver.findElement(By.cssSelector(cssSelector)).getAttribute("innerHTML");
        result = "";
        WebElement table = driver.findElement(By.cssSelector(cssSelector));
        //         System.out.println("1" + table.getAttribute("innerHTML"));
        //         System.out.println("table:" + table.html());
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        //         System.out.println("2");
        for (WebElement row : rows) {
            for (WebElement cell : row.findElements(By.tagName("td"))) {
                result += cell.getText().trim() + "\t";
            }
            result += "\n";
            //            System.out.println(result);
            //            System.out.println("------");
        }
        System.out.println(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:automation.PCCWGlobalcom.java

@After
public void tearDown() throws Exception {
    Thread.sleep(60 * 1000);//w ww.j  a  v  a2 s.  c o  m
    try {
        WebElement resultElement = driver.findElement(By.id("test_results"));
        List<WebElement> rows = resultElement.findElements(By.tagName("tr"));
        for (WebElement row : rows) {
            result += row.getText().trim() + "\n";

        }
        System.out.println(result);

        //         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:backend.MakeUserTest.java

@Test(description = "check if new user was succesfully added", priority = 3, dependsOnMethods = { "makeUser" })
public void validateNewUser() throws InterruptedException {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    //sort descending(newest on top)
    WebElement table = driver.findElement(By.id("detailTable_person"));
    WebElement idCell = table.findElement(By.xpath("//th[text()='Id']"));
    idCell.click();// w ww. ja  v  a2s .  c o  m
    Thread.sleep(500);

    //select first table entry
    List<WebElement> tableRows = table.findElements(By.tagName("tr"));
    String userId = tableRows.get(2).findElement(By.xpath("//td[1]")).getText();
    String nameCell = tableRows.get(2).findElement(By.xpath("//td[2]")).getText();

    //validate if first entry is the new user. delete user if true, fails test if false
    if (nameCell.equals("testUser123")) {
        WebElement delete = table.findElement(By.xpath("//span[@onclick=\"dialog.deleteDialog('" + userId
                + "','person',{ refresh : 'null'}, null)\"]"));
        delete.click();
        WebElement elementConfirm = wait
                .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Delete']")));
        WebElement confirm = table.findElement(By.xpath("//span[text()='Delete']"));
        confirm.click();
        WebElement elementButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
                "//span[@onclick=\"dialog.formDialog(null,'person', { refresh : 'detailTable_person'}, {})\"]")));
    } else {
        org.testng.Assert.fail("Test user was not succesfully created.");
    }
}

From source file:be.rubus.web.jerry.initializer.RequiredInitializerTest.java

License:Apache License

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

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

    Thread.sleep(1000);

    WebElement messages = driver.findElement(By.id("messages"));
    List<WebElement> errorElements = messages.findElements(By.tagName("li"));
    assertThat(errorElements).hasSize(1);

    assertThat(errorElements.get(0).getText()).contains("Validation Error: Value is required.");

}

From source file:be.rubus.web.jerry.recording.RecordValueTest.java

License:Apache License

@Test
@RunAsClient/*  w  w w.j a  va 2  s. c o  m*/
public void testClassLevelValidation() throws Exception {
    driver.get(new URL(contextPath, "dateRange.xhtml").toString());

    WebElement field = driver.findElement(By.id("test:beginDate"));
    field.sendKeys("01/03/2015");

    field = driver.findElement(By.id("test:endDate"));
    field.sendKeys("01/01/2015");

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

    Thread.sleep(1000);

    WebElement messages = driver.findElement(By.id("errors"));
    List<WebElement> errorElements = messages.findElements(By.tagName("li"));

    // FIXME The test fails and it seems that RecordingInfoPhaseListener doesn't kick in
    assertThat(errorElements).hasSize(1);

    assertThat(errorElements.get(0).getText()).contains("Validation Error: Value is required.");

}

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

License:Apache License

@Test
@RunAsClient//w w  w .j  av  a 2s  .  c  om
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   ww  w  .  j  a v  a 2  s  . co m*/
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");
}

From source file:be.rubus.web.testing.CommonElementCode.java

License:Apache License

protected List<WebElement> getAllChildren(WebElement element) {
    return element.findElements(By.xpath("*"));
}

From source file:be.rubus.web.testing.GrafacesContext.java

License:Apache License

public void initializePageFragment(Object childObject, WebElement childRoot, Object parentObject) {
    if (!hasPropertyFor(Root.class, childObject)) {
        fail("No property annotated with @Root in class " + childObject.getClass());
    }/*from   w  ww. j  a va 2 s .  com*/
    setInstanceOf(Root.class, childObject, childRoot);

    if (hasPropertyFor(Drone.class, childObject)) {
        WebDriver driver = getInstanceOf(Drone.class, parentObject, WebDriver.class);
        // FIXME when driver == null
        /*
        if (grapheneContext == null) {
        grapheneContext = GrapheneContext.getContextFor(ReflectionHelper.getQualifier(field.getAnnotations()));
        }
        grapheneContext.getWebDriver(xx.class) where xx is the class type of the field where annotation was placed on
        */
        setInstanceOf(Drone.class, childObject, driver);
    }

    try {
        List<Field> fields = ReflectionHelper.getFieldsWithAnnotation(childObject.getClass(), FindBy.class);
        for (Field field : fields) {
            By by = FindByUtilities.getCorrectBy(field, How.ID_OR_NAME);
            // WebElement
            if (field.getType().isAssignableFrom(WebElement.class)) {

                WebElement element = childRoot.findElement(by);
                ReflectionUtil.setValue(field, childObject, element);
                // List<WebElement>
            } else if (field.getType().isAssignableFrom(List.class)
                    && getListType(field).isAssignableFrom(WebElement.class)) {
                List<WebElement> elements = childRoot.findElements(by);
                ReflectionUtil.setValue(field, childObject, elements);
            }

        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    if (hasPropertyFor(Grafaces.class, childObject)) {
        setInstanceOf(Grafaces.class, childObject, this);
    }

    executeMethodsOfType(PostConstruct.class, childObject);
}