May 19, 2011

Working with Multiple windows

I saw many posts on identifying window Name, ID and Title or Attribute....but all are giving Syntax...We can find that syntax in Selenium document. In this post, I would like to give an example how to identify window Name, ID and Title or Attribute...


Below is the code is to identify the new window....By using TITLE 


package test;


import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;


public class NewWindow extends SeleneseTestCase {
public SeleniumServer ss;
@Before
public void setUp() throws Exception {
ss= new SeleniumServer();
ss.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.truepanel.com/");
selenium.start();
}


@Test
public void testNewWindow() throws Exception {
selenium.open("/");
   selenium.windowMaximize();
   selenium.setSpeed("2000");
   selenium.click("//div[@id='right']/div[2]/a[1]");
   selenium.setSpeed("2000");
   //Click on Facebook icon
   selenium.click("//div[@id='right']/div[2]/a[2]");
   selenium.setSpeed("2000");
   //Click on Linkedin icon
   selenium.click("//div[@id='right']/div[2]/a[3]");
   selenium.setSpeed("2000");
      String[] winNames = selenium.getAllWindowTitles();
 //All window names should be displayed in console
      for (int i = 0; i < winNames.length; i++) 
   {
    System.out.println("Window Titles are "+winNames[i]);
}
             
   /*selenium.selectWindow("Twitter");
   verifyEquals("Truepanel (Truepanel) on Twitter", selenium.getTitle());*/
   //Selects the new window with Title Truepanel | Facebook
   selenium.selectWindow("TruePanel | Facebook");
   selenium.setSpeed("2000");
   selenium.windowFocus();
   selenium.setSpeed("2000");
   selenium.windowMaximize();
   selenium.setSpeed("2000");
   if(selenium.isElementPresent("//div[@id='globalContainer']/div[2]/div/a/span"))
   {
    selenium.click("//div[@id='globalContainer']/div[2]/div/a/span");
selenium.waitForPageToLoad("30000");
selenium.type("firstname", "test");
selenium.type("lastname", "test");
selenium.type("reg_email__", "tets");
selenium.type("reg_email_confirmation__", "test");
selenium.selectWindow("null");
       selenium.windowFocus();
       verifyEquals("Welcome to Truepanel | Online Video Focus Groups", selenium.getTitle());
       selenium.click("//div[@id='navBtns']/a[2]");
selenium.waitForPageToLoad("30000");
       Thread.sleep(1000);
   }
   
     else
     {
    //this command will select the parent window
       selenium.selectWindow("null");
       selenium.windowFocus();
       verifyEquals("Welcome to Truepanel | Online Video Focus Groups", selenium.getTitle());
       Thread.sleep(1000);
   }
}
@After
public void tearDown() throws Exception {
selenium.stop();
ss.stop();
}
}

1 comment: