Example usage for org.openqa.selenium WebDriver findElements

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:org.metawidget.util.AngularScenarioRunnerTestCase.java

License:LGPL

@Override
protected Boolean applyExpectedCondition(WebDriver driver) {

    List<WebElement> testElements = driver.findElements(By.className("test-it"));

    // Test elements may not appear until they start running

    if (testElements.size() < getExpectedNumberOfTests()) {
        return false;
    }/*w  ww  .j a v a2  s  .  c o m*/

    // Once all test elements on the page, check them all

    for (WebElement test : testElements) {
        String classAttribute = test.getAttribute("class");

        if (classAttribute.contains("status-pending")) {
            return false;
        }
    }

    return true;
}

From source file:org.metawidget.util.AngularScenarioRunnerTestCase.java

License:LGPL

@Override
protected void displayResult(WebDriver driver) {

    // Display the result

    String failed = null;/*from   w w w  .  j a  v a 2 s . co m*/

    for (WebElement test : driver.findElements(By.className("test-it"))) {

        StringBuilder builder = new StringBuilder("\t");
        builder.append(test.getText());
        String classAttribute = test.getAttribute("class");

        if (classAttribute.contains("status-failure") || classAttribute.contains("status-error")) {
            failed = test.getText();
            builder.append(" - failed");
        } else {
            builder.append(" - passed");
        }

        System.out.println(builder);
    }

    System.out.println("Tests run: " + driver.findElement(By.className("status-success")).getText() + ", "
            + driver.findElement(By.className("status-failure")).getText() + ", "
            + driver.findElement(By.className("status-error")).getText());

    // Fail if necessary

    if (failed != null) {
        throw new RuntimeException(failed);
    }
}

From source file:org.miage.robotsurfeur.extraction.PageExtract.java

License:Open Source License

/**
 * Extract links from a given URL.//from   w  w  w .  j ava2s.co  m
 *
 * @param wd <tt>WebDriver</tt>
 * @return <tt>LinkedList&lt;Link&gt;</tt> a collection of links
 */
public static LinkedList<Link> getLinks(WebDriver wd) {

    //        System.out.println("Extract....");
    List<WebElement> listLink = wd.findElements(By.xpath("//a[@href]"));
    LinkedList<Link> allLinks = new LinkedList<>();

    for (WebElement anchor : listLink) {
        String link = anchor.getAttribute("href");
        String text = anchor.getText();
        allLinks.add(new Link(link, text));
    }

    return allLinks;
}

From source file:org.mitre.mpf.wfm.ui.CreateJobPage.java

License:Open Source License

/**
 * Run a job on the server media/*from   w ww  . ja va 2s  . com*/
 * @param driver
 * @param base_url
 * @return
 * @throws InterruptedException 
 */
public JobStatusPage createJobFromUploadedMedia(WebDriver driver, String base_url, String filename,
        String pipeline_name, int priority, int num_rows) throws InterruptedException {
    log.info("[CreateJobPage] createJobFromUploadedMedia");
    if (!CreateJobPage.ValidPage(driver)) {
        throw new IllegalStateException("This is not the correct page, current page" + "is: "
                + driver.getCurrentUrl() + " should be " + this.PAGE_URL);
    }

    //switch to uploads
    //Utils.safeClickById(driver, "btn-display-upload-root"); //media will not be checked
    //Thread.sleep(2000);//wait for it to populate
    //log.info("View Uploads Only tab click");

    //click on the remote-media directory and wait for it
    log.info("clicking remote-media");
    List<WebElement> rows = driver.findElements(By.className("list-group-item"));
    int idx = rows.size();
    //log.info("#level directories:" + idx);
    boolean found = false;
    for (WebElement row : rows) {
        String ele = row.getText();
        //log.info(ele);
        if (ele.equals("remote-media")) {
            row.click();
            found = true;
        }
    }
    if (!found) {
        // Using the TestNG API for logging
        throw new IllegalStateException("remote-media not found " + driver.getCurrentUrl());
    }
    // wait for files to load
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return d.findElement(By.id("file_list_server")) != null;
        }//may need to check for li
    });

    /*
    log.info("finding the first uploaded file");
    rows = driver.findElements(By.xpath("//table[@id='file_list_server']/tbody/tr"));
    idx = rows.size();
    if(rows.size() <= 1 && idx < num_rows){
       log.error("[createJobFromUploadedMedia] No Files in the media");
       throw new IllegalStateException("[createJobFromUploadedMedia] No Files in the media " + driver.getCurrentUrl());
    }
    log.info("#Files Available:" + idx);
    */
    log.info("finding the file: " + filename);
    WebElement search = driver.findElement(By.xpath("//div[@id='file_list_server_filter']/label/input"));
    search.sendKeys(filename);//search
    Thread.sleep(2000);

    //select the first file
    log.info("clicking the uploaded file:" + filename);
    driver.findElement(By.xpath("//table[@id='file_list_server']/tbody/tr[1]/td[1]/input")).click();

    //select the desired pipeline from the select list
    found = safeSelectUiSelectByText(driver, "jobPipelineSelectServer", pipeline_name);
    log.info("Pipeline found:" + found);

    Thread.sleep(1000);

    //select the desired priority
    log.info("clicking the priority");
    found = safeSelectUiSelectByIndex(driver, "jobPrioritySelectServer", priority);
    log.info("Priority found:" + found);

    //quick sleep to make sure the angular controller process all events
    Thread.sleep(1000);

    log.info("submit job ");
    Utils.safeClickById(driver, "btn-submit-checked"); //submit media

    log.info("Wait 10 sec for job status");
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return JobStatusPage.ValidPage(d);
        }
    });
    log.info("on job status page");
    return new JobStatusPage(driver);
}

From source file:org.mitre.mpf.wfm.ui.JobStatusPage.java

License:Open Source License

public static String[] getjobTableRow(WebDriver driver, int idx) {
    log.debug("[JobStatusPage] getjobTable");
    if (!JobStatusPage.ValidPage(driver)) {
        throw new IllegalStateException("This is not the correct page, current page" + "is: "
                + driver.getCurrentUrl() + " should be " + JobStatusPage.PAGE_URL);
    }//from  w  w w.j a v  a  2s.  co  m
    List<WebElement> rows = driver.findElements(By.xpath("//*[@id='jobTable']/tbody/tr"));
    if (rows.size() == 0) {
        log.error("[JobStatusPage] No jobs in the jobsTable");
        return null;
    }
    WebElement job = rows.get(idx);//first element should be most recent job
    log.debug("[JobStatusPage] getjobTable job row [{}] : " + job.getText(), idx);
    //Id    PipelineName    StartDate    EndDate    Status    Progress    DetailedProgress
    List<WebElement> columns = job.findElements(By.tagName("td"));
    String[] ret = { columns.get(0).getText(), columns.get(1).getText(), columns.get(2).getText(),
            columns.get(3).getText(), columns.get(4).getText(), columns.get(5).getText(),
            columns.get(6).getText() };
    return ret;
}

From source file:org.mitre.mpf.wfm.ui.NodeConfigurationPage.java

License:Open Source License

public int getCurrentServicesCount(WebDriver driver) {
    if (!HomePage.ValidPage(driver)) {
        throw new IllegalStateException(
                "This is not Home Page of logged in user, current page is: " + driver.getCurrentUrl());
    }//  w  w  w.  j  a va  2s. c om

    //get list of services
    String classname = "service-item-cart";//span
    log.info("Finding list of elements with class=" + classname);
    List<WebElement> carts = driver.findElements(By.className(classname));//grab the carts
    log.info("#carts:" + carts.size());
    if (carts.size() == 0)
        return -1;
    WebElement cart = carts.get(0);
    List<WebElement> serviceItems = cart.findElements(By.className("dnd-item-count-input"));
    log.info("#service-items:" + serviceItems.size());
    int count = 0;
    for (WebElement row : serviceItems) {
        String ele = row.getAttribute("value");
        //log.info(ele);
        count += Integer.parseInt(ele);
    }

    return count;
}

From source file:org.mitre.mpf.wfm.ui.NodesAndProcessPage.java

License:Open Source License

public static boolean ValidNodeManagerPage(WebDriver driver) {
    log.info("Current Title:" + driver.getTitle());
    List<WebElement> h2s = driver.findElements(By.xpath("//h2"));
    boolean valid = false;
    for (WebElement ele : h2s) {
        log.info("h2:" + ele.getText());
        if (ele.getText().startsWith("Cluster participants as of")) {
            valid = true;/*from   w  w w . ja va 2  s  .c  om*/
            break;
        }
    }
    return valid;
}

From source file:org.mitre.mpf.wfm.ui.NodesAndProcessPage.java

License:Open Source License

public List<String> getCurrentNodesAndProcess(WebDriver driver) {
    if (!HomePage.ValidPage(driver)) {
        throw new IllegalStateException(
                "This is not Home Page of logged in user, current page is: " + driver.getCurrentUrl());
    }//  w ww . jav a 2  s.c  o m

    // get list of shutdown btns
    List<WebElement> rows = driver.findElements(By.xpath("//*[@id='dataTable-processes']/tbody/tr"));
    ArrayList<String> list = new ArrayList<String>();

    for (WebElement row : rows) {
        List<WebElement> columns = row.findElements(By.tagName("td"));
        list.add(columns.get(0).getText() + ":" + columns.get(2).getText());// name:state
    }

    return list;
}

From source file:org.mitre.mpf.wfm.ui.NodesAndProcessPage.java

License:Open Source License

public boolean stopNode(WebDriver driver, final String node_name) {
    //get all the stop buttons
    List<WebElement> btns = Utils.getClassValues(driver, "anp_shutdownbtn");
    //find our button
    for (WebElement ele : btns) {
        if (ele.getAttribute("value").equals(node_name)) {
            log.info("Shutting down node: " + node_name);
            ele.click();//  w w w. ja va  2 s  .com
            // Wait for the status to change, timeout after 10 seconds
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    for (int attempts = 0; attempts < 10; attempts++) { // work around auto-refresh issues
                        try {
                            List<WebElement> rows = d
                                    .findElements(By.xpath("//*[@id='dataTable-processes']/tbody/tr"));
                            for (WebElement row : rows) {
                                List<WebElement> columns = row.findElements(By.tagName("td"));
                                if (columns.get(0).getText().endsWith(node_name)) {
                                    return columns.get(2).getText().equals("Stopped");
                                }
                            }
                        } catch (StaleElementReferenceException e) {
                            // nothing
                        }
                    }
                    return false;
                }
            });
            return true;
        }
    }
    return false;
}

From source file:org.mitre.mpf.wfm.ui.NodesAndProcessPage.java

License:Open Source License

public boolean startNode(WebDriver driver, final String node_name) {
    //get all the stop buttons
    List<WebElement> btns = Utils.getClassValues(driver, "anp_startbtn");
    //find our button
    for (WebElement ele : btns) {
        if (ele.getAttribute("value").equals(node_name)) {
            log.info("Starting up node: " + node_name);
            ele.click();/*  w w  w  .j  ava 2  s .  c  o  m*/
            // Wait for the status to change, timeout after 10 seconds
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    for (int attempts = 0; attempts < 10; attempts++) { // work around auto-refresh issues
                        try {
                            List<WebElement> rows = d
                                    .findElements(By.xpath("//*[@id='dataTable-processes']/tbody/tr"));
                            for (WebElement row : rows) {
                                List<WebElement> columns = row.findElements(By.tagName("td"));
                                if (columns.get(0).getText().endsWith(node_name)) {
                                    return columns.get(2).getText().equals("Running");
                                }
                            }
                        } catch (StaleElementReferenceException e) {
                            // nothing
                        }
                    }
                    return false;
                }
            });
            return true;
        }
    }
    return false;
}