follow me icons

Wednesday, October 6, 2010

Testing Pop up windows using Selenium webdriver

There are times when we need to test a pop up windows within the current browser. For example of this is when you try to test a pop up box for the user to fill in the user details or a print pop up box.

The problem is that the webdriver will still focus on the current browser and not the pop up windows and therefore any interaction will only go to the browser and not to the new pop up browsers.

To solve this simply switch the browser to point to the pop up.

Here is the implementation for selenium webdriver in ruby

def switch_to_new_pop_up
response.driver.browser.switch_to.window
(response.driver.browser.window_handles.last)
end

def close_active_window
response.driver.browser.close
response.driver.browser.switch_to.window
(response.driver.browser.window_handles[0])
end

4 comments:

  1. Hey,

    Very useful info. THanks. More selenium 2 article please :)

    ReplyDelete
  2. Hello, I am trying to use your implementation above and am having no luck. I am using selenium and cucumber to run my automation tests (I am new only 6 months exp) and need to create the step definitions. My application opens, enters some data and clicks "complete" then a new windows opens and I get any errors/warnings. I need to click that button, switch, validate messages and come back to the main window. I cannot get my step definition to work and keep getting undefined method 'window_handles'. Any chance you know how to get this into a step definition?

    Thanks

    ReplyDelete
    Replies
    1. Hi MonsterEnergy,
      you should probably use 'page' instead of 'response' everywhere, like this:
      page.driver.browser.switch_to.window(page.driver.browser.window_handles[0])

      Delete