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:br.edu.ifpb.praticas.testSystem.FilmeTest.java

@Test
public void testCadastro() throws Exception {
    WebDriver driver = new FirefoxDriver();
    WebElement element = driver.findElement(By.name("nome"));
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.

    assertEquals("http://localhost:8085/SisFilme/index.xhtml", driver.getCurrentUrl());

    Thread.sleep(2000L);/*from   w ww. j av  a2  s .co  m*/
    element = driver.findElement(By.name("nome"));
    element.sendKeys("007 contra moscol");
    element = driver.findElement(By.name("nome"));
    element.sendKeys("007 contra moscol");

    element = driver.findElement(By.name("ano"));
    element.sendKeys("2014");
    element = driver.findElement(By.name("genero"));
    element = driver.findElement(By.name("nota"));
    element.sendKeys("2");

    element = driver.findElement(By.name("salvar"));
    Thread.sleep(2000L);
    element.click();
    assertEquals("http://localhost:8085/SisFilme/gerenciamento.xhtml", driver.getCurrentUrl());

    assertNotNull(element);
    // Wait for the page to load, timeout after 10 seconds
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver d) {
            return d.getTitle().contains("NetBeans");
        }
    });

    //Close the browser
    driver.quit();
}

From source file:br.eti.kinoshita.selenium.util.Utils.java

License:Open Source License

public static Function<WebDriver, WebElement> presenceOfElement(final By locator) {
    return new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }/*from   ww w .  j  ava  2 s.  com*/
    };
}

From source file:br.eti.kinoshita.selenium.util.Utils.java

License:Open Source License

public static Function<WebDriver, Select> presenceOfSelectIndexAvailable(final By locator, final int index) {
    return new Function<WebDriver, Select>() {
        public Select apply(WebDriver driver) {
            WebElement foundElement = driver.findElement(locator);
            if (foundElement != null) {
                Select select = new Select(foundElement);
                if (select.getOptions().size() >= index) {
                    return select;
                }//  w  ww .j  a v  a2s.  co m
            }
            // TBD: or throw NoSuchException...
            return null;
        }
    };
}

From source file:br.eti.kinoshita.selenium.util.Utils.java

License:Open Source License

/**
 * Wait for assync content in a determined period
 * //from   www.j  a v a 2 s .c o  m
 * @param driver Selenium web driver.
 * @param by Selenium By expression.
 * @param timeout Selenium time out.
 * @return a WebElement asynchronously loaded.
 * @throws NoSuchElementException
 */
public static WebElement waitForAssyncContent(WebDriver driver, By by, Long timeout)
        throws NoSuchElementException {
    long end = System.currentTimeMillis() + (timeout);
    WebElement renderedWebElement = null;

    while (System.currentTimeMillis() < end) {
        try {
            renderedWebElement = driver.findElement(by);
        } catch (NoSuchElementException nsee) {
            LOGGER.debug(nsee.getMessage(), nsee);
        }

        if (renderedWebElement != null && renderedWebElement.isEnabled() && renderedWebElement.isDisplayed()) {
            return renderedWebElement;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ie) {
            LOGGER.debug(ie.getMessage(), ie);
        }
    }

    if (renderedWebElement == null) {
        throw new NoSuchElementException("Could not locate assync content");
    }

    try {
        if (renderedWebElement.isDisplayed()) {
            throw new NoSuchElementException("Element is not being displayed");
        }
    } catch (Throwable t) {
        LOGGER.debug(t.getMessage(), t);
    }

    return renderedWebElement;
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.primefaces.PrimeFacesEditor.java

License:Open Source License

public void sendKeys(CharSequence... keysToSend) {
    String[] id = getElementMap().locator();

    String str;/*from w w  w . jav  a  2s . c om*/

    str = id[0].toString();

    String value = charSequenceToString(keysToSend);

    WebDriver wd = super.getDriver();

    WebElement frame = wd.findElement(By.xpath(str));
    wd.switchTo().frame(frame);

    JavascriptExecutor js = null;
    if (wd instanceof JavascriptExecutor) {
        js = (JavascriptExecutor) wd;
    }
    WebElement editorBody = wd.findElement(By.cssSelector("body"));
    js.executeScript("arguments[0].innerHTML = ''", editorBody);

    wd.findElement(By.tagName("body")).sendKeys(value);

    wd.switchTo().defaultContent();
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.primefaces.PrimeFacesEditor.java

License:Open Source License

/**
 * Funo que tenta preencher mais de uma vez o campo. Ela verifica se o
 * contedo enviado  o mesmo que esta atualmente no campo.
 */// w  w w. j ava2  s . c  o m
public void sendKeysWithTries(CharSequence... keysToSend) {

    String[] id = getElementMap().locator();

    String str;

    str = id[0].toString();

    String value = charSequenceToString(keysToSend);

    WebDriver wd = super.getDriver();

    WebElement frame = wd.findElement(By.xpath(str));
    wd.switchTo().frame(frame);

    JavascriptExecutor js = null;
    if (wd instanceof JavascriptExecutor) {
        js = (JavascriptExecutor) wd;
    }
    WebElement editorBody = wd.findElement(By.cssSelector("body"));
    js.executeScript("arguments[0].innerHTML = ''", editorBody);

    wd.findElement(By.tagName("body")).sendKeys(value);

    wd.switchTo().defaultContent();

}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.primefaces.PrimeFacesEditor.java

License:Open Source License

public void clear() {
    String[] id = getElementMap().locator();

    String str;/*from   w  ww .  jav a  2 s.c om*/

    str = id[0].toString();

    WebDriver wd = super.getDriver();

    WebElement frame = wd.findElement(By.xpath(str));
    wd.switchTo().frame(frame);
    JavascriptExecutor js = null;
    if (wd instanceof JavascriptExecutor) {
        js = (JavascriptExecutor) wd;
    }
    WebElement editorBody = wd.findElement(By.cssSelector("body"));
    js.executeScript("arguments[0].innerHTML = ''", editorBody);

    wd.switchTo().defaultContent();
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.primefaces.PrimeFacesEditor.java

License:Open Source License

@Override
public String getText() {
    String[] id = getElementMap().locator();

    String str;//www . j a  v  a 2 s .c  o  m

    str = id[0].toString();

    WebDriver wd = super.getDriver();

    WebElement frame = wd.findElement(By.xpath(str));
    wd.switchTo().frame(frame);
    String text = wd.findElement(By.tagName("body")).getText();

    wd.switchTo().defaultContent();

    return text;
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebBaseTest.java

License:Open Source License

@Test
@Ignore//from w w  w . j a  v  a 2  s  .  com
public void testGetText() {
    WebElement element = Mockito.mock(WebElement.class);
    WebDriver driver = Mockito.mock(WebDriver.class);
    Mockito.when(driver.findElement((By) Mockito.anyObject())).thenReturn(element);
    Runner runner = Mockito.mock(Runner.class);
    Mockito.when(runner.getDriver()).thenReturn(driver);
    WebBase webBase = new WebBase();
    ElementMap mapa = Mockito.mock(ElementMap.class);
    Mockito.when(mapa.locator()).thenReturn(new String[] { "ID" });
    Mockito.when(mapa.locatorType()).thenReturn(ElementLocatorType.XPath);

    webBase.setRunner(runner);
    webBase.setElementMap(mapa);
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebBaseTest.java

License:Open Source License

@Test
@Ignore/*from w w w.j a v a2  s  . c  o m*/
public void testVerifyState() {
    WebElement element = Mockito.mock(WebElement.class);
    Mockito.when(element.isDisplayed()).thenReturn(true);
    WebDriver driver = Mockito.mock(WebDriver.class);
    Mockito.when(driver.findElement((By) Mockito.anyObject())).thenReturn(element);
    Runner runner = Mockito.mock(Runner.class);
    Mockito.when(runner.getDriver()).thenReturn(driver);
    WebBase webBase = new WebBase();
    ElementMap mapa = Mockito.mock(ElementMap.class);
    Mockito.when(mapa.locator()).thenReturn(new String[] { "ID" });
    Mockito.when(mapa.locatorType()).thenReturn(ElementLocatorType.XPath);

    webBase.setRunner(runner);
    webBase.setElementMap(mapa);
}