Tuesday 16 February 2016

Table value selection from objetcs

Below is HTML

<tbody>
</table>
</td>
</tr>
<tr>
<td>
<div id="divListView" style="width: 100%; height: 300px; overflow: auto; display: block;">
<table id="tblListView" class="adminlist" cellspacing="1" cellpadding="0" style="table-layout: fixed; width: 100%;">
  <tbody data-bind="template: { name: 'ActiveGradeTemplate', foreach: ActiveGrade }">
    <tr class="row0">
      <td data-bind="text:$index()+1" style="width: 5%;">1</td>
      <td data-bind="text: GradeName" style="width: 20%;">Vantage Point</td>
      <td align="right" data-bind="text: DisplayCreatedDate" style="width: 10%;">27 Mar 2013</td>
      <td align="right" data-bind="text: CreatedByUser" style="width: 10%;">Name</td>
      <td align="right" data-bind="text: DisplayModifiedDate" style="width: 10%;">27 Mar 2013</td>
      <td align="right" data-bind="text: ModifiedByUser" style="width: 10%;">Name</td>
      <td align="center" data-bind="text: Status" style="width: 5%;">Active</td>
      <td align="center" style="width: 10%;">
        <a id="lnkEdit_7" data-bind="click: $root.lnkEdit, attr:{'id':'lnkEdit_' + GradeID}" href="#">Edit</a>
        <span id="spanEdit_7" data-bind="attr:{'id':'spanEdit_' + GradeID}"></span>
      </td>
   </tr>
   <tr class="row0">
     <td data-bind="text:$index()+1" style="width: 5%;">2</td>
     <td data-bind="text: GradeName" style="width: 20%;">test grade</td>
     <td align="right" data-bind="text: DisplayCreatedDate" style="width: 10%;">Yesterday</td>
     <td align="right" data-bind="text: CreatedByUser" style="width: 10%;">Name</td>
     <td align="right" data-bind="text: DisplayModifiedDate" style="width: 10%;">Yesterday</td>
     <td align="right" data-bind="text: ModifiedByUser" style="width: 10%;">Name</td>
     <td align="center" data-bind="text: Status" style="width: 5%;">Active</td>
     <td align="center" style="width: 10%;">
       <a id="lnkEdit_11" data-bind="click: $root.lnkEdit, attr:{'id':'lnkEdit_' + GradeID}" href="#">Edit</a>
      <span id="spanEdit_11" data-bind="attr:{'id':'spanEdit_' + GradeID}"></span>
    </td>
  </tr>






Below is code to retrieve data from tables

// Grab the table 
WebElement table = driver.findElement(By.id("divListView")); 

// Now get all the TR elements from the table 
List<WebElement> allRows = table.findElements(By.tagName("tr")); 

// And iterate over them, getting the cells 
for (WebElement row : allRows) { 
    List<WebElement> cells = row.findElements(By.tagName("td")); 

    // Print the contents of each cell
    for (WebElement cell : cells) { 
        System.out.println(cell.getText());
    }
}


Thursday 4 February 2016

DB connection from selenium

  • In order to test Database using Selenium you need to
    1. Make a connection to the Database
    2. Send Queries to the Database
    3. Process the results
  • The Syntax to connect to Database is
    • DriverManager.getConnection(URL, "userid", "password" )
  • You will also need the Statement Object to send queries
    • Statement stmt = con.createStatement();
  • To send the query to database use execute query and store the results in the ResultSet object
    • ResultSet rs = stmt.executeQuery(select * from employee;);
  • Java provides lots of built-in methods to process the SQL Output using the ResultSet Object