psb
psb

Reputation: 21

How to iterate through table using selenium?

I have a table called UserManagement that contains information about the user.This table gets updated whenever new user is created. If i create two users then i need check whether two users are actually created or not. Table contains ID,UserName,FirstName,LastName,Bdate..ctc. Here ID will be generated automatically.
I am running Selenium-TestNG script.Using Selenium,how can i get the UserName of the two users which i have created? Should i have to iterate through table? If so how to iterate through the table?

Upvotes: 2

Views: 20248

Answers (4)

Laura Hannah Vachon
Laura Hannah Vachon

Reputation: 133

Generically, something like this?

table = @browser.table(:id,'tableID')
table.rows.each do |row|
    # perform row operations here
    row.cells.each do |cell|
        # do cell operations here
   end
end

Upvotes: 0

Nagaraju Akula
Nagaraju Akula

Reputation: 21

Get the number of rows using:

int noOfRowsInTable = selenium.getXpathCount("//table[@id='TableId']//tr");

If the UserName you want to get is at fixed position, let's say at 2nd position, then for each row iterate as given below:

selenium.getText("xpath=//table[@id='TableId']//tr//td[1]");

Note: we can find the number of columns in that table using same procedure

int noOfColumnsInTable = selenium.getXpathCount("//table[@id='TableId']//tr//td");

Upvotes: 1

Ravikishore
Ravikishore

Reputation: 19

Get the count of rows using Selenium.getxpathcount(\@id = fjsfj\td\tr") in a variable rowcount

Give the columncount in a variable

Ex:

int colcount = 5;

Give the req i.e New user

String user1 = "ABC"

for(i = 0;i <=rowcount;i++)
{
   for(j=0;j<=colcount;j++)
   {
      if (user1==selenium.gettable("//@[id=dldl/tbody" +i "td"+j))
      {
         system.out.println(user1  + "Inserted");
         break;
      }
      break;
   }
}

Upvotes: 1

Wesley Wiser
Wesley Wiser

Reputation: 9881

Use ISelenium.GetTable(string) to get the contents of the table cells you want. For example, selenium.GetTable("UserManagement.0.1"); will return the contents of the table's first row and second column. You could then assert that the correct username or usernames appear in the table.

Upvotes: 2

Related Questions