Ask a Question

Switch Case for Dropdown Selection Issue

mgreen
Contributor

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;
}
}

10 REPLIES 10

Hi,

 

> if (!navigate.WaitAliasChild('carrier', 5000).Width == 0 || navigate.WaitAliasChild('carrier', 5000).Exists)

> because you're calling code in your IF clause for an object that might not exist yet.

If you don't like nested conditions, you may use the fact that JScript uses short circuit conditionals. The code in this case may look like this:

if ((navigate.WaitAliasChild('carrier', 5000).Exists) && (navigate.carrier.Width > 0))

  // sucess

else

  // failure

 

Regards,
  /Alex [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
================================
cancel
Showing results for 
Search instead for 
Did you mean: