Page 1 of 1

Host.Local.FindSingle Slow (Appro 7 seconds to find an item)

Posted: Thu Jun 07, 2018 10:11 am
by Gunner
Hi,

I am attempting to test a web application, that uses select2 on its dropdown lists.

What it does is replace a standard html <select> element with a prettier version with a search bar, and hides the original select.

I have written the following users code to select and item from one of these boxes:
[UserCodeCollection]
    public class Select2
    {
        /// <summary>
        /// Select an item from select2 dropdown
        /// </summary>
        [UserCodeMethod]
        public static void SelectItem(string itemToSelect, RepoItemInfo theItem)
        {
        	WebElement web;
        	theItem.Exists(out web);        	
        	web.Click();        	

			var dropdown = Host.Local.FindSingle<UlTag>(".//ul[@class='select2-results__options']");
			var options = dropdown.FindDescendants<LiTag>();
			
			var found = false;	
				
			foreach(var opt in options){				
				if(opt.InnerText == itemToSelect){
					opt.Click();
					found = true;
					break;
				}
			}
			
			if(found == false){
				throw new InvalidOperationException("Select 2 option not found");
			}
        }
    }
The above function works but is slow, as the line:
var dropdown = Host.Local.FindSingle<UlTag>(".//ul[@class='select2-results__options']");
Takes 7 seconds to run. I've also noticed it breaks if I have a select 2 box open in a different browser, so it appears this line is trying to find a ui tag across all of the applications open on my host machine, so I can see why this would be inefficient.

Is there a way to get it just to search the page with the WebElement i have a reference to on?

Just a note on the internals of select2, I cannot use Child/Sibling on the web element as the 'select2-results__options' is not near the item in the dom as if there are multiple select2s on a page there will be only one 'select2-results__options' which has it's contents replaced with that of the open list.

Thanks for any help,

Ian.

Re: Host.Local.FindSingle Slow (Appro 7 seconds to find an item)

Posted: Thu Jun 07, 2018 11:21 am
by odklizec
Hi,

In my opinion, the only way how to speed up the search is to make the xpath more precise, start search from nearest parent element or, at very least, start search from the DOM element. Try for example something like this:

Code: Select all

WebDocument webDoc = "/dom[@domain='domain_name']";  
var dropdown = webDoc.FindSingle<UlTag>(".//ul[@class='select2-results__options']");