I want to validate/check the state of a button switch element and if it different from the inputed argument it will click the button: button
<button id="form-user_status" type="button" role="switch" aria-checked="true" xpath="1">
But it seem i cant get it right. i tried a few method :
- Check 'aria-checked' attribute value and compare it to inputed string argument
public void SetFormStatus(string data)
{
if (driver.FindElement(FormStatus).GetAttribute("aria-checked") != data)
{
driver.FindElement(FormStatus).Click();
}
}
but the problem is aria-checked value is changing from true/false
when in creating new form item to 0/1
when viewing detail item.
- Using
driver.FindElement(FormStatus).Selected
and compare it to inputed bool argument but.Selected
return wrong, even when button active it return false.
For now i used brute method to work around it :
public void SetFormStatus(bool data)
{
bool currentStatus;
if (driver.FindElement(FormStatus).GetAttribute("aria-checked") == "true" || driver.FindElement(FormStatus).GetAttribute("aria-checked") == "1")
{
currentStatus = true;
}
else
{
currentStatus = false;
}
if (currentStatus != data)
{
driver.FindElement(FormStatus).Click();
}
}
It work for now but is there other legit way to check this kinda button? Thanks in advance!
CodePudding user response:
well, coming from my JavaScript/CSS background.
You Need To check based on the class of the element
(element = your switch button).
Generally UI Frameworks like Bootstrap
,(The one you are using I believe).
when its checked it adds the class ant-switch-checked
at the class attribute.
so I recommend that you check if the button has the class ant-switch-checked
then it'll be 100% clicked.