Created On: 17 July 2011 The methods GetFocus and HasFocus are not available on the Open Agent when testing Browsers. The code below shows you how to workaround this by using a JavaScript call to return the DomElement that has focus on a webpage. [-] testcase IsElementFocused() appstate none [ ] WebBrowser.SetActive() [ ] //Use the next line to set the focus on a particular DomElement if needed [ ] //WebBrowser.Find("//BrowserWindow//DomTextField[1]").SetFocus() [ ] Print(GetFocusedElementName()) [ ] Print(GetFocus()) [ ] [-] private string GetFocusedElementName() [ ] Window browserWindow [ ] browserWindow = WebBrowser.Find( "// BrowserWindow " ) [ ] browserWindow.ExecuteJavaScript( " document.documentElement.focused = document.activeElement.name " ) [ ] string value [-] do [ ] value = browserWindow.Find( "//HTML" ).GetProperty( "focused" ) [-] except [ ] value = "" [ ] return value [ ] [-] private Window GetFocus() [ ] Window browserWindow [ ] browserWindow = WebBrowser.Find( "//BrowserWindow" ) [ ] Window object [-] do [ ] string value [ ] value = browserWindow.Find( "//HTML" ).GetProperty( "focused" ) [ ] object = browserWindow.Find( "//DomElement[@name='"+GetFocusedElementName()+"']" ) [-] except [ ] object = null [ ] return object The GetFocusedElementName() function name provides the value of the property name of the focused element on the page. If no element has the focus the returned value is a empty string. The GetFocus() function in this script should have nearly the same behavior as the Classic Agent. The only precondition is that a name property is set which identifies the focused object. Instead of the name you can choose any other property if it is better for the uniqueness of the object. Incident #2526054 Old KB# 34257
↧