开发者

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 te开发者_JAVA技巧xt.

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?


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


You could use isElementPresent as follows:

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


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/");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜