2

Just started working with CasperJS, writing a few sample tests. Everything works fine when using CSS Selectors, however I'm having trouble using Xpath. The only reason I want to use xpath is to create a more resilient locator, i.e.

'//a[text()="Office Building"]' versus the css version: #content > div > div > div > div:nth-child(1) > a

Here's what I've tried (Note: I have tested this xpath in the browser console):

Tried passing directly

casper.waitUntilVisible('//a[text()="Office Building"]');

Tried specifying the locator explicitly as Xpath:

casper.waitUntilVisible({
    type: 'xpath',
    path: '//a[text()="Office Building"]'
});

And tried requiring the helper selectXPath from the module as documented here:

var x = require('casper').selectXPath;
casper.waitUntilVisible(x('//a[text()="Office Building"]'))';

All of these result in a timeout error Wait timeout of 30000ms expired, exiting. because the element is never "found". Any ideas?

Gunderson
  • 3,263
  • 2
  • 16
  • 34
  • What about the "contains" version: `//a[contains(., "Office Building")]`? – alecxe Jan 13 '17 at 15:40
  • Another optiion would be casper.waitForText('Office Building'). The last example you wrote should work, but better with contains in case of space. – dasmelch Jan 13 '17 at 15:51
  • @alecxe wow you're everywhere :) unfortunately same result. I may try a few different methods, such as using `evaluate` to get into the DOM, and force some things there directly – Gunderson Jan 13 '17 at 15:53
  • @dasmelch unfortunately that won't help me too much, cause my next step is going to be `casper.click('//a[text()="Office Building"]');` So it's really the locator that I need to figure out. – Gunderson Jan 13 '17 at 15:58
  • Can't you use another option than the inner html text, something like id, class or any other tag paramter? It's always better then the text. You can use @alecxe soltion for that, like //a[contains(@class, "office_building")] or //a[contains(@href, 'office_building')] or etc.? – dasmelch Jan 13 '17 at 16:05
  • @dasmelch It's a series of angular repeater elements, so they all have the same attributes. I think text is the only unique thing about them =/ – Gunderson Jan 13 '17 at 16:26
  • 1
    Ok got it. ;) And a casper.waitForText and casper.clickLabel doesn't do the job? – dasmelch Jan 13 '17 at 17:42
  • 1
    @dasmelch ah, I did not see the clickLabel method. Combined with waitForText works for me! If you want to write up a small answer I will accept that! Thanks! – Gunderson Jan 13 '17 at 19:03

1 Answers1

1

This could be a solution if you are only got text as identifier for waiting and clicking a link in casperjs without using xPath.
It's more simple to wait for the text and then use clickLabel (if the text is unique):

casper.start('http://yourTestUrl.com');

casper.then(function() {
    casper.waitForText('Office Building');
});

casper.then(function() {
    casper.clickLabel('Office Building');
});

casper.run();
dasmelch
  • 532
  • 3
  • 11