Thursday, 8 July 2010

Selenium Connecting to New Windows

I have been evaluating Selenium as of late. One thing I was having issues with was connecting to new windows opened with target="_blank" link using the selectWindow function. It wasn't working and a looked high and low for information on why I could not connect to the newly opened window. I couldn't find anything helpful. Even got no replies to my question on the email list. I was finally poking around and found out that selectWindow just won't work in Selenium IDE and that you need to use Selenium RC or Selenium Grid. So hopefully this blog post will help people who have the same issue as I had figure out how to get this working. I guess I'm not the only one who has done their first investigation and evaluation of how Selenium works using the Selenium IDE before moving on RC and Grid. Also while I was using RC and the Grid I was using Python files rather than the default HTML that IDE uses. Below you can see the files of my example case it will also close the window and reconnect with the original launching window.

Python Test Script:
from selenium import selenium
import unittest, time, re

class test_link_to_blank(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "file:///D:/code/selenium/sandpit/new-window/base-page-blank.html")
        self.selenium.start()
    
    def test_test_link_to_blank(self):
        sel = self.selenium
        sel.open("file:///D:/code/selenium/sandpit/new-window/base-page-blank.html")
        self.assertEqual("Base Page Blank", sel.get_title())
        try: self.failUnless(sel.is_text_present("Base Page Blank"))
        except AssertionError, e: self.verificationErrors.append(str(e))
        sel.click("link=Link")
        sel.select_window("title=Pop Up Window")
        self.assertEqual("Pop Up Window", sel.get_title())
        try: self.failUnless(sel.is_text_present("Pop Up Window"))
        except AssertionError, e: self.verificationErrors.append(str(e))
        sel.close()
        sel.select_window("null")
        self.assertEqual("Base Page Blank", sel.get_title())
        try: self.failUnless(sel.is_text_present("Base Page Blank"))
        except AssertionError, e: self.verificationErrors.append(str(e))
    
    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

base-page-blank.html:
<html>
<head>
<title>Base Page Blank</title>
</head>
<body>
<p>Base Page Blank</p>
<p><a href="popup.html" target="_blank">Link</a></p>
</body>
</html>

popup.html:
<html>
<head>
<title>Pop Up Window</title>
</head>
<body>
<p>Pop Up Window</p>
</body>

6 comments:

Wale said...

Thanks a millions Dave. It worked for me at last. This is fantastic.

Anonymous said...

Hi Dave,
first - thanks a lot for sharing your knowledge. it's really helpful.

I tried your solution with selecting new windows with selenium. It only works for me when I'm using button with window.open. when i try and a link with href - it never works.

do you know why?

thanks a lot.

Joe

Karit said...
This comment has been removed by the author.
Karit said...

Hi Joe,

Sorry I don't

Anonymous said...

thanks for your time and effort.

Anonymous said...

hi,
maybe you can help - I'm trying to use selenium with xpath to activate a link on the page(locator).

this is basically the HTML -
list item
definition list class=class1
item in a definition list(text=same text)
describe an item

list item
definition list class=class2
item in a definition list(text=same text)
describe an item

definition list class=class3
item in a definition list(text=other text)
describe an item
unordered list class=class3
list item class=class4
my link is here with the click function and some text

i needed to write the html as text - i failed validation when posting the comment...

I need to activate the link(click).
I know the classes and the texts.
any idea how to do it using xpath?
thanks in advance.