Wednesday, May 5, 2010

Handling web pages that navigate back to themselves

by Damon Lockwood May 5, 2010

I recently worked through a customer issue that revolved around a page that reloaded itself when a form was submitted. This is a pretty common scenario, but it can be a little tricky to handle in OpenSpan, especially if the reloaded page doesn’t actual…

Read the full article →

Handling web pages that navigate back to themselves

by Damon Lockwood May 5, 2010

I recently worked through a customer issue that revolved around a page that reloaded itself when a form was submitted. This is a pretty common scenario, but it can be a little tricky to handle in OpenSpan, especially if the reloaded page doesn’t actually create any new elements to wait upon. The trick is to wait for the Destroyed event to fire on the web page after you call the method that triggers the reload. This is pretty easy to do in an automation by using the “Wait for this event” feature.

First, drag the Destroyed event from the page onto the automation surface. Right click and select for “Wait for this event”. The event will now transform into an object you can wait for.


Selecting the “Wait for this event” command
Next, connect the Setup port to the method that will trigger the reload, in this case the PerformClick method.


The right way to trigger the event.

Note, you must use the Setup port to ensure that OpenSpan is ready to wait for the event when you trigger it. If you call PerformClick before you enter the event block there is always the chance that the Destroyed event will fire between the two blocks in which case the event will be missed.


The wrong way to trigger the event.

Finally, connect the Fired port to the blocks that should execute after the page has reloaded.


The complete solution.

As you can see, waiting for an event is very easy in an automation, but how would you accomplish the same thing in code? You could spin your own eventing mechanism, but this would be a fair amount of work and require a good knowledge of threading. Fortunately there is another way to access the wait for event functionality. Every OpenSpan control supports a GetEvent method which takes a special EventName object. This method actually pre-dates the automation “Wait for this event” functionality. GetEvent takes a parameter of type EventName which is simply a wrapper around the name of the event. GetEvent returns an Event object which exposes a Wait method. Events objects are managed internally by the control so you do not have to worry about disposing the object. The following code snippet illustrates how to use GetEvent.

public void DoSearch(){ // Get event Event waitForDestroy = mSample.EventSamplePage.GetEvent(new EventName("Destroyed"));

 // Perform click mSample.Next.PerformClick();

 // Wait for event if (waitForDestroy.WaitForEvent(10000)) {  // Next steps } else {  // Handle error }}
Read the full article →