0

I am working with a HTML select (i.e. dropdown) in a web page that initially loads with no value. It loads the values only when an onfocus JS event is executed on it. Note that it is a GWT control.

Here is the DOM rendering of the dropdown. Note the onfocus event that actually loads the values in the dropdown:

<select id="Field1" class="select not_disabled" onchange="clearOperators(Operator1); addOperators(Field1,Operator1); adjustInput(Field1, 1);" onfocus="loadValues(Field1);" style="width:20em;" name="Field1">
</select>

The problem is that the click on the element is not getting registered unless the window is in focus. And hence the values are not loading.

I have this in my code, and this executes with no effect:

// bring the focus on the dropdown
((JavascriptExecutor) webDriver).executeScript("return document.getElementById('Field1').focus;", dropdownElement);

// click on it to load the values
dropdownElement.click();

Even tried the old way of firing an event on the element. Did not work:

JavascriptLibrary javascript = new JavascriptLibrary();

javascript.callEmbeddedSelenium(webDriver, "triggerEvent", dropdownElement, "blur");

I also tried out some of these suggestions, but none helped:

Correct way to focus an element in Selenium WebDriver using Java

https://groups.google.com/forum/#!msg/selenium-users/jk59XG2TQk8/xbaskp9uFnQJ

Could someone suggest what I can do to load the values in the dropdown? I am using Java with Selenium 2 on Firefox 47.

Community
  • 1
  • 1
techjourneyman
  • 1,701
  • 3
  • 33
  • 53

1 Answers1

0

I found a way to get this working. Although it's not a very desirable solution for UI tests, since it does not mimic a "real user" behavior, it does work fine.

I am invoking the onfocus function from within my test class and that does the loading of values.

(String) ((JavascriptExecutor) webDriver).executeScript("return loadValues(Field1);", dropdownElement);

Further, I added this preference to the Firefox profile:

profile.setPreference("focusmanager.testmode",true);

Hope it helps someone.

techjourneyman
  • 1,701
  • 3
  • 33
  • 53