|
Some events such as Button Clicking are passed to automation code before the application gets the event. These events are named with the suffix ing such as Clicking and RightClicking.
All of these events can either be handled and passed on to the application or they can be handled but not passed on to the application. When an event is not passed to the application, we describe it as being canceled.
Cancellable events all receive an argument of type CanceEventArgs which has a property of Cancel. To prevent an event from being given to the associated application, set Cancel to true and then handle the event however is needed.
In the attached solution, a Winform dialog has a text box and is integrated with Google’s search page. If the dialog has the same text in it as the Google search textbox, then the search is cancelled.
protected void OnSearchClicking( Object sender, CancelEventArgs e ) { // If search text is same as on form, cancel event. if ( txtCancel.Text == m_Google.q.Text ) { // Change the Google search text to something else. m_Google.q.Text = “I don’t feel like it”; // This will cancel the click and the web page won’t see it e.Cancel = true; } }
To test this, run the attached solution. Enter text into the dialog box and the same text into Google and press the Google Search button.
One additional note about this exercise, it works both for the Google search home page and for the search results page. There are many ways to do this, but in this example it is done using match rules that include both search pages.
The match rule for the webpage has the text match rule for the URL and for the window title changed they will match either screen. The match rule for the search form is a bit trickier. In this case, the default Form Name Match Rule won’t work because the form on the home page and the one on the results page use different names. Instead the Control Children Match Rule and Element Inner Text Match Rule are used where the inner text is checked to see if it contains the words, “Advanced Search”.
|