Question Firing a keyDown or keyUp event on an element in a WebBrowser control

AutomationGuy

Member
Joined
Feb 17, 2010
Messages
5
Programming Experience
5-10
Hi all

I'm trying to do some basic browser automation with one of our web applications, just to help with some system monitoring.

The task is fairly simple, open a page, complete two fields, click a button and check some page content, and for the most part I've done what I need to do, however I've run into a problem...

VB.NET:
wb.Navigate(sPageUrl)
WaitForPageLoad() 'custom method to wait until the page has fully loaded

wb.Document.GetElementById("houseNo").SetAttribute("value", sHouseNo) ' enter text
wb.Document.GetElementById("postCode").SetAttribute("value", sPostCode) ' enter text
wb.Document.GetElementById("submit-search").InvokeMember("Click") ' click button

... the code works, and does all the interactions - the problem is that when it click the search button at the end, the web-page is giving me an error saying that I've not entered a value into one of the text fields (even though I have, and I can see the value in the text box).

I think the problem is that the web page fires some javascript on the back of the "keyUp" or "keyDown" event on the text box, and because of the way the browser automation works, that "keyUp" or "keyDown" event isn't actually being fired... hence why the browser seems to think that the text box is empty.

So the question from me is, does anyone know of a way to force those events to fire?

I was thinking something along the lines of this, but that doesn't work...

wb.Document.GetElementById("submit-search").InvokeMember("keyDown")

Perhaps I'm firing the wrong event, or firing it incorrectly... or both??

Thanks
 
Managed to get around this by doing a SendKeys instead...

VB.NET:
wb.Document.GetElementById("postCode").Focus()
SendKeys.Send(sPostCode)


... not quite what I had in mind, but I think it will suffice.
 
I see this often while testing various object with TestComplete... It mostly happens with bound controls, where changing the .Value property does not actually affect the data read by the application. Using SendKeys actually validates the data like it would if you would type it and is often the only way to achieve what you are doing. For example if a TextBox has a TextChanged handler which copies the content of the textbox to a private variable, which is in turn used in the query sent to the server, directly changing the .Value property doesn't trigger the event and the private variable remains in an undefined state.
 
Last edited:
Back
Top