Cucumber and jstree
I'm building a rails app that has a page with a jstree object on it, and am using cucumber for my integration testing. I'm frustrated though, because I'm trying to write a cucumber ste开发者_Go百科p (using capybara/selenium for the web driver) that will click to expand one of the nodes of the jstree. I, for the life of me, cannot figure out how to do this! Executing 'click' on the jstree-icon object with the plus/minus sign in it does nothing. Any ideas?
UPDATED: Here is an example tree, as simple as I can make it pretty much, that has a tree. http://jsfiddle.net/aV62w/ - now, I need to simulate the act of clicking on the plus by the Node B folder, to expand it.
You can interact with the tree by utilising jstree's methods -- for this to work you need to set id's on the nodes, so they can be referenced by jstree.
jQuery
// toggle_node, or open_node, or close_node
$('#tree').jstree('toggle_node', '#node_b');
Fiddle: http://jsfiddle.net/aV62w/1/
The html-unit web driver (in Java) comes with a executeScript, which can be used in a web step. I did this for a Grails project with cucumber (it uses groovy)
Then(~"I open jstree folder \"(.*)\"") { String folderName ->
js = """
var obj = \$('div.main a:contains("' + arguments[0] +'")').parent();
var tree = obj.parents('div.jstree-0');
tree.jstree('toggle_node',obj);
"""
browser.executeScript(js,folderName)
Thread.sleep(5000); // wait for ajax call, so next step will have tree loaded
}
Note that all DOM finding and traversing is done in JS with JQuery and there is no need to attach an ID - navigation can be improved, of course :) Hope it helps.
精彩评论