Webdriver - how can I assert that an attribute exists
I am trying to assert that a certain "id" contains a "hidden" attribute. The hidden attribute will never matter. It will exist if I clicked on a specific button, and it won't exist unless I clicked on a specific button. Here is the code:
<div id="Callback-time" hidden="">
I tried to get the attribute descriptor as follows, but I am not getting anything:
IwebElement CallBackTime = driver.FindElement(By.Id("Callback-time");
String Value = CallBackTime.GetAttribute("hidden");
System.Diagnostics.Debug.WriteLine(Value);
+3
source to share
1 answer
Two conditions to check:
- The button was not pressed and the attribute does not exist
<div id="Callback-time">
- the button is pressed and the attribute exists
<div id="Callback-time" hidden="">
You can extract the html for an element as a string and do some basic substring validation to tell the difference
IwebElement CallBackTime = driver.FindElement(By.Id("Callback-time");
String Value = CallBackTime.GetAttribute("innerHTML");
Assert.AreEqual(Value.Contains("hidden=\"\""), true);
+4
source to share