Handling tables

Handling tables is something that I saw a few people on the internet and in my company have struggled a couple of times. You can do so many things with tables if it’s done right. So in this short post, I will talk about handling elements within the table.

In one of the previous blogs, there was a snippet how to extract all elements to the list. When you have it on the list you can make all kinds of stuff. For example usually, you would need to check or assert if table element contains some kind of text or if it has changed. When you get the list of full table elements just go through it and assert/check what you need.

		for(WebElement e : list){
			if(e.getText().contains(fileName)){
				return true;
			}
		}

That’s an easy one. But because you add it as a full web element it maintains all of the functions of web element so you can do all kind of stuff with it. Also, because you are in a loop and going through these elements one by one it means that the selected element is isolated and there should be no fear to use some stuff which would be considered dangerous in the plain site, outside the element.

Example:

		for(WebElement e : list){
			if(e.getText().contains(fileName)){
				e.findElement(By.tagName("input")).click();
			}
		}

As you can see I’m not afraid to use tag name input, which in this case mean checkbox because the WebElement is isolated and I’m searching stuff within it.

XPath/CSS int the table

If you are trying to click table link or checkbox and you can’t find anything to catch on don’t be afraid to use regular for loop.

		for(int i = 0 ; i < list.size() ; i++){
			if(list.get(i).getText().contains(fileName)){
			driver.findElement(By.xpath("//td[@id='table:"+i+":subT_rowHeader']")).click();
			Thread.sleep(3000);
			}
		}

This is a good example how can you go through the table elements and click certain things that are hardly reachable, however you’ll need to spend some time finding out the best CSS/XPath pattern for the checkbox/link/expand arrow.

All of these examples work well even if used on a grid with multithreading and the table is manipulated, expanded, customized so take care!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s