I have a scenario where a new browser window opens when a button is clicked. I need to close the new window after checking the elements and switch back to parent window.
I tried the following code but I am unable to close the child window because of which I am unable to get the control back to the parent window.
package testDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.SeleneseTestNgHelper;
public class Driver extends SeleneseTestNgHelper
{
private WebDriver driver = null;
private String parentwindow;
@BeforeClass
public void setup() throws InterruptedException
{
//driver = new FirefoxDriver();
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void user() throws InterruptedException
{
driver.findElement(By.xpath("//input[@name='username']")).sendKeys("ccaadmin");
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("ccaadmin");
driver.findElement(By.xpath("//input[@name='password']/following::div[text()='Login']")).click();
Thread.sleep(2000);
parentwindow = driver.getWindowHandle();
driver.findElement(By.xpath("//td[text()='Services']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//td[text()='Reports']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[contains(text(),'Activity Log')]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//td[contains(text(),'Filters')]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//div[text()='Run/View']")).click();
Thread.sleep(50000);
//Switch to new window opened
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.switchTo().frame("reportframe");
driver.switchTo().frame("openDocChildFrame");
Thread.sleep(2000);
driver.findElement(By.id("IconImg_CrystalReportViewer_toptoolbar_export")).click();
Thread.sleep(20000);
//Close the new window, if that window no more required
driver.close();
driver.switchTo().window(parentwindow);
}
@AfterTest
public void tearDown() throws InterruptedException
{
driver.switchTo().window(parentwindow);
System.out.println("aftertest");
driver.findElement(By.xpath("//html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr/td[3]/table/tbody/tr/td[2]/div/div")).click();
Thread.sleep(2000);
System.out.println("after click onlogout");
driver.quit();
}
}
--
Thanks & Regards,
DAYANANDA REDDY CHALLA
-----------------------------------------------------------------------