Switch Case for Dropdown Selection Issue
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Switch Case for Dropdown Selection Issue
Hey all,
I'm working with a dropdown, where bladeParent is the category button, and bladeChild is a title within that category.
When you click bladeParent, then bladeChild will appear. If you click bladeParent again, it will close the dropdown. Pretty simple. In the case the dropdown is already open, you can click bladeChild right away and it will open the screen.
One thing I've noticed is that a common way for devs to hide elements on the screen is to make width and height 0.
so I came up with this method, and was working reliability until now. In the past I've had issues with .Exists and .VisibleOnScreen)
The issue: The very first time with a fresh browser session - the element navigate.bladeChild does not exist, and therefore has no width property yet, and then I get a prompt in my test saying "object doesn't exist" and fails.. it doesn't go to case false.
BUT if prior to test I click on the element once and and visually see it for a second, close the dropdown and then start my test, the routine runs perfectly everytime and it can go to either case true/false successfully with no issue at all.
My question is: Where is my logic wrong in this and what is the most robust/reliable solution for this scenerio? I know it's been asked a ton and I've tried many ways but there always seems to be a edge case and can't make it 100% reliable. Code is below. Many thanks community!
function NavBlade() {
Indicator.PushText("Navigating to Blade...");
let navigate = Aliases.browser.page.nav
let navbar = Aliases.browser.page.header
//Clear any open blades
Aliases.browser.page.header.button3.HoverMouse();
delay(200)
Aliases.browser.page.header.button3.Click();
delay(200)
//If bladeChild button is visible (not width=0) click it. If false then click bladeParent followed by the bladeChild button.
switch (!navigate.WaitAliasChild('bladeChild', 5000).Width == 0) {
case true:
Log.Message("case true width was not 0, click bladeChild")
delay(100);
navigate.bladeChild.HoverMouse();
delay(100);
navigate.bladeChild.Click();
Log.Checkpoint("Clicking bladeChild button...");
break
case false:
Log.Message("case false, width was 0")
delay(100);
navigate.bladeParent.HoverMouse();
delay(100);
navigate.bladeParent.Click();
delay(100);
navigate.bladeChild.HoverMouse();
delay(100);
navigate.bladeChild.Click();
Log.Checkpoint("Clicking bladeParent > bladeChild button...");
break;
}
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If it has no width property in the beginning, then width isn't = 0 and that would make it skip your false condition. You need a second check on that property that allows for it to be null or whatever will detect that non-zero condition.
Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the response!
Yes as I'm reading it makes sense now why width == 0 wouldn't work if not exists.
By second check to detect the non-zero condition.. how would you implement that?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I don't know your syntax so I'm guessing but it will be something like:
(!navigate.WaitAliasChild('bladeChild', 5000).Width == 0) OR (!navigate.WaitAliasChild('bladeChild', 5000).Exists)
You want to give the switch the two conditions to check at the beginning and either one is acceptable.
Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
OR or || are not valid options in the case you are suggesting.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Like I said, I don't know your syntax.
You want to be able to have either one of those conditions be true for the true part of the switch. If you can't embed it in the switch, then use an if statement to test the condition and set a variable to true/false from that and then use the variable for the switch.
Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ty for the reply I will give it a go
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have adjusted the code to include the following example
let navigate = Aliases.browser.page.nav
let navbar = Aliases.browser.page.header
if (!navigate.WaitAliasChild('carrier', 5000).Width == 0 || navigate.WaitAliasChild('carrier', 5000).Exists) {
Log.Message("carrier exists");
}
else {
Log.Message("carrier ");
}
}
Even in this case, the test case fails and stops because item does not exist.
The function will not go to else
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Right... because you're calling code in your IF clause for an object that might not exist yet. Your "Exists" check should happen first, before anything else, as anything else will fail if the object does not exist.
let navigate = Aliases.browser.page.nav let navbar = Aliases.browser.page.header if (navigate.WaitAliasChild('carrier', 5000).Exists){ if (navigate.WaitAliasChild('carrier', 5000).Width != 0) { Log.Message("carrier exists"); } else { Log.Message("carrier "); } }
Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----
Why automate? I do automated testing because there's only so much a human being can do and remain healthy. Sleep is a requirement. So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.
Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the assistance, Marsha, Robert!
@mgreen, was the issue resolved?
Tanya Yatskovskaya
SmartBear Community and Education Manager
