Background:
Been using Selenium 2.0 (WebDriver), and understand that the focus moves to elements when locating them, like so:
var element = driver.FindElement(By.CssSelector("#foo"));
element.Click();
I also know we can do powerful things if jQuery is on the page under test (way cool):
IJavaScriptExecutor scriptExecutor = ((IJavascriptExecutor)driver);
scriptExecutor.ExecuteScript("$('#foo')[0].click();";
IWebElement hiddenElement = scriptExecutor.ExecuteScript("return $('#my_dynamic_element')[0]");
hiddenElement.Click; // found ya!
Question:
Does Selenium’s WebDriver (specifically .NET flavor) fire a focus while it’s converting the .ExecuteScript() result into IWebElement?
I do not believe so, and here is a test I setup to test this:
I created the following html page that contains a script that will change the class name when an element get focus:
When the field gets focus, it will change the class name.
Then, I can run the following WebDriver test that retrieves an element using the IJavaScriptExecutor, and then check to see if the class name was changed to indicate that the element was focused:
The assert fails, which tells me that focus was not set.