Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total

Get xpath string expression of a document element

// given a document element returns the xpath string expression of that element.
//

function getElementXPath(elt)
{
     var path = "";
     for (; elt && elt.nodeType == 1; elt = elt.parentNode)
     {
   	idx = getElementIdx(elt);
	xname = elt.tagName;
	if (idx > 1) xname += "[" + idx + "]";
	path = "/" + xname + path;
     }
 
     return path;	
}

function getElementIdx(elt)
{
    var count = 1;
    for (var sib = elt.previousSibling; sib ; sib = sib.previousSibling)
    {
        if(sib.nodeType == 1 && sib.tagName == elt.tagName)	count++
    }
    
    return count;
}

Using xpath to test the rails

Originally posted in #rubyonrails by ? springjp

def test_account_list
    #  There should be some accounts to test
    assert @ttrueheart.accounts.count > 0
 
    @request.session[:user] = @ttrueheart
    get :list
    assert_success
 
    xml = nil
    assert_nothing_thrown { xml = REXML::Document.new(@response.body) }
 
    @ttrueheart.accounts.each do |account|
      td = REXML::XPath.match(xml, "//td[text()=\"#{account.display_string}\"]")
      assert_equal(1, td.size)  # Unique entry
 
      assert_equal(3, REXML::XPath.match(td, 'following-sibling::td/a').size)
 
      href = REXML::XPath.match(td, "following-sibling::td/a[@href='/account/activity_list/#{account.id}']")
      assert_equal(1, href.size)
 
      href = REXML::XPath.match(td, "following-sibling::td/a[@href='/account/edit/#{account.id}']")
      assert_equal(1, href.size)
 
      href = REXML::XPath.match(td, "following-sibling::td/a[@href='/account/delete/#{account.id}']")
      assert_equal(1, href.size)
    end
  end
« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total