0

I am testing an electron app using Spectron and webdriverIO, when I try to locate an element using xpath, it doesn't seems to work, as when I log in the console it tells me the element is undefined. The html code is shown below:

<div class="shadowdiv style-scope bmp-graphs" id="bmsgraphsdiv">
<span class="style-scope bmp-graphs"><a class="style-scope bmp-
graphs">some text</a></span>
</div>

The copied Xpath from Chrome developer tool is

//*[@id="bmsgraphsdiv"]/span/a

And when I put this into my test code below (Using Spectron, Mocha and Chai)

it('text test', function () {

var text = app.client.element('//*[@id="bmsgraphsdiv"]/span/a');
var innerText = text.innerText;
console.log("text is " + innerText);

return expect(innerText).to.eventually.equal('some text');
});

Seems like it doesn't like the attribute based Xpath that I used in the element() function. Anyone know why is that or is there anyway else I can get the element without using the text, since the inner text is one of the things that I am text with?

eLRuLL
  • 17,114
  • 8
  • 67
  • 91
Harvey Lin
  • 605
  • 9
  • 23
  • On Chrome dev tools -> Elements section , give `ctrl+F` and on the search panel give your xpath, and see whether you element is highlighting or not? – Aswin Ramesh Dec 22 '17 at 06:25
  • It should work, seems ok, check if you have any iframes. – lauda Dec 22 '17 at 09:27
  • Why can't you use the link text? I don't understand your comment about it.Can you clarify what this means? `since the inner text is one of the things that I am text with` – tehbeardedone Dec 22 '17 at 14:59
  • I am suppose to test if the field/label is showing up on the page, so I guess if I have to use the inner text in my test code, I would have to assert failure instead of success. – Harvey Lin Dec 22 '17 at 16:55
  • @lauda what does iframes have to do with this, does it stops the xpath from working? – Harvey Lin Dec 22 '17 at 16:56
  • iframe behaves as it would be another page in that page, if the element is contained in the iframe you need to switch to the iframe first – lauda Dec 22 '17 at 17:31
  • Spectron does not use WDIO sync mode, So your expectation is running before your variables are assigned. Have your app.client.element return a promise with the text or use async/await. – sonhu Dec 23 '17 at 07:57
  • @sonhu I think this might be it, even through I don't fully understand your comment. I added another test before this one using the same xpath, to check if the field isVisible() or not, then I ran the same test I posted. It passed this time, might have to do with the behavior you suggested. Thanks for the heads up! – Harvey Lin Dec 23 '17 at 19:56
  • 2
    @HarveyLin I can't give you a definitive answer due to the fact I don't use Spectron but as a heavy user of webdriver.io I think my above comment is the root of your problem. An easy test is to put `app.client.element('//*[@id="bmsgraphsdiv"]/span/a').innerText` within your expect statement and see what result you get. I am not sure if .innerText is a valid function but if it is it should hopefully return the result you wanted. I think .getText() would work better. http://webdriver.io/api/property/getText.html. Lastly the css selector would be `'#bmsgraphsdiv a.bmp- graphs'`. – sonhu Dec 23 '17 at 20:41
  • Thanks for the heads up, just a side note. How do I generate a test report with webdriverIO? Do I have to hook up my entire automated test suite with something like a CI system? – Harvey Lin Dec 24 '17 at 05:52

0 Answers0