Example usage for org.openqa.selenium By xpath

List of usage examples for org.openqa.selenium By xpath

Introduction

In this page you can find the example usage for org.openqa.selenium By xpath.

Prototype

public static By xpath(String xpathExpression) 

Source Link

Usage

From source file:br.eti.kinoshita.selenium.samples.GoogleSearchResultsPage.java

License:Open Source License

public boolean lookForTextWithinResults(String text) {
    boolean found = Boolean.FALSE;

    searchResults = getDriver().findElements(By.xpath("//h3[@class='r']//a")); // <h3 class='r'><a...

    for (WebElement searchResult : searchResults) {
        if (searchResult.getText().toLowerCase().contains(text.toLowerCase())) {
            found = Boolean.TRUE;
            break;
        }//from   ww w  .j  a  v  a  2 s .c om
    }

    return found;
}

From source file:br.eti.kinoshita.selenium.TestGoogle.java

License:Open Source License

@Test(groups = { "GoogleTest" }, /* dependsOnGroups={"LoginGroup"}, */dataProvider = "DataExcel")
public void testGoogle(String search, String result, ITestContext ctx, Method method) {
    this.addScreenShot(ctx, method, "Main page");

    WebElement inputQueryField = driver.findElement(By.name("q"));

    inputQueryField.sendKeys(search);/*from  www. j  a  va 2s  . co m*/

    WebElement searchButton = driver.findElement(By.name("btnK"));

    searchButton.submit();

    // Wait for results to load
    Utils.waitForAssyncContent(driver, By.xpath("//h3[@class='r']//a"), getConfiguration()
            .getLong("selenium.timeout", 15000)); /* second parameter in getLong is a default value */

    List<WebElement> searchResults = driver.findElements(By.xpath("//h3[@class='r']//a")); // <h3 class='r'><a...

    this.addScreenShot(ctx, method, "Search results for " + search);

    Assert.assertNotNull(searchResults, "Couldn't find anything for query string " + search);

    boolean found = Boolean.FALSE;

    for (WebElement searchResult : searchResults) {
        if (searchResult.getText().toLowerCase().contains(result.toLowerCase())) {
            found = Boolean.TRUE;
            break;
        }
    }

    Assert.assertTrue(found,
            "Couldn't locate " + result + " in current page :" + getConfiguration().getString("selenium.url"));

}

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  .java 2s  .com*/

    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.
 *///from ww w .j a va  2  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;/*  w  w  w .  j a  va  2  s.  c o  m*/

    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;//from  ww  w .j a va2s. c  om

    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.primefaces.PrimefacesGrid.java

License:Open Source License

@Override
public String findTextInTable(Element element, String l, String c) {
    String xpathTabela = preparaXPath(element, l, c);
    WebElement myElement = (WebElement) ((WebDriver) runner.getDriver()).findElement(By.xpath(xpathTabela));
    String str = "";
    try {//from ww  w  .  j  ava  2s .  c  om
        str = myElement.getText();
    } catch (Exception ex) {
    }
    if (str == null || str.isEmpty()) {
        xpathTabela = xpathTabela.concat("//input[@type='text']");
        myElement = ((WebDriver) runner.getDriver()).findElement(By.xpath(xpathTabela));
        str = myElement.getAttribute("value");
    }
    return str;
}

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

License:Open Source License

@Override
public void tableButtonClick(Element element, String l, String c) {
    String xpathTabela = preparaXPath(element, l, c);
    String xpathTabelaBase = xpathTabela;
    xpathTabela = xpathTabela.concat("//button[@type='submit' or @type='button']");
    xpathTabela = xpathTabela.concat("|");
    xpathTabela = xpathTabela.concat(xpathTabelaBase);
    xpathTabela = xpathTabela.concat("//input[@type='checkbox']");
    xpathTabela = xpathTabela.concat("|");
    xpathTabela = xpathTabela.concat(xpathTabelaBase);
    xpathTabela = xpathTabela.concat("//div[@class='ui-row-toggler']");
    xpathTabela = xpathTabela.concat("|");
    xpathTabela = xpathTabela.concat(xpathTabelaBase);
    xpathTabela = xpathTabela.concat("//a");
    xpathTabela = xpathTabela.concat("|(");
    xpathTabela = xpathTabela.concat(xpathTabelaBase);
    xpathTabela = xpathTabela.concat(")");
    xpathTabela = xpathTabela.concat("/div/div[2]");

    WebElement myElement = (WebElement) ((WebDriver) runner.getDriver()).findElement(By.xpath(xpathTabela));
    myElement.click();/*from w  ww  .  ja v a 2 s .c  o  m*/
}

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

License:Open Source License

@Override
public void tableSelectClick(String value, String l, String c, Element element) {
    String xpathTabela = preparaXPath(element, l, c);
    xpathTabela = xpathTabela.concat("//label");
    WebElement myElement = (WebElement) ((WebDriver) runner.getDriver()).findElement(By.xpath(xpathTabela));
    String selectId = myElement.getAttribute("id");
    selectId = selectId.substring(0, selectId.length() - 6);
    myElement.click();//w  w  w  .  jav  a 2 s.com

    try {
        Sleeper.SYSTEM_SLEEPER.sleep(new Duration(2, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    WebElement myElement2 = (WebElement) ((WebDriver) runner.getDriver()).findElement(
            By.xpath("//div[@id='" + selectId.toString() + "_panel']/div/ul/li[text()='" + value + "']"));
    myElement2.click();
}

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

License:Open Source License

@Override
public void tableTextSendKeys(String value, String l, String c, Element element) {
    String xpathTabela = preparaXPath(element, l, c);
    String xpathTabelaBase = xpathTabela;
    xpathTabela = xpathTabela.concat("//textarea");
    xpathTabela = xpathTabela.concat("|");
    xpathTabela = xpathTabela.concat(xpathTabelaBase);
    xpathTabela = xpathTabela.concat("//input[@type='text']");
    WebElement myElement = (WebElement) ((WebDriver) runner.getDriver()).findElement(By.xpath(xpathTabela));
    myElement.clear();//  w ww  .  jav a2 s . co  m
    myElement.sendKeys(value);
}