DEnumber50 DEnumber50 - 3 months ago 15
C# Question

Selenium C#: Assert verifying if text field is uneditable

I am trying to write a script to assert if the text field in question isn't editable. I was attempting to write a verification hidden = "true", because that is what ultimately is hiding the editable text box. How would I be able to verify something like this? and is this the way I should be handling such a verification? Thanks for your responses in advance.

Here is the html

<div id="ctl00_contentMain_sponsorInfo">
<fieldset>
<legend>
Referral Information
</legend>
<ol>
<li>
The name of your referrer is Nameā€¦
</li>
<li>
<input id="ctl00_contentMain_sponsorUsername" class="textBig" type="text" hidden="true" style="width:270px;" req="false" max="30" value="Name" name="ctl00$contentMain$sponsorUsername"></input>
</li>
</ol>
</fieldset>
</div>

Answer

As you mentioned the best way to test this scenario is to see if the hidden attribute has a value true

IWebElement element = Driver.FindElement(By.Name("ctl00$contentMain$sponsorUsername"));
if (element.GetAttribute("hidden").Contains("true"))
{
    Assert.Pass("Your message");
}

Note: You do have to make sure the element exist. You can use some explicit wait with something like

var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("something ")));

in order to grab the attribute. This case Selenium will work just fine since you are not interacting with the element.