Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying text is present within certain element

When creating selenium tests using the #{selenium} tag, how do you use complex XPath locators? I've got some simpler examples that work but I'd really like to avoid having to give every element an id just to facilitate testing.

I've tried variations like these:

#{selenium 'Sitemap'}
... 
// These work:
assertTitle('Site Map')
verifyTextPresent('Site Map')
verifyTextPresent('Login')
verifyText('id=test', 'Login')
verifyText('//ul', 'Login')
verifyText('//ul[2]', 'Login')

// this one results in "Element //ul[@class=sitemap] not found"
verifyText('Login','//ul[@class=sitemap]')

#{/selenium}

Has anyone gotten the more complex versions working? It looks like it should work according to the selenium docs. Also, is the creation of selenium tests in the context of Play documented anywhere? The only mention of it that I can find are these trivial examples.

like image 690
Brad Mace Avatar asked Nov 28 '25 09:11

Brad Mace


2 Answers

I disagree to a degree. Badly constructed xpaths that search through the entire DOM will slow down tests but well constructed xpaths should have minimal effect on speed (unless you are using IE which has a horrific JavaScript rendering engine).

Ideally you want to key your xpath to an ID as close to the area of the DOM you want to search as possible, this will ensure you are only searching a specific area of the DOM rather than using an xpath like the one shown above that will search through the entire DOM even it it does find a matching element quickly.

I'll provide some examples of what I would call good xpaths using http://www.lazeryattack.com as an example. If you have anything specific in mind shout and I'll see what I can do to help:

  • The Voice Comms link: //ul[@id='leftMenu']/li/a[.='Voice Comms']
  • H3 element that contains a span element with the text "January VAT Increase": //div[@id='news']/h3[span[.='January VAT Increase']]
  • The first paragraph of text under the above element: //div[@id='news']/h3[span[.='January VAT Increase']]/following-sibling::p[1]
  • The second paragraph of text under the above element: //div[@id='news']/h3[span[.='January VAT Increase']]/following-sibling::p[2]
  • Start searching from the featuredNews div and drop down to the h2 element with the text "New Look And Feel": //div[@id='featuredNews']/descendant::h2[.='New Look And Feel']

I would suggect using FireFox with the FireBug and FirePath extensions to help you work out xpaths, this is my personal favourite combination.

like image 168
Ardesco Avatar answered Nov 30 '25 00:11

Ardesco


Did you try

//ul[@class='sitemap']

Notice the single quote around sitemap. XPath would make your tests slow and id, name are better option to use.

like image 32
Tarun Avatar answered Nov 29 '25 23:11

Tarun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!