tags:

views:

72

answers:

3

I am using Selenium IDE and Selenium RC to check links on a website, but I am having trouble finding a way to assert that a link points to a specified url and has the specified text.

Constraints:

  • locating the element by dom or xpath is not feasible for it will make the test brittle
  • there are multiple link elements with identical link text

The HTML:

<a href="path_x">link text</a>
<a href="path_y">link text</a>

The test I'd like to do (rspec):

page.is_element_present?("Href=path_y, Link=link text").should be_true
#locator parameters are in order of preference

Any ideas?

+2  A: 

i guess get_attribute should help. you should get href from link, it is attribute.

Andrey
Interesting. In my case though, I have multiple elements with the same text but want to avoid calling elments by xpath or dom.get_attribute does not return an array when given a locator that matches multiple elements. Instead it returns the first element.
arnthorsnaer
@arnthorsnaer.mp use id attribute then, it should be unique. Or use xpath - i always use it, why not?
Andrey
The links don't have ID's :)I will use xpath if I have to but making my test depend on view implementation specifics does make them more brittle.I'd vote you up but I lack reputation points.
arnthorsnaer
@arnthorsnaer your tests are dependent on view implementation, because it is UI test. it is normal. why can't links have id btw? you can wrap it into div and assign id to div
Andrey
@Andrey In this case I am writing acceptance tests, but I do not write the software (and have limited access to controlling the implementation). Hence I care more about the domain information being presented than the implementation specific details. Of course, if I cant solve this I'll use xpath :)
arnthorsnaer
+1  A: 

Given the following HTML:

<html>
   <body>
     <a id="myLink" href="http://www.example.com"&gt;Example Link</a>
   </body>
</html>

You could use the following Selenium commands (using Java/TestNG, and Selenium 1.x)

assertEquals(selenium.getText("id=myLink@href"), "Example Link");
assertEquals(selenium.getAttribute("id=myLink@href"), "http://www.example.com/");

Using Selenium 2.x the example would be:

WebElement myLink = driver.findElement(By.id("myLink"));
assertEquals(myLink.getText(), "Example Link");
assertEquals(myLink.getAttribute("href"), "http://www.example.com/");
Dave Hunt
Good suggestion, but I was hoping to be able to select the element without specifying the ID (by link AND href due to multiple links with the same text).
arnthorsnaer
You should probably have mentioned that in your question =) If you update the question (perhaps with a HTML snippet) I'd be happy to attempt a more suitable answer. :)
Dave Hunt
Thanks for the tip Dave. I've updated the question.
arnthorsnaer
+1  A: 

You could use isElementPresent as follows:

assertTrue(selenium.isElementPresent("//a[text()='Example Link' and @href='http://www.example.com/']");
Dave Hunt
I could and I shall. Well played! Thanks.
arnthorsnaer