July 12, 2016

Perfect Example for Handling Window,popup,Frame and Alerts

July 12, 2016 18
Perfect Example for Handling Window,popup,Frame and Alerts
In this post i will explain about how to handle

  1. Multiple windows
  2. Popups
  3. Tabs
  4. Frames
  5. Alerts
and how to write customized functions, instead of duplicating the webdriver code.

Here is one example to switchToChildWindow()
This method is useful to switch to new window/tab/popup if you have only 2 windows/tabs/popup then you can use below customized function.

In the below example i wrote different functions 

  1.  isElepresent(By locator) -- Verify if the element is present or not
  2. selectByText(By locator, String visibleText) select a value from dropdown
  3. switchToChildWindow() ---Switch to  child window. If there are only two windows.
  4. switchToWindowByTitle(String title)  ---Switch to child window. Use this if title of the pages are unique.
  5. switchToFrame(WebElement locator)  --switch to frame by locating the frame.

Scenario: which has all multiple windows/tabs/popups/alerts/frames etc...

  1. Open http://www.hdfcbank.com/
  2. Close the Ad window if that is visible
  3. Click on Agri Link --which will open new window/tab
  4. Select "Telugu" from dropdown
  5. Handle the alert
  6. Select "English" from dropdown
  7. Click on Account Details --which will open new window/tab
  8. In the new window, enter customer ID --"test" (Actually this text box is in frame)
  9. Click on Privacy Policy link (Actually this link is in another frame) --which opens new window/tab/popup
  10. Click on CSR link on Privacy Policy page.
  11. Now come back to Main window (Parent window) 
  12. Click on Customer Care.

Below is the code: