cancel
Showing results for 
Search instead for 
Did you mean: 

Switch Case for Dropdown Selection Issue

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
Community Hero

Re: Switch Case for Dropdown Selection Issue

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.

 

 

Contributor

Re: Switch Case for Dropdown Selection Issue

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? 

 

 

Highlighted
Community Hero

Re: Switch Case for Dropdown Selection Issue

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.

Contributor

Re: Switch Case for Dropdown Selection Issue

OR or || are not valid options in the case you are suggesting. 

Community Hero

Re: Switch Case for Dropdown Selection Issue

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.

Contributor

Re: Switch Case for Dropdown Selection Issue

Ty for the reply I will give it a go

Contributor

Re: Switch Case for Dropdown Selection Issue

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

Community Hero

Re: Switch Case for Dropdown Selection Issue

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
[Community Expert Group]
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
Extensions available
Community Manager

Re: Switch Case for Dropdown Selection Issue

Thanks for the assistance, Marsha, Robert!

 

@mgreen, was the issue resolved?

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Here?
Join us and watch the welcome video:
6 / 7 API Testing Mistakes Video
Top Kudoed Authors