How to walk through table (by row)


#1

I’m new to WebTestIt and rather new to Protractor. Is there a good reference for basic operations? I’m trying to walk through a table and if one column contains a particular text string then it clicks a link in another column.

I can’t seem to figure this out. In Python, I would just loop through the collection of

elements and look for the text.

Thanks,


#2

Hello @Delghing,

could you share your python script (a dummied down version will also do it) and potentially the markup/site you’re testing against so we can take a closer look.

Without knowing your concrete example, in general lots of it might be expressed with a XPath. So take as an example the Example 1 table from this site. Imagine you know that the website of your target row is called http://www.jdoe.com and that one you’d like to edit by pressing the edit button next to it.
So the XPath for the website target cell would be

/html//div[@id='content']/div/table[1]//td[.='http://www.jdoe.com']

now all we need to go is traverse one back to the parent by using .. which brings us back to the grouping tr and from there on we search deep for an anchor with the text edit.
The resulting query might look like:

/html//div[@id='content']/div/table[1]//td[.='http://www.jdoe.com']/..//a[.='edit']


Here’s also a video how you can use Ranorex Selocity for tweaking your selectors and step by step building above example


#3

Thank you for the prompt reply. Below is an example of something I have recently written in Python.
If needed I can mock one up that that is publicly accessible.

The idea is pretty simple, using the xpath I find the tbody element and make that a variable. Then I search that variable for all the css selector’s

. This gives me an object that I can loop through and in the below example, I am writing it to an array. (in reality, I would be checking links and content). This might be slightly different than your example because I want to walk through all the rows, not just one in particular. Either way, I really appreciate both the help and the response.

class PendingFines(BasePage):
_pending_fines_table = ‘//*[@id=“content”]/div/div/div[2]/table/tbody’
_table_row = ‘tr’

def __init__(self, driver):
    super(PendingFines, self).__init__(driver)

def get_table(self):
    **table** = self.driver.find_element_by_xpath(PendingFines._pending_fines_table)
    **rows** = **table**.find_elements_by_css_selector('tr')
    result_table = []
    for row in **rows**:
        result_row = row.text.split()
        result_table.append(result_row)

#4

Ok so it’s about finding multiple targets.

So the good thing about Selenium is that all APIs are available for all client languages, as such you should be able to replicate your example one-on-one to Java and Typescript.
Below you can find my naive port of your sample without knowing the details of Python too well. E.g row.text.split() would translate to row.getText() in Typescript which results in a string and doing split without a parameter will result in an array with one element containing the whole string.

One thing you’ll notice is that you should

// Ranorex Webtestit Page Object File

import { browser, by, element, ExpectedConditions } from 'protractor';

export class PendingFines {
  private get _table_row() { return by.css('tr'); }
  private get _pending_fines_table() { return by.xpath('//*[@id=“content”]/div/div/div[2]/table/tbody'); }


  public async getTable() {
    await browser.wait(ExpectedConditions.visibilityOf(element(this._pending_fines_table)), browser.allScriptsTimeout, this._pending_fines_table.toString());
    const table = element(this._pending_fines_table);
    const rows = await table.findElements(this._table_row);

    for (const row of rows) {
      // no idea what results_row/result_table is
      const results_row = (await row.getText()).split('YOUR_SPLIT_KEY')
      result_table.push(results_row);
    }
  }
}

In general, Protractor is just a light wrapper above native Selenium API. You can find their API documentation here which does contain some nice samples.