Example usage for org.openqa.selenium WebDriver findElement

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

Introduction

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

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

From source file:com.cloudbees.workflow.ui.AbstractPhantomJSTest.java

License:Open Source License

protected void moveMouseOffElement(WebDriver webdriver) {
    Actions actions = new Actions(webdriver);
    actions.moveToElement(webdriver.findElement(By.cssSelector("html"))).build().perform();
}

From source file:com.cloudbees.workflow.ui.AbstractPhantomJSTest.java

License:Open Source License

protected List<WebElement> waitForElementsAdded(WebDriver webdriver, String cssSelector) {
    return waitForElementsAdded(webdriver.findElement(By.cssSelector("html")), cssSelector);
}

From source file:com.cloudbees.workflow.ui.AbstractPhantomJSTest.java

License:Open Source License

protected void waitForElementsRemoved(WebDriver webdriver, String cssSelector) {
    waitForElementsRemoved(webdriver.findElement(By.cssSelector("html")), cssSelector);
}

From source file:com.cloudbees.workflow.ui.view.BuildArtifactsTest.java

License:Open Source License

@Ignore("remove phantomjs: https://trello.com/c/JpUg8S5z/159-get-rid-of-phantomjs-webdriver")
@Test//w  w w. j  av  a  2  s . c  o m
public void test() throws Exception {
    WebDriver webdriver = getWebDriver();

    WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "Noddy Job");

    job.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Archiving'); "
            + "   sh('mkdir targs && echo hello > targs/hello1.txt && echo hello > targs/hello2.txt'); "
            + "   archive(includes: 'targs/*.txt'); " + "}"));

    QueueTaskFuture<WorkflowRun> build = job.scheduleBuild2(0);
    jenkinsRule.assertBuildStatusSuccess(build);

    String jobUrl = getItemUrl(jenkinsRule.jenkins, job);
    webdriver.get(jobUrl);

    // Look for the build artifacts popup button...
    WebElement buildArtifactsPopupBtn = webdriver.findElement(By.cssSelector(".build-artifacts-popup"));
    Assert.assertNotNull(buildArtifactsPopupBtn);

    // Move over the button to load the popover...
    moveMouseToElement(webdriver, buildArtifactsPopupBtn);

    // Look for and test the popover content...
    List<WebElement> buildArtifactsPopover = waitForElementsAdded(webdriver, ".cbwf-build-artifacts");
    Assert.assertEquals(1, buildArtifactsPopover.size());

    WebElement popover = buildArtifactsPopover.get(0);
    List<WebElement> artifacts = waitForElementsAdded(popover, ".artifact");
    Assert.assertEquals(2, artifacts.size());

    // TODO: Something strange here with selenium.
    // Can't seem to get the text of that element even though it is there if you sout the whole page.
    // isDisplayed on those anchor elements returns false for some reason, even though the parent elements
    // are "displayed".
    // Assert.assertEquals("hello1.txt", artifacts.get(0).findElement(By.cssSelector(".name")).getText());
    // Assert.assertEquals("hello2.txt", artifacts.get(1).findElement(By.cssSelector(".name")).getText());

    // Make sure it goes away once we move off the popover...
    moveMouseOffElement(webdriver);
    waitForElementsRemoved(webdriver, ".cbwf-build-artifacts");
}

From source file:com.cloudbees.workflow.ui.view.WorkflowStageViewActionTest.java

License:Open Source License

@Ignore("remove phantomjs: https://trello.com/c/JpUg8S5z/159-get-rid-of-phantomjs-webdriver")
@Test//  w  w w.j a  v  a2 s  . co  m
public void test() throws Exception {
    WebDriver webdriver = getWebDriver();

    WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "Noddy Job");

    job.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Build'); " + "   sh ('echo Building'); "
            + "   stage ('Test'); " + "   sh ('echo Testing'); " + "   stage ('Deploy'); "
            + "   sh ('echo Deploying'); " + "}"));

    QueueTaskFuture<WorkflowRun> build = job.scheduleBuild2(0);
    jenkinsRule.assertBuildStatusSuccess(build);

    String jobUrl = getItemUrl(jenkinsRule.jenkins, job);
    webdriver.get(jobUrl);

    //        System.out.println(webdriver.getPageSource());

    // Make sure the jobsTable is rendered in the page
    WebElement jobsTable = webdriver.findElement(By.className("jobsTable"));
    Assert.assertNotNull(jobsTable);

    // Check the totals are rendered
    //        List<WebElement> stageWrappers = jobsTable.findElements(By.cssSelector(".totals .stage-wrapper"));
    //        Assert.assertEquals(3, stageWrappers.size());

    // Should have just one job
    List<WebElement> jobs = jobsTable.findElements(By.cssSelector(".job"));
    Assert.assertEquals(1, jobs.size());

    // That job should have 3 stages
    List<WebElement> jobStages = jobs.get(0).findElements(By.cssSelector(".stage-wrapper"));
    Assert.assertEquals(3, jobStages.size());
}

From source file:com.coderoad.automation.common.util.old.BasePage.java

License:Open Source License

/**
 * Wait for element.//from w ww .ja v  a 2s . c  o  m
 * 
 * @param by the by
 * @return the web element
 */
protected WebElement waitForElement(final By by) {

    WebElement element = null;
    LogUtil.log("WaitForElement : + " + this.getTimeStamp(), LogLevel.HIGH);
    for (int i = 0; i < 3; i++) {
        try {
            element = new WebDriverWait(driver, TestUtils.timeout).until(new ExpectedCondition<WebElement>() {

                public WebElement apply(WebDriver d) {

                    return d.findElement(by);
                }
            });
        } catch (Exception e) {
            TestLogger.logMsg("Wait waitForElement : + " + this.getTimeStamp());
        }
    }
    LogUtil.log("Done waitForElement : + " + this.getTimeStamp(), LogLevel.HIGH);
    return element;
}

From source file:com.coderoad.automation.common.util.PageUtil.java

License:Open Source License

/**
 * Checks if is visible./*  w w w .  j ava2 s . c  om*/
 * 
 * @param driver the driver
 * @param locator the locator
 * @param timeout the timeout
 * @return true, if is visible
 */
public static boolean isVisible(final WebDriver driver, final By locator, final Timeout timeout) {
    LogUtil.log("Check the Element is Visible(By Locator) ", LogLevel.HIGH);

    try {
        return new WebDriverWait(driver, timeout.getValue())
                .until(ExpectedConditions.visibilityOf(driver.findElement(locator))).isDisplayed();
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.coderoad.automation.common.util.PageUtil.java

License:Open Source License

/**
 * Method to set date in a date picker/*from   w  ww.  ja  v a 2 s  . com*/
 * @author Ruth Chirinos
 * @param driver the WebDriver
 * @param xpathDatePicker the xpath of the datePicker
 * @param vehicleSelectionPage  
 * year  - the year minus 1900.
  * month - the month between 0-11.
  * date  - the day of the month between 1-31.
 * @throws InterruptedException 
 * @throws ParseException 
 */
public static boolean setDateInDatePicker(WebDriver driver, String xpathDatePicker, Date dateRequested)
        throws InterruptedException, ParseException {
    Calendar dateRequestedCalendar = DateUtil.getCalendarForDate(dateRequested);
    LogUtil.log("Date for changing is > "
            + DateUtil.format(dateRequestedCalendar, DateUtil.DATE_MONTH_ABBREVIATION), LogLevel.LOW);
    boolean processOk = false;
    String dateSelectedString = "";
    Date dateSelected = null;

    SimpleDateFormat simpleFormat = new SimpleDateFormat(DateUtil.DATE_MONTH_YEAR_FMT);
    WebElement datePicker = driver.findElement(By.xpath(xpathDatePicker));
    datePicker.click();
    WebElement monthYearElement = driver
            .findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]"));
    WebElement daySelected = driver.findElement(By.xpath("//*[contains(@class, 'x-datepicker-selected')]/a"));
    dateSelectedString = daySelected.getAttribute("innerHTML") + " "
            + monthYearElement.getAttribute("innerHTML");
    dateSelected = simpleFormat.parse(dateSelectedString);

    WebElement pickerPrevEl = driver.findElement(
            By.xpath("//div[@class='x-datepicker-header']/a[contains(@class, 'x-datepicker-prev')]"));
    WebElement pickerNextEl = driver.findElement(
            By.xpath("//div[@class='x-datepicker-header']/a[contains(@class, 'x-datepicker-next')]"));

    //Choosing the year
    while (dateRequested.getYear() < dateSelected.getYear()) {
        pickerPrevEl.click();
        monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]"));
        dateSelectedString = daySelected.getAttribute("innerHTML") + " "
                + monthYearElement.getAttribute("innerHTML");
        dateSelected = simpleFormat.parse(dateSelectedString);
    }

    while (dateRequested.getYear() > dateSelected.getYear()) {
        pickerNextEl.click();
        monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]"));
        dateSelectedString = daySelected.getAttribute("innerHTML") + " "
                + monthYearElement.getAttribute("innerHTML");
        dateSelected = simpleFormat.parse(dateSelectedString);
    }

    //Choosing the months
    while (dateRequested.getMonth() < dateSelected.getMonth()) {
        pickerPrevEl.click();
        monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]"));
        dateSelectedString = daySelected.getAttribute("innerHTML") + " "
                + monthYearElement.getAttribute("innerHTML");
        dateSelected = simpleFormat.parse(dateSelectedString);
    }

    while (dateRequested.getMonth() > dateSelected.getMonth()) {
        pickerNextEl.click();
        monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]"));
        dateSelectedString = daySelected.getAttribute("innerHTML") + " "
                + monthYearElement.getAttribute("innerHTML");
        dateSelected = simpleFormat.parse(dateSelectedString);
    }

    //Choosing the day      
    WebElement dateWidget = driver.findElement(By.className("x-datepicker-inner"));
    List<WebElement> columns = dateWidget.findElements(By.tagName("td"));
    int aux = 0;

    for (WebElement cell : columns) {
        if (cell.getText().equals(dateRequested.getDate() + "")) {
            processOk = cell.getAttribute("class").contains("x-datepicker-active");
            if (processOk) {
                cell.findElement(By.tagName("a")).click();
                ;
                LogUtil.log("The date was selected.", LogLevel.LOW);
                aux = 1;
                break;
            }
        }
    }

    if (aux == 0) {
        LogUtil.log("The date selected is not enabled.", LogLevel.LOW);
    }
    return processOk;
}

From source file:com.coderoad.automation.rocketTruedx.RocketTruedxNaBasePage.java

License:Open Source License

protected WebElement waitForElement(final By by) {

    WebElement element = null;//w  w w .j a  va 2s  .co m
    TestLogger.logMsg("Start waitForElement : + " + this.getTimeStamp());
    for (int i = 0; i < 1; i++) {
        try {
            element = new WebDriverWait(driver, TestUtils.timeout).until(new ExpectedCondition<WebElement>() {

                @Override
                public WebElement apply(WebDriver d) {

                    return d.findElement(by);
                }
            });
        } catch (Exception e) {
            TestLogger.logMsg("Wait waitForElement : + " + this.getTimeStamp());
        }
    }
    TestLogger.logMsg("Done waitForElement : + " + this.getTimeStamp());
    return element;
}

From source file:com.coderoad.automation.rocketTruedx.RocketTruedxNaTestBase.java

License:Open Source License

/**
 * Wait for element.//from w  ww.j  av a 2  s.  c  o m
 * 
 * @param by the by
 * @return the web element
 */
protected WebElement waitForElement(final By by) {

    WebElement element = null;
    TestLogger.logMsg("Start waitForElement : + " + TestUtils.getTimeStamp());
    for (int i = 0; i < 3; i++) {
        element = new WebDriverWait(driver, TestUtils.timeout).until(new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver d) {

                return d.findElement(by);
            }
        });
        TestLogger.logMsg("Wait waitForElement : + " + TestUtils.getTimeStamp());
    }
    TestLogger.logMsg("Done waitForElement : + " + TestUtils.getTimeStamp());
    return element;
}