0

I have been trying to use PhantomJSWebDriver framework for automating an application using Headless browser. The main issue is as we can successfully switch between windows in firefox or IE windows, here I am not able to switch between windows based on handles. Please help me.

Below is the code I have tried so far.

    System.setProperty("phantomjs.binary.path", file.getAbsolutePath());        
    driver = new PhantomJSDriver();  
    driver.get(application url);          

    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

    WebElement txtUsername = driver.findElement(By.id("it_C_C5"));        
    txtUsername.sendKeys("sreenis");        
    WebElement txtPassword = driver.findElement(By.id("it_C_C7"));
    txtPassword.sendKeys("sreeni");
    WebElement btnLogin = driver.findElement(By.id("ic_C_C8"));        
    btnLogin.click();

    Thread.sleep(10000);

    String winTitle = "Role profile selection";

    boolean bool = switchWindow(winTitle);
    if (bool){
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();     
    }
    else {
        System.out.println("Switch to window '" + winTitle + "' failed!");
        driver.quit();
    }

 public static boolean switchWindow(String windowtitle){

    String mainWindowsHandle = driver.getWindowHandle();
    Set<String> handles = driver.getWindowHandles();
    System.out.println(handles.size());
    for(String winHandle : handles){            
        driver.switchTo().window(winHandle);
        System.out.println(driver.getTitle());
        if(driver.getTitle().toLowerCase().equals(windowtitle)){
            return true;
        }           
    }

    driver.switchTo().window(mainWindowsHandle);
    return false;
}

When I tried to print the window titles in collection, it is only printing the parent window and not the other windows. I am not able to guess what is happening since nothing can be seen and it is headless testing. Please suggest me is there any other way so that I can test the app with many browser windows.

Mukesh Takhtani
  • 852
  • 5
  • 15
coding_cs
  • 77
  • 9
  • whats the error you are getting on console? you may want to provide the stack trace. – Paras Dec 18 '15 at 08:33
  • No Error but, it is unable to find the window to switch in Window handles collection. Have you tried this, if yes please tell me what mistake I have done. I have even added Thread.Sleep(10000) :( – coding_cs Dec 18 '15 at 08:54
  • which browser are you using.Is it IE?? – Akhil K Dec 18 '15 at 09:27
  • @AkhilK: It is PhantomJS no browser is visible, it is Headless browser testing. – coding_cs Dec 18 '15 at 09:35
  • @harsha.cs, how would you know, it is unable to find the window, it must have thrown an error right? can I have the trace of the log? – Paras Dec 18 '15 at 12:27
  • @pArAs: See my above code, I am just call switchToWindow() function, if it returns false I am quitting driver. So inside switchToWindow it is looping only one time and the window title inside the function is same as the earlier opened window, it is not getting new title and hence it is returning false, so it is going to else condition and quitting driver without any error. Hope you understood :) – coding_cs Dec 18 '15 at 13:43
  • which phantomjs driver version are you using? – Paras Dec 19 '15 at 09:26
  • @pArAs: PhantomJSDriver with Selenium – coding_cs Dec 22 '15 at 06:20
  • maybe its because it has no window handle really, after all it is headless? – Menahem Feb 08 '17 at 15:21

1 Answers1

0
Actions act = new Actions(d);
    act.contextClick(elements).sendKeys("W").perform();

    Set<String> win = d.getWindowHandles();
    Iterator <String> itrwin = win.iterator();
    String parent = itrwin.next();
    String child = itrwin.next();
    d.switchTo().window(child);

identify an web element first using findElement() and store it in element.