watir and ajax tables
I use watir a lot. I find that it’s easy to build a watir script, due to the simplicity of the language and versatility of the code. Even though my main role is to performance test a system for my client, watir scripts help me in my role, when I’m doing web testing.
This article is just to embed this process in my memory, or so I can refer to it in future, as well as possibly helping the wider community having trouble with watir and Ajax…
Currently, the application I’m testing has a lot of Ajax controls. With our Ajax controls, once you type the value you want to input, an drop down list appears, however this is not a list, it is a table.
You must then select the value you would like, even if you have entered the correct “string” and only one cell is returned..seems silly.
This is how I used to do it before
def workspace; @browser.frame(:name, "WORK_SPACE"); end
begin
workspace.text_field(:name, "nameOfTextField").set("Restaurants")
wait_until { workspace.cell(:title, "Restaurants").exists? }
workspace.cell(:title, "Restaurants").click
end
..And it works well. However, I found some problems when my environment went down, and I had to work in a new/different environment. Sometimes the data was a little different, I wanted my watir script to be robust, in any environment. Changing hardcoded values is a pain, and paramtising the watir script seemed a little overkill for a script I only used when I needed to re-record a new script or setup some data.
The solution, well, there’s really two solutions. Either, not input any data at all and use the Ajax drop down menu, or input the first few letters of the data and select the first option in the drop down menu.
Instead of checking if “Restaurant” exists, after I’ve typed it in, I just decided to select the first cell that of the table was returned by the Ajax control.
It’s even proven to be useful for those text fields that has changing values, or where you don’t care what you enter, just as long as it has a value populated.
Here is the quick solution.
def workspace; @browser.frame(:name, "WORK_SPACE"); end
begin
workspace.text_field(:name, "nameOfTextField").set("Restaurants")
workspace.link(:id,"AjaxDropDownButton").click
wait_until { workspace.table(:id,"EntireAjaxTableThatIsReturned").exists? }
workspace.table(:id,"EntireAjaxTableThatIsReturned")[2][1].click
end
I select row 2, column 1 in the entire Ajax table that is returned as the row 1 column 1, is the heading of the table, the second row is the value I want selected.
This may be a bit of a no brainer, but its these little slight enhancements that make the script just that little more robust.
Obviously you don’t need this line :
workspace.text_field(:name, "nameOfTextField").set("Restaurants")
If you want to select the first value, or whatever row/column number you want.
Enjoy!