arnthorsnaer
arnthorsnaer

Reputation: 53

how do I assert that a hyperlink points to a given URL?

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:

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?

Upvotes: 2

Views: 4752

Answers (3)

Dave Hunt
Dave Hunt

Reputation: 8223

You could use isElementPresent as follows:

assertTrue(selenium.isElementPresent("//a[text()='Example Link' and @href='http://www.example.com/']");

Upvotes: 1

Dave Hunt
Dave Hunt

Reputation: 8223

Given the following HTML:

<html>
   <body>
     <a id="myLink" href="http://www.example.com">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/");

Upvotes: 1

Andrey
Andrey

Reputation: 60065

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

Upvotes: 2

Related Questions