Select controls, and IE, Chrome, and Firefox

Ask general questions here.
User avatar
Aracknid
Posts: 388
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Select controls, and IE, Chrome, and Firefox

Post by Aracknid » Tue Jul 24, 2012 7:22 pm

Hi,

I know this has been asked many times, and I've read many things that have been helpful, but I'm really having a tough time with select controls.

I finally read a post that said IE was different than FF and Chrome. That posting was helpful.

So I wrote 2 pieces of code. If IE, use ListItems in the Container that IE creates, and if the other browsers use Options in the Select control.

The problem I'm now having is that while this is working great for IE (ver 9) and FF (ver 14), Chrome (ver 20) is being difficult. Basically, in the section were I check it the Option I intend to click is visible or not, it always returns false (not visible) even though it is. Here's my code...
'This is part of a greater function that handles all types of controls.
'Many of the variables are defined at the top of this function, and so you will
'see them defined here. Hopefully it is clear.

'These first 5 lines find and click on the select control
MySelectTag = Adapter.Create(Of Ranorex.SelectTag)(MyControl.Element)
ControlsSession.mo_BrowserWebDoc.EnsureVisible()
MySelectTag.EnsureVisible()
MySelectTag.Click()
MySelectTag.UseEnsureVisible = False ' This is important, or the control will jump around like crazy...and close itself while doing other things...

'BrowserType is my own ENUM of browser types
'ge_BrowserType is what keeps track of the browser I'm currently using
If ge_BrowserType <> BrowserType.IE Then

	'This code should work with FireFox and Chrome
	For Each MyOption In MySelectTag.Options
	
		MyOption.UseEnsureVisible = False
		'sValue is what I passed into the function to find in the select control
		If MyOption.InnerText = sValue Then
			MySelectTag.PressKeys("{Home}")
			Ranorex.Delay.Milliseconds(250)
			
			'This is where it fails for Chrome (but works for FireFox). MyOption is always NOT visible in Chrome, even though I have scrolled it into view (if it wasn't already) by pressing pagedown
			Do While Not MyOption.Visible
				MySelectTag.PressKeys("{PageDown}")
				Ranorex.Delay.Milliseconds(250)
			Loop
			MyOption.Click()
			bFound = True
			Exit For
		End If
	Next
	
Else

	'This code works with IE
	
	Dim c As Ranorex.Container = Nothing
	Dim l As Ranorex.ListItem = Nothing
	Dim u As Ranorex.Unknown = Nothing

	c = "/container[@caption='selectbox']"
	For Each u In c.Children
	
		If u.Element.PreferredCapability.Name.ToLower = "listitem" Then
			l = Adapter.Create(Of Ranorex.ListItem)(u.Element)
			'sValue is what I passed into the function to find in the select control
			If l.Text = sValue Then
				MySelectTag.PressKeys("{Home}")
				Ranorex.Delay.Milliseconds(250)
				Do While Not l.Visible
					MySelectTag.PressKeys("{PageDown}")
					Ranorex.Delay.Milliseconds(250)
				Loop
				l.Click()
				bFound = True
				Exit For
			End If
		End If
		
	Next
End If
Any thoughts?

Oh, and by the way, this code now means I no longer need to execute a script because when the click occurs, it fires the event, and if a message box appears it doesn't block me... see my previous post http://www.ranorex.com/forum/post14937. ... 5dc#p14937

Thanks,

Aracknid

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Select controls, and IE, Chrome, and Firefox

Post by Support Team » Wed Jul 25, 2012 9:33 am

Hi Aracknid,

Yeah, those select controls quite struggle if you want them to be clicked, selected or what else...
Take them with the key as you did is a smart opportunity for handling them.
This is how I did it. And I was really surprised as this one went well in IE, FF and Chrome.
Hope you have as much luck as I had.
var someSelectTag = webRepo.TryitEditorV15.SomeSelectTag;
			someSelectTag.Click();
//moving up the "selection" to the upmost entry
			someSelectTag.PressKeys("{PageUp}");
			IList<OptionTag> opTag= someSelectTag.Options;
			int i=0;
			while(opTag.InnerText!="YourOptionTag")
			{
				i++;
				someSelectTag.PressKeys("{Down}");
//just to see whats happening
				Ranorex.Delay.Milliseconds(250);
			}

			someSelectTag.PressKeys("{Enter}");


Kind Regards,
Larissa
Ranorex Support Team

User avatar
Aracknid
Posts: 388
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Select controls, and IE, Chrome, and Firefox

Post by Aracknid » Wed Jul 25, 2012 1:53 pm

I'd prefer to not have to use your provided example because it is too slow when the lists are large. I have some lists that have over 100 items in them.

Edit: Just as an example, all our date pickers use a select control for the year, and the year has dates from 1900 to 2090 (191 items). When I want to pick a date in today's close proximity, it has to press down 112 times which makes setting a date a real pain.)

More to the point, why does the code I have NOT work for Chrome? Is there a bug in Ranorex? It works fine for FireFox.

Aracknid

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Select controls, and IE, Chrome, and Firefox

Post by Support Team » Thu Jul 26, 2012 11:42 am

Hi,

The reason why your code is not working in Chrome is because the object elements are never "Visible". Same in IE (see Spy). FF sets the objects as visible. This is no bug of Ranorex but just browser-specific options.

My example presents a slow solution, that's right, but it was more for understanding (step by step).

In my opinion you should skip all those visibility settings - they are not necessary.
If you want to select your tag fast, you can do the following:
var someSelectTag = webRepo.TryitEditorV15.SomeSelectTag;
someSelectTag.Click();
someSelectTag.PressKeys(sValue);
someSelectTag.PressKeys("{Enter}");
This should work browser independent and quick.

Regards,
Larissa
Ranorex Support Team

User avatar
Aracknid
Posts: 388
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Select controls, and IE, Chrome, and Firefox

Post by Aracknid » Thu Jul 26, 2012 3:45 pm

Thanks, that actually is a much better solution in general for all browsers.

Although I do have a related question. Select controls can be "combo box" style or "list box" style. Some list boxes allow you to select multiple items. Do you have a solution for those? For a list box , typing in the text to jump to what you want and hitting enter deselects the previous choice (if you are trying to set multiple ones).

Thanks for your help so far.

Aracknid.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Select controls, and IE, Chrome, and Firefox

Post by Support Team » Fri Jul 27, 2012 11:28 am

Hi,

I like to be challenged, so no worries, keep on asking. :wink:
I guess you mean select tags with attribute multiple? In this case it would be wise using selected.
For example:
var someSelectTag = webRepo.TryitEditorV15.SomeSelectTag;
IList<OptionTag> opTagList= someSelectTag.Options;
			foreach(OptionTag opTag in opTagList){
				if(opTag.Value=="MySelect1")
					opTag.Selected=true;
				if(opTag.Value=="MySelect2")
					opTag.Selected=true;
			}
This is also browser-independent.
Would this be sufficient for your requirements? Have you onchange events or something like that on those list boxes too?

Kind Regards,
Larissa
Ranorex Support Team

User avatar
Aracknid
Posts: 388
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Select controls, and IE, Chrome, and Firefox

Post by Aracknid » Fri Jul 27, 2012 2:18 pm

Yeah, your example only works if there are no events that require firing. There might be events related to onchange or onclick for some controls. I cannot know for sure for every one until I write code to deal with it. Our application is huge and has close to 1000 ASP/ASPX pages, each filled with various controls.

Basically, I have a framework that handles all the controls in my app (which is constantly being modified as we run into unexpected results either to do with the control itself or the browser we are running on). I try to make everything as generic as possible and have it handle all situations. Sometimes I guess that it is not possible and need to have special cases to handle them.

Aracknid