Multiplying windows
Web applications, unfortunately, do not live in one window of your browser. An example of this can be a site that shows reports. Most reports will have their own window so that people can easily move between them.
Unfortunately, in testing terms, this can be quite difficult to do, but in this section, we will have a look at creating a test that can move between windows.
Working with multiple windows
Working with multiple browser windows can be one of the most difficult things to do within a Selenium test. This is down to the fact that the browser needs to allow Selenium to programmatically know how many child browser processes have been spawned.
In the following examples, we will see the tests click on an element on the page that will cause a new window to appear. If you have a pop-up blocker running, it's a good idea to disable it for this site while you work through these examples. Open up Selenium IDE and go to the Chapter 1 page on the site and refer to the following steps:
- Click on one of the elements on the page that has the text Click this link to launch another window. This will cause a small window to appear.
- Verify the text in the popup by right-clicking and selecting VerifyText id=popup text within the popup window.
- Once the window has loaded, click on the Close the Window text inside it.
- Add a verify command for an element on the page. Your test should now look like the following screenshot:
Sometimes, Selenium IDE will add a clickAndWait
command instead of a click
command. This is because it notices that the page has to unload. If this happens, just change the clickAndWait
command to a click so that it does not cause a timeout in the test.
In the test script, we can see that it has clicked on the item to load the new window and then has inserted a waitForPopUp
command. This is so that your test knows that it has to wait for a web server to handle the request and the browser to render the page. Any commands that require a page to load from a web server will have a waitFor
command. The next command is the selectWindow
command. This command tells Selenium IDE that it will need to switch context to the window, called popupwindow
, and will execute all the commands that follow in that window unless told otherwise by a later command.
Once the test has finished with the pop-up window, it will need to return to the parent window from where it started. To do this, we need to specify null
as the window. This will force the selectWindow
command to move the context of the test back to its parent window.
Complex working with multiple windows
In this example, we will open two pop-up windows and move between them and the parent window as it completes its steps:
- Start Selenium IDE and go to Chapter 1 on the website.
- Click on the Click this link to launch another window link. This will launch a pop-up window.
- Assert the text on the page. We do this by right-clicking and selecting assertText.
- Go back to the parent window and click on the link to launch the second pop-up window.
- Verify the text on the page.
- Move to the first pop-up window and close it using the close link. As before, be aware of
clickAndWait
instead of click. - Move to the second pop-up window and close it using the close link.
- Move back to the parent window and verify an element on that page.
- Run your test and watch how it moves between the windows. When complete, it should look like the following screenshot:
We just had a look at creating a test that can move between multiple windows. We saw how we can move between the child windows and its parent window as though we were a user.