Thursday, December 18, 2008

how to automate sproutcore appliaction through watir

Automation of a sproutcore application is still a headache for programmers. We can automate sproutecore application through selenium and watir as well.

I have successfully created a sample script which runs from both selenium and watir as well.
Actually, I have managed some api which run according to the environment like watir or selenium.

The question how you automate a sproutcore application while there is no such HTML tag on the page. Complete application runs through JavaScript. We can not click on any event directly from id. So, how can we deal with this problem?
So guys cheer up in the sproutcore everything is a link. So I have classified the event into three parts:

1. text field
2. button
3. Check box or radio button.

For the first case we deal it normally as we did normally with watir

$browser.text_field(:id,”id”).set(“hello sproutcore”)

The second and third case is different as we can not click on the button or any event directly.

We need to find out the controller and corresponding method associated to the event. We need to execute JavaScript. Well guys, there is a hell lot of difference to click on button or check box. Have a look at the controller.

There is a direct method associated to the button. Check out the view page this is an action while radio button and check box bind the value. Now go to the controller you will see that the corresponding method is actually bind from the variable. That means it always observes the value of the variable and performs action accordingly.

Ok. I am giving you an example how to automate it from watir.

I have create a sample application which have a button with the id=”toggle_button”.
I just find out the source code and go to the index of “toggle_button”. The reason being I need the controller and method information as you can see the following source code:


toggleButton: SC.ButtonView.extend({
action: "SproutHello.appController.toggleGreeting"
}).outletFor("#toggle_button") ,



So now I have all information. I just need to create a JavaScript and just execute it.

body_array = page_source()
action_str = body_array[body_array.index("}).outletFor(\"##{button_id}\")")-1]
action = action_str.split("\"")[1]
ff.js_eval("var w_all = getWindows(); var w_one = w_all[#{window_count}]; w_one.content.#{action}();")


So, guys start playing with clicking on button. :)

We can not click on check_box with the above code the difference is in the javascript because check box and radio button the working as toggle. If it is on then you need to turn it off and vice versa. So you need to get the value and set the value.

body_array = page_source()
action_str = body_array[body_array.index("}).outletFor(\"##{event_id}\")")-3]
action = action_str.split("\"")[1]
_obj = action.split(".")
_get = "w_one.content" + "." + _obj[0].to_s + "." + _obj[1].to_s + ".get(\"" + _obj[2] + "\")"
_set = "w_one.content" + "." + _obj[0].to_s + "." + _obj[1].to_s + ".set(\"" + _obj[2] +"\",!" + _get.to_s + ")"
ff.js_eval("var w_all = getWindows(); var w_one = w_all[#{window_count}]; #{_set}")


Don’t get confused with window count this is to check how much firefox windows are open and on which script should work upon.

Enjoy sproutcore automation
:)
gud luck.

6 comments:

  1. with regards to your post do you have more information on using watir with sproutcore. Or if you could provide more information about how to access link in sproutcore that have the same class attribute using Watir.

    ReplyDelete
  2. Hi tester86,

    As i have mentioned in the blog, while automating sproutcore application every element need to be treated as link.

    I think you casn driectly clink on the link by identfing it on page. Though Please find soem way to click on element as everthing is a link :P


    -
    ie.link(:text, "Feedback").click

    -
    ie.div(:class, "sc-view contextMenu").link(:index, 1).fire_event("onMouseDown")

    ie.div(:class, "sc-view contextMenu").link(:index, 1).fire_event("onMouseUp")

    -
    topbuttons = ie.div(:class, "sc-view sc-segmented-view sc-layout-horizontal sc-regular-size")

    topbuttons.link(:index, 2).fire_event("onMouseDown")

    topbuttons.link(:index, 2).fire_event("onMouseUp")


    Enjoy....

    ReplyDelete
  3. Yes, that is true everthing in sproutcore is a link. I managed to solve my problem by using indexing since I have a text box that has over 100 links.

    I have another question. If on a web page there are three boxes with over 100 links in each box using watir what is the best way to set the focus on each box so then I can click on a link.

    I have tried using fire_event such as focus and onMouseOver but it just checks the link in the first box....

    ReplyDelete
  4. Find out the location(xpath) or the ID of you box over the page.
    Then just traverse over the links.....
    Watir would click on the first box because either you are giving the location of first box only or you are failing to give appropriate id of second or third box so default Watir pick the first box :)

    ReplyDelete
  5. Hi

    Final question for you. Everything is sproutcore has a id and when an element is added to the page the numbers will automaticlly change.

    In a watir script for a link that ID value is hard coded and when something new is added that ID value will change and the script will fail.

    Using watir how can we overcome this. Note that I need to use either the ID or the index, [I cannot access it via type as type is always text].

    Thanks

    ReplyDelete
  6. Key solution is to use Recursive Descent Xpath.

    Problem of changing index might be resolved by making Recursive Descent xpath by Text or name. I did not verify at what extent it will work for you.

    Clicking be Id, Title(Element attribute) in sproutcore application is still a puzzle for me.......


    Remember: It is expected to rework on automation scripts whenever a short change is made on the page. :)

    ReplyDelete