watir and ajax tables

Home > watir > watir and ajax tables

watir and ajax tables

January 23rd, 2009

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!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值