What is switch command?

Webdriver library contains a set of classes and methods that help us to communicate with the web browser. In most of testing scenarios we find element on the website and then click it in case of buttons or fill the text in case of text boxes. Often it’s enough when you begin to automate your tests in the web browser.

However at some point you may get stuck discovering that you’re not able to find the element on the page. The first thing that you should ask yourself is: Maybe my application opened a new popup window and the element that I’m trying to find is placed in this new window? If your answer is “YES” – remember one thing:

The new popup window(or new tab) shows another HTML page which opens in a new context, so you should switch context to the new window first and then you can communicate with the new window. This is exactly what switch command comes into play.

Methods available under WebDriver API for Switch Commands

There is a set of methods accessible in the webdriver API. We could use a Visual Studio functionality called intellisense. Intellisense shows all possible methods:Let’s examine the following methods quickly. We’ll cover some of them in the examples so that you can easily copy code fragments and run in your Visual Studio.

ActiveElement – Switches focus to element that has focus at the moment. If there’s no element with focus, this method will switch to body element of the page.

Alert – Switches focus to currently active dialog box

DefaultContent – Switches focus to the first frame on the page or main document if page contains iframes.

Now let’s see the way to find a list of windows that webdriver can interact with. Such set of windows is called windows handles. Each window handle is identified by random string, which is different on each test run.

Each of the tests in this article should be preceded by special method. Thanks to [TestInitialize] annotation the Setup() method is run before each test method in the class.

We can distinguish 3 handle types, We’ll cover each of them in the below examples:

alert

frame

Window

How to handle Alert in Selenium using C#?

Alertis a popup window that can be shown by running javascript alert() method. The website that we’re testing makes use of alert(). The result is presented in the following picture:

Let’s automate the test

Before we start, we need to be aware that there are various test frameworks that we can use. In the world of .NET the most popular frameworks are NUnit and Visual Studio Testing tools. In the example we used the latter because it’s a Visual Studio build-in tool so we don’t need to install additional NuGet packages. You can see How to Configure and Use NUnit Test Framework works.

The first step that we take is creating a new class with tests.We need to create test method which looks like this:

C#

1

2

3

4

5

6

7

8

9

10

[TestMethod]

publicvoidOpenJavaScriptAlert()

{

varalertButton=_driver.FindElement(By.Id("alert"));

alertButton.Click();

varalert=_driver.SwitchTo().Alert();

varexpectedAlertText="Knowledge increases by sharing but not by saving. Please share "+

"this website with your friends and in your organization.";

Assert.AreEqual(expectedAlertText,alert.Text);

}

What happens in the code:

We found Alert button element and later clicked on it.

Then we switched the context to newly created alert window.

Finally we use Assert class to compare the text displayed in the alert window with expected value. The test should pass.

How to handle Frame in Selenium using C#?

Some websites use frames to load fragments of the website. Most of the currently created websites avoid the frames. However we still need to know how to switch to frame. The solution is really simple and it looks exactly like above example with Alert. The only difference is that we use Frame method instead of Alert.

To switch the context to frame : _driver.SwitchTo().Frame()

To switch back to parent frame : _driver.SwitchTo().ParentFrame()

How to handle Window in Selenium using C#?

We can open a new window in javascript by running the window.open() method. On the website that we test after clicking a button the code that runs is similar to this: window.open(“www.toolsqa.com“);. When the web browser runs this code the new window opens. The web browser should show the following:Now let’s create next test to automate the test:

Step 1: Test is just locating the Button but it is not clicking on it yet. Assert is used to check the number of handles by using driver.WindowHandles property. It should be 1, which is the parent main window. Another assert is used to check the title of main window.

Step 2: Clicked the button in order to open a new window. Now the Assert is used to check that if the count of windows opened is 2.

Step 3: WindowHandles.Last() is used to point to last or recent window opened by the driver. Once the handle is recevied, SwichTo() method is used to change the context to the new window. he last statement in this test checks if the title of a new window changed.

How to handle a New Tab in Selenium using C#?

Now we know that we can open a URL in another window. But we can also open it in a new tab. Then a new tab in the browser is created and new window is not created. Let’s look at the code:

Author: Darek Kacban

Senior .NET Software Engineer. Keen on test automation, web applications and software architecture. Especially interested in Specflow and C#. Usually he works in Poland and solves at least one problem every day. He believes that education can good communication can improve software quality and customer satisfaction. Connect me at LINKEDIN