Custom Wait - .Net driver

151 views
Skip to first unread message

SeleniuMaddog

unread,
Jun 4, 2011, 3:51:38 AM6/4/11
to webdriver
Hi,

I really like the custom waiting functionality, it's really cool and
allows really flexible scenarios.
I understand that it has some Exception capturing abilities, can
anyone expand on that? What does it catch?

I don't understand how to pass my own interval for the timer. Why does
it need IClock and all those other things? Can someone please give an
example of how to use it? Why isn't there just another Timespan
parameter for interval?

Thank you

Jim Evans

unread,
Jun 4, 2011, 8:19:41 AM6/4/11
to webdriver
Basically, by default, the Until<T>() method catches
NotFoundExceptions. This would include NoSuchElementException,
NoSuchWindowException, and NoSuchFrameException. That's usually enough
for most cases. If not, you can always subclass WebDriverWait, or
implement your own wait class that implements IWait<T>.

As for creating a custom interval, you can pass in a SystemClock,
which implements IClock. To wit:

// the following creates a wait that polls every 100 milliseconds
// and times out after 5 seconds.
WebDriverWait wait = new WebDriverWait(new SystemClock(), driver,
TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(100));

Being the open source project that it is, you can always examine the
source code for the library yourself. See http://code.google.com/p/selenium/source/checkout
for instructions on how to check out the code using Subversion. You
can also browse the source online. The WebDriverWait source can be
found at http://selenium.googlecode.com/svn/trunk/dotnet/src/WebDriver.Support/UI/WebDriverWait.cs

Regards,
--Jim

SeleniuMaddog

unread,
Jun 4, 2011, 5:22:28 PM6/4/11
to webdriver
Ah.. I looked at the code, I just didn't know what I should put as
IClock - didn't know it was that simple, thank you :)

I've read how the waiter handles exceptions. I wanted to write
something that waits for iframes to load (the driver doesn't - it just
waits for the main page) - but since I would need to capture a wider
array of exceptions, I'll have to use my own Until method...

Thank you

On Jun 4, 8:19 am, Jim Evans <james.h.evans...@gmail.com> wrote:
> Basically, by default, the Until<T>() method catches
> NotFoundExceptions. This would include NoSuchElementException,
> NoSuchWindowException, and NoSuchFrameException. That's usually enough
> for most cases. If not, you can always subclass WebDriverWait, or
> implement your own wait class that implements IWait<T>.
>
> As for creating a custom interval, you can pass in a SystemClock,
> which implements IClock. To wit:
>
> // the following creates a wait that polls every 100 milliseconds
> // and times out after 5 seconds.
> WebDriverWait wait = new WebDriverWait(new SystemClock(), driver,
> TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(100));
>
> Being the open source project that it is, you can always examine the
> source code for the library yourself. Seehttp://code.google.com/p/selenium/source/checkout
> for instructions on how to check out the code using Subversion. You
> can also browse the source online. The WebDriverWait source can be
> found athttp://selenium.googlecode.com/svn/trunk/dotnet/src/WebDriver.Support...

Adrian Longley

unread,
Jun 4, 2011, 6:48:29 PM6/4/11
to webd...@googlegroups.com, webdriver
On 4 Jun 2011, at 22:22, SeleniuMaddog <madd0g...@gmail.com> wrote:

Ah.. I looked at the code, I just didn't know what I should put as
IClock - didn't know it was that simple, thank you :)

I've read how the waiter handles exceptions. I wanted to write
something that waits for iframes to load (the driver doesn't - it just
waits for the main page) - but since I would need to capture a wider
array of exceptions, I'll have to use my own Until method...

I'm my opinion this is inevitable, especially if you want to allow some flexibility in the UI implementation. You never know why you might get a driver exception, on sites with lots of async js particularly. 

You might be interested in this video of a short presentation I gave at the london .net user group at Skills Matter in London last week, of Coypu -- the project I have started recently to try and tackle exactly these kinds of problems (I did post about it a few weeks back).

Here's the video:

http://skillsmatter.com/podcast/home/london-dot-net-user-group-may/js-1817

About 10 mins in I demo three examples of tests that would have required fiddly waits / exception handling to work in raw WebDriver but are handled seamlessly by Coypu, enabling you to concentrate on writing clean, simple test code.

Would love to hear any feedback.
Adrian

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

dan hirsch

unread,
Jun 5, 2011, 1:05:40 AM6/5/11
to webd...@googlegroups.com
for what you want, try to iterate over all exiting frames in the age (recursively of course) and for each frame do driver.switchTo().frame() and run the logic waiting for it to finish loading.

I am not sure if this is what you are looking for but form your last post it sounds like it :)
hope it helps.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.




--
regards,
Dan Hirsch

Daniel Wagner-Hall

unread,
Jun 5, 2011, 8:06:50 AM6/5/11
to webd...@googlegroups.com
On 4 June 2011 22:22, SeleniuMaddog <madd0g...@gmail.com> wrote:
> I've read how the waiter handles exceptions. I wanted to write
> something that waits for iframes to load (the driver doesn't - it just
> waits for the main page)

The contract WebDriver claim to uphold is to wait until all frames are
loaded, I would expect this to include iframes. Could you file a bug
at https://code.google.com/p/selenium/issues/entry with a minimal test
case, and details of which browser(s)/platform(s) this fails on?
Thanks

SeleniuMaddog

unread,
Jun 6, 2011, 12:44:42 AM6/6/11
to webdriver
Wow, so many of responses over the weekend :-)

@DanielW - Thank you, I'll try to find the exact scenario in which it
happens, I do believe the failures I'm currently experiencing involve
dynamically created nested iframes, some created in linear JS code
(i.e. are on the page or created with document.write), but some are
created asynchronously (but also during page load, like with
appendChild). I want to write the waiting code because I get
intermittent errors like "no longer attached to the DOM" and "element
not found" (maybe others, I don't recall) when iterating through
nested iframes.

I don't know how WebDriver is waiting for things to load - but would
it wait for things like an iframe created with "appendChild" by a
javascript marked defer=true that is called during page-load inside
another iframe? It's just a crazy scenario - but I've seen script's
marked defer=true, it might possibly be the case. Will WebDriver
actually try to handle such a weird case? I'd imagine it's really hard
to "catch".


@danh - yep, that's exactly how I want to do it, I'm just not sure how
to check if the iframes have finished loading - I thought of just
running it until I get no exceptions of the type I know are associated
with frame-access failures - but is that the answer? Maybe it's still
loading something but is already accessible.


@Adrian - thanks, I've seen the presentation, impressive stuff, funny
how you call webdriver "raw", it's one of the least raw tools I found
- it even doesn't let you interact with an element if it's hidden,
that's like really user-friendly (the only feature I really don't like
in WebDriver). Can coypu deal with a scenario like the one I described
above? If I'm not looking for a specific iframe, but iterating all of
them - how does it (technically) handle the problem of still-loading
iframes during page load?

Funny how it's a word - south american rodent, I totally expected a
red underline under Coypu :)


Thank you!

On Jun 5, 8:06 am, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> On 4 June 2011 22:22, SeleniuMaddog <madd0g.co...@gmail.com> wrote:
>
> > I've read how the waiter handles exceptions. I wanted to write
> > something that waits for iframes to load (the driver doesn't - it just
> > waits for the main page)
>
> The contract WebDriver claim to uphold is to wait until all frames are
> loaded, I would expect this to include iframes.  Could you file a bug
> athttps://code.google.com/p/selenium/issues/entrywith a minimal test

Adrian Longley

unread,
Jun 7, 2011, 3:34:37 AM6/7/11
to webd...@googlegroups.com

Well frames aren't implemented yet but I'll take a look at them next.

However the Coypu approach will be to handle any iteration necessary to find the frame you need in the Coypu driver then wrap that in the same retry-until-timeout strategy as everything else. So you would just call FindFrame(locator) or WithinFrame(locator,lambda) as for sections and fieldsets that are already implemented.

Does that sound like it would work for you. What properties of the frame are you using to locate it in your iteration?

Adrian

> Funny how it's a word - south american rodent, I totally expected a
> red underline under Coypu :)
>
>
> Thank you!
>
> On Jun 5, 8:06 am, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
>> On 4 June 2011 22:22, SeleniuMaddog <madd0g.co...@gmail.com> wrote:
>>
>>> I've read how the waiter handles exceptions. I wanted to write
>>> something that waits for iframes to load (the driver doesn't - it just
>>> waits for the main page)
>>
>> The contract WebDriver claim to uphold is to wait until all frames are
>> loaded, I would expect this to include iframes. Could you file a bug
>> athttps://code.google.com/p/selenium/issues/entrywith a minimal test
>> case, and details of which browser(s)/platform(s) this fails on?
>> Thanks
>

Reply all
Reply to author
Forward
0 new messages