Monday, June 21, 2010

Selenium wait()

Selenium wait() not defined ? What will happen ?

Selenium uses the browser to make requests and process the server response. In short, selenium asks for a page and server sends back the response.
The browser then constructs a DOM tree out of the response. Selenium then parses this DOM tree and looks for elements as per the test statements.

Short example
Step 1 --> @selenium.open("www.google.com")
Step 2 --> @selenium.wait()
Step 3--> @selenium.is_text_present("Google ...")

Here the request is Step 1.

Now assuming that the server is not loaded at all, the network latency is very minimal and the client machine has a very fast processor, the time from the first request to receiving the response will STILL be atleast a few miliseconds. So not considering the network latency and delayed response from the server, some time is definitely required for the browser to generate a DOM tree out of the response and then ready the page to be rendered.

If we do not allow for a wait period, however small it maybe due to ideal network and server load conditions, then selenium will jump from step 1 to step 3. At this point, the DOM tree is not ready and selenium will NOT find the text "Google ..." in the DOM tree. Thus, our test will fail.

For any test environments, the server response times could be large and selenium has to wait for the response to be completely consumed by the browser and build a DOM tree. Even if we were to make the test environments extremely fast and run at almost no load, there will be some amount of time required for DOM tree creation and parsing :)

Hence wait() is a basic required function for Selenium and can not be avoided :(

No comments:

Post a Comment