Switch Commands

Switch Commands

We often find situations where we might have to switch our focus/webdriver control to another window, popup, message box or frame. The reason we might have to do this is because of the way WebDriver has been designed. In short suffice to say that there is almost a separate DOM associated with each of the types mentioned above. And since Selenium WebDriver has been designed to handle only one DOM at a time with the reference handle we create, we will have to switch the references to be able to do operations. Lets see some examples below

Pre-requisites:

  1. You have completed the section “Set up Env” and have your environment ready with JDK, maven and Eclipse (+plugins)
  2. You have read and understood HTML DOM
  3. You were able to comprehend “Find Element Strategies” post
  4. You downloaded the project code base for class5 under – https://github.com/machzqcq/CucumberJVMExamples and imported into Eclipse and the structure should look as below

class5_project_structure

If you import in Intellij [which I would recommend over Eclipse IDE], it should look as below

class5_intellij_project_structure

Switch to a new Window

Here we are switching to a window by using the handle reference

Switch to a new Tab

A browser tab is pretty much another window. So we handle it the same way as we did for window

Switch to JavaScript Alert

A Javascript alert is a pop up that comes up by firing a javascript alert [Remember that this is a different alert from the alert that vb script can generate as that is not spawned from browser]. Generally a selenium javascript alert object has accept, dismiss methods that correspond to OK and CANCEL

Switch to Frame

A frame generally represents a HTML document. So frames contain another DOM inside a parent DOM. Hence to interact with the elements within a frame, we will need to switch to that frame. We can switch back to parentFrame anytime with a method as shown below.

Cucumber Scenario

step_definitions/SwitchCommands.java

Execution Output

Intellij ouput (How to execute is explained in previous post)

class5_intellij_output

Eclipse output

class5_eclipse_output

Command line output

class5_cmd_output

CI Server output

class5_ci_output

class5_ci_output1

Certy Resume @ All Rights Reserved 2017