0

I'm using jQuery for automated testing. The HTML for different parts of the application looks very much alike, but sometimes capitalization of class names, ids, and styles is different.

For instance, the class for result tables is sometines 'Grid', sometimes 'grid'.

So I need to use different jQuery expressions in the test code (in Java):

public String getResultCell(int row, int colum) throws Exception {
  return _browser.evaluateJavaScript("$('table.Grid').find('tbody').find('tr').eq(" + row + ").find('td').eq(" + colum + ").text()").trim();
}

And sometimes

// FIXME: fix case sensitivity
public String getResultCell(int row, int colum) throws Exception {
  return _browser.evaluateJavaScript("$('table.grid').find('tbody').find('tr').eq(" + row + ").find('td').eq(" + colum + ").text()").trim();
}

Is there a way to make jQuery case-insensitive altogether?

Peter
  • 533
  • 1
  • 6
  • 17
  • 2
    How about actually fixing the problem? If those class names are supposed to mean the same, make them the same. – Cerbrus May 28 '14 at 09:20
  • Agreed with Cerbrus, give both classes the same name. `class="grid"` in your HTML everywhere instead of both "Grid" and "grid". – Daan May 28 '14 at 09:23
  • I'm only writing the tests, not the application. I can probably get those class names fixed. At the same time, I want to write stable test code that doesn't depend on capitalization. – Peter May 28 '14 at 09:35
  • 1
    Consistency isn't a test criteria? You seem to be working around a bug in the system to write tests that work. – Cerbrus May 28 '14 at 09:44
  • I agree that it's a bug in the system that must be fixed. But I would consider it an improvement if the tests were case-insensitive, not a work around. – Peter May 28 '14 at 09:54
  • Then you might want to have a look at this question: http://stackoverflow.com/questions/3967869/writing-jquery-selector-case-insensitive-version – Cerbrus May 28 '14 at 10:02
  • Thanks, Cerbrus. It seems quite messy. It's probably better to clean up the HTML first. – Peter May 28 '14 at 11:09

1 Answers1

1

This jquery code should find table by class name and not depend on case

var classname = 'grid';
$('table').filter(function(){
   return (" "+$(this).get(0).className.toLowerCase()+" ".indexOf(' '+classname.toLowerCase()+' ')!=-1);
});

You may wrap this as a jquery method:

$.fn.filterByClass = function (classname) {
    return $(this).filter(function(){
       return (" "+$(this).get(0).className.toLowerCase()+" ".indexOf(' '+classname.toLowerCase()+' ')!=-1);
    });
};

and then use this like this: $('table').filterByClass('grid')

see demo

paulitto
  • 4,585
  • 2
  • 22
  • 28
  • Thanks for the code to handle class selectors. But I hoped there was a more generic solution that applies to all selectors. – Peter May 28 '14 at 11:17