Find a selected radiobutton via Selenium/Webdriver - selenium

I'm using Selenium 2 (Webdriver) for automating tests on a webpage.
However I wonder if there is way to find out if a radiobutton is selected or not using webdriver framework?
I can find the element and click it by using Click() method.
I would like to test that it actually was set, or is that implicit done by the Click() method on IWebElement object?
(Using C# and NUnit)

You can determine if an element is selected by catching the element and then checking selected.
IWebElement thisElement = driver.FindElement(By.ID(//radiobutton id));
if(thisElement.Selected)
{
//do something here.
}
you can do this other ways but the .Selected is what you are looking for.

webDriver.findElement(By.className("radio")).click();
Boolean radio = webDriver.findElement(By.className("radio")).isEnabled();
if(radio.booleanValue()==true){
result.addPassedTestStepResult("user cannot uncheck the radiobutton");
}

Related

How to find prompt by Selenium

Does anybody know how to find this type of element by Selenium? (to validate its presence or text)?
I tried to catch it as alert (swithToAlert()) but it doesn't work. Any ideas? It is also can not be inspected as element and I can't find it in Elements. Thank you in advance.
This uses HTML5 form validation. This is created by the browser, and does not exist in the DOM. Therefore Selenium cannot see this.
You can access this using JavaScript. Here is a brief code sample:
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement field = driver.findElement(By.tagName("input")); // your input box
if (!(Boolean) jsExecutor.executeScript("return arguments[0].validity.valid;", field)) {
return (String) jsExecutor.executeScript("return arguments[0].validationMessage;", field);
}
The entire API is documented.
element.validity.valid
Returns true if the element's value has no validity problems; false otherwise.
So this popup is displayed if this returns false, but only after clicking Submit on the form.

Find element in selenium using xpath

I'm trying to find and click the element "Test 123" using Selenium Webdriver in C#. I've tried all the methods I can think of, but no good. I think the values are hidden before they are selected, but not sure. Any ideas, please?
enter image description here
Simple Xpath locator can do the trick:
public void ClickElementByXpath(string text) {
IWebElement element = Driver.FindElement(By.XPath("//li[contains(text(), '${text}')]");
element.Click();
}
ClickElementByXpath("Test 123");

How do I check if an element is present on a webpage?

I'm using BrowserFactory in selenium and not WebDriver. I would like to check if an element is present on the DOM. If yes click it, otherwise do something else. There should be a simple solution to it but I'm not using WebDriver. If I do use it, it opens a new browser window and does everything in that.
Can you try this method to verify if the element is present
public static void isElementPresent(WebElement element) {
//log.info("Checking if element is present");
if(element.isDisplayed();){
element.click();
}
}

how can i check if a checkbox is enabled (doesn't matter checked or unchecked.)

I am new to selenium, I am trying to verify if a checkbox is enabled on a page.It does not matter if it is checked or unchecked. I only have to verify that it can be selected i.e it is enabled for usage.
I have the id of the checkbox that is "chkEP". Kindly help. I am using java.
I have found different answers against my query via googling it but they all are verifying if chechbox is checked or not. Many many thanks for the help.
The WebElement interface has an isEnabled method on it. See here.
Otherwise, you can manually check the attribute - See here. Code:
String isDisabled = textlink.getAttribute("disabled");
if (isDisabled==null || !isDisabled.equals("disabled")){
System.out.println("View link: Enabled");
}else{
System.out.println("View link: Disabled");
}
Have you tried isEnabled()??
WebElement we = driver.findElement(By.id(""));
we.isEnabled();
I'd prefer to use the webdriver isEnabled() method.
WebElement element = driver.findElement(By.id("value"));
if (element.isEnabled()) {
//insert your code here
}
Please note that if the element is not present in the web page, noSuchElementException will be thrown in the first line itself.

How can I test context menu functionality in a web app?

I'm playing with a grails app that has a contextmenu (on right-click).
The context menu is built using Chris Domigan's jquery contextmenu plugin.
While the contextmenus do actually work, I want to have automated tests, and I can't work out how to do it.
I've tried Selenium 2.05a (ie. Webdriver), but there's no rightClick method.
I notice that HtmlUnit has a rightclick method, but I don't seem to be able to detect any difference in the DOM between before the click and after it.
Currently there's no right click method in WebDriver, there's an enhancement request opened for it - http://code.google.com/p/selenium/issues/detail?id=161
For now you can use keyboard shortcut Shift+F10 to simulate the right click on the element:
WebElement element = driver.findElement(....);
element.sendKeys(Keys.chord(Keys.SHIFT, Keys.F10));
While I'd like to be able to do it in Internet Explorer or Firefox as well, the main usage will be HtmlUnit. It's nice that the HtmlUnit HtmlElement has a rightClick() method, but unfortunately it's protected and so not accessible from the WebDriver wrapped HtmlUnitWebElement.
I wrote a hack to make it accessible, and so now I can call rightClick(), although it only works if it's running with HtmlUnit - not IE or FF.
// Needs to be in this package to get access to the element
package org.openqa.selenium.htmlunit;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
public class OpenHtmlUnitWebElement extends HtmlUnitWebElement {
// Provide a constructor, even though we don't really need it.
public OpenHtmlUnitWebElement(HtmlUnitDriver parent, HtmlElement element) {
super(parent, element);
}
// this is the method we really want.
public static HtmlElement using(HtmlUnitWebElement huwe) {
return huwe.element;
}
}
Now my (groovy) test looks like this:
import static org.openqa.selenium.htmlunit.OpenHtmlUnitWebElement.using
...
def itemWithContextMenu = driver.findElement(By.id('theId'))
if (itemWithContextMenu instanceOf HtmlUnitWebElement) {
using(itemWithContextMenu).rightClick()
def contextMenu = driver.findElement(By.id('jqContextMenu'))
assert ...
}
if you use Ruby with Capybara, this one should be useful:
module Capybara
module Node
class Element
def context_click
#session.driver.browser.action.context_click(self.native).perform
end
end
end
end