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

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:
link text
link text
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/");

Related

click on a image link with Capybara

I am trying to click on a image link with a Capybara / Rspec test. I am having very little success at the moment.
I am trying to select the link with href "/post/3", (knowing that they are other links before). I have tried many combinations of xpath without success. The only combination working was
page.first(:xpath, //a).click
however when I have changed the file and added more links above my Capybara test is broken.
<div class='row'>
<img id="imagen3" src="/system/posts/images/000/000/003/original/frankie-mannings-102nd-birthday-5160522641047552-hp.gif?1464448829" alt="Frankie mannings 102nd birthday 5160522641047552 hp" />
<p>caption</p>
</div>
How can I select that link, and click it?
ok I got it:
find(:xpath, "//a[contains(#href,'posts/3}')]").click
//a[contains(#href, 'posts/3’)]

How to get the xpath for a link?

I'm writing selenium test for a wicket application and having trouble to get some xpaths.
For example:
I'm trying to get the xpath for a link. Here is the the link created by the wicket dynamically:
<span>
<a id="id2b3" href="#" onclick="var wcall=wicketAjaxGet('./participante_geral?7-1.IBehaviorListener.0-containerDiv-frameSetDiv-gridDiv-linhas-body-rows-1-cells-2-cell-linkPanel',function() { }.bind(this),function() { }.bind(this), function() {return Wicket.$('id2b3') != null;}.bind(this));return !wcall;">
<span>Some String</span>
</a>
</span>
I'm trying to get the xpath for the link like this:
//a/span[(text() = 'Some String')]/..
But when I run my selenium test the WebDriver for firefox, I can't locate the element/link.
When I run
$x("//a/span[(text() = 'Some String')]/..")
in chromium console I can locate the element with success.
But when I run the same command in firefox I get an array containing the link.
What is wrong with my xpath?
Thanks in advance.
Try to follow the following syntax when creating a xpath
//tagname[#attribute='value']
Try to use contains function-
//a[#id='id2b3']/span[contains(text(), 'Some String')]
Wicket supports rendering an extra attribute to each component for this use case, see IDebugSettings#setOutputComponentPath(boolean) [1]. This will render wicketpath attribute. You can use it like: //a/[#wicketpath='grand:parent:child:id'].
This is for Wicket 1.5.x/6.x.
For Wicket 7.x+ there is DebugSettings#setComponentPathAttributeName(String). If it is non-null then Wicket will use this String as a name for the attribute, e.g. data-wicket-path.
https://github.com/apache/wicket/blob/wicket-1.5.x/wicket-core/src/main/java/org/apache/wicket/settings/IDebugSettings.java#L39

element not supported by watir webdriver

So element_by_xpath has been removed from watir webdriver and I am wondering if there is something similar to this still in existence. Basically there is a path element (pie chart) that is clickable and I need to have a regression test set up to for it. The people who designed the website apparently thought it would be cool to have everything inside this thing to be custom tags and there is nothing supported by watir webdriver that I can point to, at least to my knowledge.
element_by_xpath was replaced with an :xpath locator, which is used like any other locator such as :text.
For example, say you have a custom 'asdf' element in your html:
<html>
<body>
<asdf>text</asdf>
</body>
</html>
Then you could locate the element via xpath like:
browser.element(:xpath => '//asdf').text
#=> "text"
The usual suggestion is to avoid xpath. If you are only using the xpath because of the tag name, you can use the :tag_name locator instead:
browser.element(:tag_name => 'asdf').text
#=> "text"

Writing xpath and CSS locator/path for Selenium Automated Test

I have the following HTML:
<input type="submit" style="-webkit-user-select:none;line-height:100%;height:30px" value="Advanced Search" class="jfk-button jfk-button-action adv-button">
I have written xpath as: //input[#value='Advanced Search']
What is the CSS locator/path?
It's difficult to answer as optimum search selectors need the entire source code to be written, as several DOM Elements in the document could be returned for a generic selector.
In this case, a more detailed selector would be :
input.adv-button[value='Advanced Search']
You can convert your xpath into corresponding CSS Locator by using the following website:
http://cssify.appspot.com/
For example:
Go to site http://cssify.appspot.com/
Insert the XPath //input[#value='Advanced Search'] into text field
Click submit button and observe the result
You can see the corresponding CSS Locator as follows:
input[value="Advanced Search"]

Selenium Web driver xpath, span locator

Selenium Webdriver. Looking to Locate New Article from following code. Please note this is under an iframe.
<img class="rtbIcon" src="/icons/16/app/shadow/document_add.png" alt="">
<span class="rtbText">New Article</span>
I have tried to locate with xpath and many other ways. But following is what I get everytime
Code : driver.findElement(By.xpath("id('RadToolBar1'):div:div:div:ul:li[3]:a:span:span:span:span"));
Result:
The given selector id('RadToolBar1'):div:div:div:ul:li[3]:a:span:span:span:span is either invalid or does not result in a WebElement. The following error occurred:
New article has no name, id so please if some one can help find me solution.
Your xpath seems to be wrong. The best way to get the xpath for any element on a page is by installing mozilla add on - Fire Bug. You can inspect any element using this add on and also copy the correct xpath of your element present on the page.
This should be your xpath -
driver.findElement(By.xpath("//*[#class='rtbText']"));
or
driver.findElement(By.linkText("New Article"));
One of these should work. Let me know if you face any problem.

Resources