How to assert the text box is enabled or disabled - selenium

i would like to assert whether a particular text box is disabled or enabled. below is the HTML for it. the difference between the disabled and enabled text box is only By disabled Attribute towards the end.
disabled text box HTML:
<input controlid="txtIPAddress" class="textfield form-control servicefield invItem_dynamic_validation valid" id="OrderProperties_4__Value" isdisabled="true" name="OrderProperties[4].Value" style="display:" type="text" value="" disabled="">
enagled text box HTML-
<input controlid="txtIPAddress" class="textfield form-control servicefield invItem_dynamic_validation valid" id="OrderProperties_4__Value" isdisabled="true" name="OrderProperties[4].Value" style="display:" type="text" value="">
I tried element.isEnabled() and element.isEnabled() methods to no avail.
any help much appreciated!

isEnabled()
As per the documentation isEnabled() method is defined as:
boolean isEnabled()
Description:
Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
Returns:
True if the element is enabled, false otherwise.
So your code trial as element.isEnabled() was perfect to retrieve the status whether the element was enabled or not provided the element uniquely identified the node which you have provided within the question.
Alternative
As an alternative you can try to validate if the elelemt is present without the attribute disabled using the following solution:
try {
driver.findElement(By.xpath("//input[#class='textfield form-control servicefield invItem_dynamic_validation valid' and not(disabled)][#controlid='txtIPAddress']"));
System.out.println("Element is enabled");
} catch (NoSuchElementException e) {
System.out.println("Element is not enabled");
}

To Assert Text box editable, we can use isEnabled() function.
See below sample,
boolean anytextfield = driver.findElement(By.xpath("respectivexpath")).isEnabled();
Assert.assertEquals(anytextfield,true);

Related

How to determine if element attribute exists or not using TestCafe?

I'm using TestCafe and would like to determine if the checkbox element is present or not. In the HTML element, if the checkbox is already checked then the attribute checked exists otherwise not. How do I determine using TestCafe?
I used the function available in TestCafe - .hasAttribute('checked') but the return is undefined.
Here is the HTML code when the checkbox is checked:
<input class="jss1523" tabindex="-1" type="checkbox" data-indeterminate="false" value checked>
Here is the HTML code when the checkbox is unchecked:
<input class="jss1523" tabindex="-1" type="checkbox" data-indeterminate="false" value>
How do I solve this using TestCafe?
For every Dom element which you get with Selector() you can check property checked - https://devexpress.github.io/testcafe/documentation/test-api/selecting-page-elements/dom-node-state.html#members-specific-to-element-nodes
For checkboxes and radio inputs it returns Boolean value (true - if checked, otherwise - false) and for other types of elements return undefined

How to validate Check box if xpaths are same in case of selected and unselected

M unable to validate checkbox, if it's selected or not
because both the HTML are same
I tried isSelected(), but it's not working
Below is the HTML code for both selected and unselected
1) Selected
<label class="c-account-access-panel__checkbox " for="23336" data-js-checkbox-label="">
<input id="23336" class="c-account-access-panel__checkbox-input" type="checkbox"
data-label-for-value-missing="Please select at least one account from the options below" data-form-field-validation-on-grid=""
required="" checked="" data-js-checkbox="" value="DE29973399" name="payer"/>
<div class="c-account-access-panel__checkbox-symbol"/>
2) Unselected
<label class="c-account-access-panel__checkbox " for="23336" data-js-checkbox-label="">
<input id="23336" class="c-account-access-panel__checkbox-input" type="checkbox"
data-label-for-value-missing="Please select at least one account from the options below" data-form-field-validation-on-grid=""
required="" checked="" data-js-checkbox="" value="DE29973399" name="payer"/>
<div class="c-account-access-panel__checkbox-symbol"/>
Thanks in advance!
As per the Java Docs isSelected() method determines whether the element is selected or not. This operation only applies to <input> elements such as checkboxes, options within a <select> tag and radio buttons.
To validate if the desired checkbox is selected or not you can use the following code block:
boolean checkboxSelected = driver.findElement(By.xpath("//input[#class='c-account-access-panel__checkbox-input' and #name='payer']")).isSelected();
If isSelected() is not working for you. Then , you can use JavascriptExecutor to do your task. Following JS statements shall let you know the state of target checkbox.
document.getElementById("23336").click();
document.getElementById("23336").checked;
The checked method returns true or false depending on the checkbox state.
You can validate using getAttributemethod.
First select the webElement using any of the unique locator,
WebElement checkbox=driver.findElement(By.xpath(".//input[#type='checkbox']"));
If the checkbox is selected, then checkbox.getAttribute("checked")will give the result as true else, it will give the result as null. So, you can add the condition using checkbox.getAttribute("checked")
Use xpath expression like: (//div [#id='23336')[1] or (//div [#id='23336')[2] to make them into unique element then do .isselected ()

Jaws screen reader IE 11 select tag issue

I have some issues with IE 11 and Jaws. Please take a look at the example below.
Simplified code example:
<input type="text" />
<span role="alert" aria-live="assertive" id="err"></span>
<select id="colours">
<option value="White">White invalid</option>
<option value="Green">Green invalid</option>
<option value="Red">Red</option>
<option value="Blue">Blue</option>
</select>
var i = 0;
$('#colours').on('keyup', function (e) {
if ($(this).prop('selectedIndex') < 2) {
$('#err').html('an error has occurred ' + i++);
}
else {
$('#err').html('');
}
});
jsfiddle: https://jsfiddle.net/obwapffq/2/
Basically, I am validating the selected element in a dropdown list (in the example 'white' and 'green' values are invalid). I am only using the keyboard's up and down buttons to change the selected value. If an invalid option is selected, I update the content of a span element with the appropriate error message. The span element has role="alert" and aria-live="assertive". I have 2 issues:
If the selected option is invalid, the error message is read out, but the selected option is not read out and so the user does not know what option is invalid i.e. what option caused that error.
Sometimes even the valid options are not read out. This mostly happens with the first valid option in the list i.e. in the example 'Red'
This is working 100% correct with NVDA.
Any ideas?
Please add aria-label="yourOptionText" attribute to your option tag.
for e.g. aria-label="White Invalid".
Hope this will help.

TestStack.Seleno TickCheckbox not working

I have 2 forms that I am testing using TestStack.Seleno. Both forms have a checkbox that is mandatory. The first form has (including the checkbox) 5 fields. I can use TestStack.Seleno to create a passing test with valid data. I set the checkbox like this:
Input.TickCheckbox(f=>f.Accept,form.Accept);
On my other form which has 10 or so fields, when I try to set the checkbox to be ticked (using the same code) nothing happens. However when I try
var acceptCheckBox = Find.Element(By.Name("Accept"),new TimeSpan(0,0,0,50));
if (form.Accept)
{
acceptCheckBox.Click();
}
I get error "Element is not currently visible and so may not be interacted with"
Element is clearly visible and is not injected in using javascript.
I am using latest version of TestStack.Seleno from github.
Any ideas?
So I have managed to figure out what the issue is and have a work around, however I cannot explain why it works on the other form. The mandatory Accept field has html generated by mvc that looks like
<div>
<input class="check-box" data-val="true" data-val-mustbetrue="The Accept our field is required" data-val-required="The Accept our field is required." id="Accept" name="Accept" type="checkbox" value="true"><input name="Accept" type="hidden" value="false">
<label for="Accept">
Accept our Please accept our Terms and Conditions
</label>
<span class="field-validation-valid" data-valmsg-for="Accept" data-valmsg-replace="true"></span>
</div>
So you have the checkbox and hidden field both with id Accept, I suspect in code seleno is picking up the hidden field and setting value, so I updated my code todo
var acceptCheckBox = Find.Element(By.CssSelector("#Accept.check-box"));
acceptCheckBox.Click();
Now everything works.
Ismail

how to check radio button is checked or not? i am getting Unable to locate element: {"method":"id","selector":"700700139"} error in console

Here is the HTML code:
<input id="700690188" class="default_shipping_address"
type="radio" name="address" checked="true">
and i am trying with below code:
if(driver.findElement(By.id("700700139")).isSelected()){
System.out.println(driver.findElement(
By.xpath("//input[#id='700700139']/following-sibling::span[1]")).getText());
You are using a different id than the one on the page. You say the page shows an element with id 700690188 but you are querying for 700700139
Depending on the version/type of control will depend on the usage of the .isSelected If your input element is not working with the .isSelected for some reason...it should..., then you can check the attribute #checked for true or false. Personally I would pare these together in a function and call it to return a true/false. That way I can check any of them with the same code.
c#
if (IsSelected(driver.findElement(By.id("700690188")))
System.out.println(driver.findElement(By.xpath("//input[#id='700690188']/following-sibling::span[1]")).getText());
public bool IsSelected(IWebElement element)
{
try
{
return element.isSelected();
}
catch
{
if(element.GetAttribute("checked") == "true")
return true;
else
return false;
}
}
If neither of the above work then there is something funky with your input element.
Edit:
changed the ID in the example as Bryan is right, the webpage and your element id don't match.