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:testTheClient.java

@Test
public void lookUp2002() throws Exception {
    WebElement element = (new WebDriverWait(driver, WAIT_MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return driver.findElement(By.xpath("//*[@id=\"filter\"]"));
            });//from   w w  w . j  a  va 2 s  .  com
    element.clear();
    element.sendKeys("2002");
    WebElement table = driver.findElement(By.tagName("tbody"));
    List<WebElement> rows = table.findElements(By.tagName("tr"));
    Assert.assertThat(rows.size(), is(2));
}

From source file:testTheClient.java

@Test
public void sortByYear() {
    WebElement sort = (new WebDriverWait(driver, WAIT_MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return driver.findElement(By.xpath("//*[@id=\"h_year\"]"));
            });/*w w w. ja  v  a  2s . c  om*/
    sort.click();
    WebElement table = driver.findElement(By.tagName("tbody"));
    List<WebElement> sortedRows = table.findElements(By.tagName("tr"));
    Assert.assertThat(sortedRows.get(0).findElements(By.tagName("td")).get(0).getText(), is("938"));
    Assert.assertThat(sortedRows.get(4).findElements(By.tagName("td")).get(0).getText(), is("940"));
}

From source file:testTheClient.java

@Test
public void verifyAlterations() throws Exception {

    WebElement editButton = null;/*from  w w w.  jav a 2  s. c  o m*/
    List<WebElement> rows = (new WebDriverWait(driver, WAIT_MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));
    ;
    for (int i = 0; i < rows.size(); i++) {
        if (rows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
            editButton = rows.get(i);
            break;
        }
    }
    editButton = editButton.findElements(By.tagName("td")).get(7).findElements(By.tagName("a")).get(0);
    //click the edit button
    editButton.click();
    //clear description field and type coolscars 
    WebElement element = driver.findElement(By.id("description"));
    element.clear();
    element.sendKeys("cool cars");
    driver.findElement(By.xpath("//*[@id=\"save\"]")).click();
    List<WebElement> updatedRows = (new WebDriverWait(driver, WAIT_MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));
    ;
    String newDescr = null;
    for (int i = 0; i < updatedRows.size(); i++) {
        if (updatedRows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
            newDescr = updatedRows.get(i).findElements(By.tagName("td")).get(5).getText();
            break;
        }
    }
    assertThat(newDescr, is("cool cars"));
}

From source file:testTheClient.java

@Test
public void testFieldErrorPrompt() {
    driver.findElement(By.id("new")).click();
    driver.findElement(By.id("save")).click();

    String errorMessage = (new WebDriverWait(driver, WAIT_MAX))
            .until((ExpectedCondition<String>) (WebDriver d) -> {
                return d.findElement(By.xpath("//*[@id=\"submiterr\"]")).getText();
            });//from  w  w w.  j a  v  a 2 s .  c o  m
    Assert.assertThat(errorMessage, is("All fields are required"));
}

From source file:testTheClient.java

@Test
public void zCreateNewCarEntry() throws Exception {
    (new WebDriverWait(driver, WAIT_MAX)).until((ExpectedCondition<String>) (WebDriver d) -> {
        return d.findElement(By.xpath("//*[@id=\"new\"]")).getText();
    });/*from w ww .j ava 2  s .com*/
    //populate fields
    driver.findElement(By.id("new")).click();
    driver.findElement(By.id("year")).sendKeys("2008");
    driver.findElement(By.id("registered")).sendKeys("2002-5-5");
    driver.findElement(By.id("make")).sendKeys("Kia");
    driver.findElement(By.id("model")).sendKeys("Rio");
    driver.findElement(By.id("description")).sendKeys("As new");
    driver.findElement(By.id("price")).sendKeys("31000");
    //save
    driver.findElement(By.id("save")).click();
    List<WebElement> updatedRows = (new WebDriverWait(driver, WAIT_MAX))
            .until((ExpectedCondition<WebElement>) (WebDriver d) -> {
                return d.findElement(By.tagName("tbody"));
            }).findElements(By.tagName("tr"));
    assertThat(updatedRows.size(), is(6));
    assertThat(updatedRows.get(5).findElements(By.tagName("td")).get(1).getText(), is("2008"));
    assertThat(updatedRows.get(5).findElements(By.tagName("td")).get(4).getText(), is("Rio"));

}

From source file:AddTask.java

@Test
public void testAddTask() throws Exception {
    driver.get(baseUrl + "/LoginWebApp/taskPage.jsp?username=admin");
    driver.findElement(By.xpath("(//button[@type='button'])[2]")).click();
    driver.findElement(By.id("tasktitle")).clear();
    driver.findElement(By.id("tasktitle")).sendKeys("teste");
    driver.findElement(By.id("taskmessage")).clear();
    driver.findElement(By.id("taskmessage")).sendKeys("teste");
    driver.findElement(By.id("date")).clear();
    driver.findElement(By.id("date")).sendKeys("03/05/2010");
    driver.findElement(By.xpath("(//button[@type='button'])[4]")).click();
    assertEquals("Task successfully added!", closeAlertAndGetItsText());
}

From source file:openCloseTables.java

public static void main(String[] args) throws IOException {

    System.setProperty("webdriver.ie.driver",
            "C:\\Users\\Colin Cook\\Desktop\\selenium-2.53.1\\IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();

    driver.get("http://10.0.1.53/opstar_test/");

    out.println("Launching Internet Explorer browser..");
    driver.manage().window().maximize();

    try {//from w w  w .  j  av a2s  .  co  m
        Thread.sleep(3000); // pauses for 3 seconds
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    out.println("The title of the page being tested is:   " + driver.getTitle());

    WebElement acknowledge = driver
            .findElement(By.xpath("html/body/div[5]/div[1]/div[2]/div[2]/div[1]/div/div[2]/center/a"));
    acknowledge.click(); //first popup when you go on site

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement applications = //driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div[1]/ul[2]/li[5]/a/span"));
            driver.findElement(By.linkText("Applications"));
    applications.click();

    WebElement toTools = driver
            .findElement(By.xpath("html/body/div[3]/div[2]/div/div[1]/ul[2]/li[7]/ul/li[2]/a"));
    toTools.click();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement traingFolder = driver
            .findElement(By.xpath("html/body/div[3]/div[2]/div/div[1]/ul[2]/li[7]/ul/li[2]/ul/li/a"));
    traingFolder.click();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement closeBox = driver.findElement(By.id("cboxClose"));
    closeBox.click(); //clicks second popup acknolwedge box

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement textBox = driver.findElement(By
            .xpath("html/body/div[3]/div[4]/div[2]/div/div[1]/div/div[2]/div/form/div/div/span/span[1]/span"));
    textBox.click();

    textBox.sendKeys("ken"); //first three letters of someones name

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    textBox.sendKeys(Keys.ENTER);

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table1 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[1]/div/div[1]/div/a[2]"));
    table1.click();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table1.click();

    out.println("The Designations folder has been successfully opened and closed");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table2 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[2]/div/div[1]/div/a[2]"));
    table2.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table2.click();
    out.println("The Waivers folder has been successfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table3 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[3]/div/div[1]/div/a[2]"));
    table3.click();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table3.click();
    out.println("The Professional Licenses/ Certificates / Medical folder has been opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table4 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[4]/div/div[1]/div/a[2]"));
    table4.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table4.click();
    out.println("The First Aid folder has been succesfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table5 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[5]/div/div[1]/div/a[2]"));
    table5.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table5.click();
    out.println("The Star folder has been successfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table6 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[6]/div/div[1]/div/a[2]"));
    table6.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    table6.click();
    out.println("The Training Courses and Tests folder has been successfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table7 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[7]/div/div[1]/div/a[2]"));
    table7.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table7.click();
    out.println(
            "The Professional Development / Individual Development Plan folder has been successfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table8 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[8]/div/div[1]/div/a[2]"));
    table8.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    table8.click();
    out.println("The FLETC Certificates folder has been successfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement table9 = driver.findElement(
            By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[2]/div[9]/div/div[1]/div/a[2]"));
    table9.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    table9.click();
    out.println("The Miscellaneous folder has been succesfully opened and closed");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }

    WebElement qualifications = driver
            .findElement(By.xpath("html/body/div[3]/div[4]/div[2]/div/div[2]/div[1]/div/div/div[1]/div/a[2]"));
    qualifications.click();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    qualifications.click();
    out.println("The Qualifications folder has been successfully opened and closed");

    // I should program test scripts like this in the future

    //        try {
    //            WebElement xbox = driver.findElement(By.id("cboxClose"));
    //            xbox.click();
    //        } catch(Exception e) {
    //            System.err.print("NO NO NO that ");
    //        }

}

From source file:ProveedorTest.java

@Test
public void testCreateProveedor() throws Exception {

    /**//  ww w .j  a v  a2s  . c om
     * Comando que realiza click sobre el boton "create" del toolbar. La
     * funcin 'find' encuentra el control y posteriormente hace clic en el
     * mismo. La forma en la que se busca el control es utilizando
     * expresiones xPath ya que los id de los mismos nunca son iguales (se
     * generan con junto con el valor de componentId que vara).
     */
    driver.findElement(By.xpath("//button[contains(@id,'createButton')]")).click();

    /**
     * Comando que duerme el Thread del test por 2 segundos para dejar que
     * el efecto 'slide down' de backbone abra el formulario de createSport.
     */
    Thread.sleep(2000);

    String name = "Nombresito";
    String email = "test@test.com";
    String telefono = "1231235";
    String direccion = "carrera 1 con calle 2";
    String observaciones = "jejejejejeje";
    /**
     * Comando que busca el elemento 'name' en el html actual.
     * Posteriormente limpia su contenido (comando clean).
     */
    driver.findElement(By.id("name")).clear();
    /**
     * Comando que simula la escritura de un valor en el elemento(sendKeys)
     * con el String de parmetro sobre // el elemento encontrado.
     */
    driver.findElement(By.id("name")).sendKeys(name);

    //Comandos para llenar el campo email
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys(email);

    /**
     * Comandos para llenar el campo telefono
     */
    driver.findElement(By.id("telefono")).clear();
    driver.findElement(By.id("telefono")).sendKeys(telefono);

    /**
     * Comandos para llenar el campo direccion
     */
    driver.findElement(By.id("direccion")).clear();
    driver.findElement(By.id("direccion")).sendKeys(direccion);

    /**
     * Comandos para llenar el campo observaciones
     */
    driver.findElement(By.id("observaciones")).clear();
    driver.findElement(By.id("observaciones")).sendKeys(observaciones);
    /**
     * Comando que encuentra y hace clic sobre el boton "Save" del toolbar
     * (una vez mas encontrado por una expresin Xpath)
     */
    driver.findElement(By.xpath("//button[contains(@id,'saveButton')]")).click();

    /**
     * Comando que duerme el thread para esperar el efecto de slide down que
     * abre la lista
     */
    Thread.sleep(2000);
    /**
     * Comando que obtiene el div azul de creacin exitosa. Si se obtiene,
     * la prueba va bien, si no, saldr un error y la prueba quedar como
     * fllida.
     */
    WebElement dialog = driver.findElement(By.xpath("//div[contains(@style,'display: block;')]"));
    /**
     * Comando que obtiene la tabla con el elemento que se cre
     * anteriormente.
     */
    List<WebElement> table = driver
            .findElements(By.xpath("//table[contains(@class,'table striped')]/tbody/tr"));
    boolean sucess = false;
    /**
     * Se itera sobre los elementos de la tabla para ver si el nuevo
     * elemento creado est en la lista
     */
    for (WebElement webElement : table) {
        List<WebElement> elems = webElement.findElements(By.xpath("td"));

        if (elems.get(0).getText().equals(name) && elems.get(1).getText().equals(email)
                && elems.get(2).getText().equals(telefono) && elems.get(3).getText().equals(direccion)
                && elems.get(4).getText().equals(observaciones))

        {
            /**
             * si se encuentra la fila, la variable 'fail' pasa a true,
             * indicando que el elemento creado esta en la lista.
             */
            sucess = true;
        }

    }
    /**
     * la prueba es exitosa si se encontr el dialogo de creacin exitosa y
     * el nuevo elemento est en la lista.
     */
    assertTrue(dialog != null && sucess);
}

From source file:ProveedorTest.java

@Test
public void testUpdateProveedor() throws Exception {

    String name = "Un nombre ahi";
    String email = "prueba@prueba.com.co";
    String telefono = "9999999";
    String direccion = "diagonal 5 con calle 6";
    String observaciones = "hohohoo";

    /**//  w ww.  j a va2  s .  com
     * Se hace clic en el vinculo "Edit" del primer elemento de la lista de
     * sports
     */
    driver.findElement(By.linkText("Editar")).click();
    Thread.sleep(2000);
    /**
     * Se realiza el mismo proceso de diligenciamento de los campos con los
     * cambios
     */
    driver.findElement(By.id("name")).clear();
    driver.findElement(By.id("name")).sendKeys(name);
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys(email);
    driver.findElement(By.id("telefono")).clear();
    driver.findElement(By.id("telefono")).sendKeys(telefono);
    driver.findElement(By.id("direccion")).clear();
    driver.findElement(By.id("direccion")).sendKeys(direccion);
    driver.findElement(By.id("observaciones")).clear();
    driver.findElement(By.id("observaciones")).sendKeys(observaciones);

    driver.findElement(By.xpath("//button[contains(@id,'saveButton')]")).click();
    Thread.sleep(2000);
    /**
     * Se verifica que en la lista de respuesta hallan aparecido los cambios
     * en el elemento y tambin el mensaje de edicin exitosa.
     */
    WebElement dialog = driver.findElement(By.xpath("//div[contains(@style,'display: block;')]"));
    List<WebElement> table = driver
            .findElements(By.xpath("//table[contains(@class,'table striped')]/tbody/tr"));
    boolean fail = false;
    for (WebElement webElement : table) {
        List<WebElement> elems = webElement.findElements(By.xpath("td"));

        if (elems.get(0).getText().equals(name) && elems.get(1).getText().equals(email)
                && elems.get(2).getText().equals(telefono) && elems.get(3).getText().equals(direccion)
                && elems.get(4).getText().equals(observaciones)) {
            fail = true;
        }

    }
    assertTrue(dialog != null && fail);
}

From source file:ProveedorTest.java

@Test
public void testDeleteProveedor() throws Exception {
    /**/* w  w w . j  a  v  a  2 s .co m*/
     * Se hace clic en el vinculo "Delete" del primer elemento de la lista
     * de sports
     */
    driver.findElement(By.linkText("Eliminar")).click();
    Thread.sleep(2000);
    /**
     * Se verifica que en la lista el elemento halla desaparecido. Si
     * existe, hubo un error.
     */
    try {
        List<WebElement> table = driver
                .findElements(By.xpath("//table[contains(@class,'table striped')]/tbody/tr"));
        boolean fail = false;
        String name = "Un nombre ahi";
        String email = "prueba@prueba.com.co";
        String telefono = "9999999";
        String direccion = "diagonal 5 con calle 6";
        String observaciones = "hohohoo";
        for (WebElement webElement : table) {
            List<WebElement> elems = webElement.findElements(By.xpath("td"));

            if (elems.get(0).getText().equals(name) && elems.get(1).getText().equals(email)
                    && elems.get(2).getText().equals(telefono) && elems.get(3).getText().equals(direccion)
                    && elems.get(4).getText().equals(observaciones)) {
                fail = true;
            }

        }

        WebElement dialog = driver.findElement(By.xpath("//div[contains(@style,'display: block;')]"));
        assertTrue(dialog != null && !fail);
    } catch (Exception e) {
        assertTrue(true);
    }

}