How can I verify text exist within a table with db using Selenium WebDriver?



  • Using selenium Webdriver+Java- I am looking for a way to identify and or verify a value in a table. There are more columns in the table, but I am only concerned with the Country column, Also the number of rows varies as well as the location of the value I am looking for.

    So, say, I am looking for Country there. the country = “Germany”, who could exist in any row 1 to 9 or 10 or 20 etc.How could I locate and verify that user exist with Database query?

    **Company**                 **Contact**        **Country**
    Alfreds Futterkiste           Maria Anders        Germany
    Centro comercial Moctezuma    Francisco Chang     Mexico
    Ernst Handel                  Roland Mendel       Austria
    Island Trading                Helen Bennett       UK
    Laughing Bacchus Winecellars  Yoshi Tannamuri     Canada
    Magazzini Alimentari Riuniti  Giovanni Rovelli    Italy
    

    HTML Table Code:

    <table>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </table>
    

    I need to get the country name from ui and verify that country name is exist in database using java code.



  • (on mobile, sorry for formatting)

    String country = "Germany"
    
    // All ui rows
    List<WebElements> rows = driver.findElements('tr');
    
    // Is there any row with Country?
    Boolean existOnUI = !rows.stream().filter(
                  row => row.findElement('td:nth-of-type(3)')
                            .getText().equals(country))
                            .isEmpty();
    
    //If so, is there some row with Country on the DB?
    
    Boolean existOnDB = !dbDriver.execute("Select * from Table where country =" + country + ";").isEmpty();
    

    The variables existOnDB and existOnUI hold information if the country exists.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2