I am having trouble verifying the current state of a switch ('on' or 'off') using selenium. The toggle appears in the 'on' position when the webpage loads, but it affects filter results in a table when switched off. I am unable to successfully retrieve its current state with my selenium tests.
- element.Selected; always returns false regardless of the toggle position
- element.Enabled; always returns true regardless of the toggle position
- string test = myToggle.GetAttribute("class"); returns the text "slider round" which is not helpful
- string test1 = myToggle.GetAttribute("checked"); is null
- string test2 = myToggle.GetAttribute("value"); is null
Below is the code snippet for the toggle switch:
<label class="switch">
<input type="checkbox" id="myToggle" checked />
<span class="slider round"></span>
</label>
The following code checks the toggle position and loads table data accordingly:
if (Amt == currentAmt ) {
if ($("#myToggle").prop("checked") == true) {
loadMyProducts('NAN', $("#total").val());
}
Here is an example of my current test setup:
[FindsBy(How = How.XPath, Using = "//*[@id='divTestAcc']/div[1]/p[9]/label/span")] public IWebElement myToggle { get; set; } public Boolean CheckMyToogleIsOnorOFF() { string test = myToggle.GetAttribute("class"); Console.Write("checking "+ test); // string test1 = myToggle.GetAttribute("checked"); //string test2 = myToggle.GetProperty("checked"); // .Selected always returns false // .Enables always returns true Boolean result; result = myToggle.Enabled; bool result1 = myToggle.Selected; if (result == true) { Console.WriteLine("My toggle is enabled"); } else { Console.WriteLine("My toggle is not enabled"); } return result; }