-1

In my project i tried to automate Creating a new employee , for that i need to click a link which will open a child window from main window and in that child window i need to click on a lookup button, to select a report manager for that employee . After clicking that lookup im getting a new grandchild window which contains list of managers name, so that i can select one from it.

Following is the code that i have used to move between windows,

// Moving from Parent to Child

    String parentWindow = driver.getWindowHandle();

    Set<String> windowHandles = driver.getWindowHandles();      

    System.out.println(windowHandles.size());

    windowHandles.remove(parentWindow);

   driver.switchTo().window((String) windowHandles.toArray()[0]);

   // Click lookup to emulate Grandchild window
   driver.findElement(..)

   //From child to grandchild

   Set<String> grandChild_Child_ParentHandles=driver.getWindowHandles();

    grandChild_Child_ParentHandles.remove(parentWindow);
    grandChild_Child_ParentHandles.remove(windowHandles);

    System.out.println(grandChild_Child_ParentHandles.size());

    driver.switchTo().window((String) grandChild_Child_ParentHandles.toArray()[0]);

    System.out.println(driver.getTitle());

My code is running till clicking that lookup , after that my code stopped to execute. And the code starts again if and only i closed the Grandchild window manually .I'm wondering why it happening.

Kindly help!

Thanks in Advance, Siva

  • I am wondering whether the grandchild window you are talking about, is infact a new window or a javascript alert, basing on the statement that your code stopped to execute when the grandchild window opens, and the code resumes when it is closed. Try one thing. Place the code `System.out.println("Total windows: "+driver.getWindowHandles().size());` after the code that opens grandchild. Let me know what it returns.. – Subh Feb 12 '15 at 18:02
  • Hi, Thanks for ur quick reply . I tried by placing ur code after the grandchild window opens, as i mentioned earlier , total no of windows is getting displayed only after i closed the grandchild window. And i have already tried placing the below alert handle code, which also not working. Alert aler = driver.switchTo().alert(); System.out.println(aler.getText()); and im getting "null" as a text of the alert after closing the Grandchild window. – Sivashankar Mani Feb 13 '15 at 09:30
  • Can you please add a screenshot as how the grandchild window looks like? You can upload the [image in imgur](http://imgur.com/) and add the link here for reference. – Subh Feb 13 '15 at 09:36
  • Hi, kindly find the below shared link https://drive.google.com/file/d/0B4jUpzMjM4aNaWlJZ3JCblpNb1E/view?usp=sharing P.S: imgur is blocked in my office . – Sivashankar Mani Feb 13 '15 at 10:00
  • Oh! I am afraid this might be a [modal window](http://en.wikipedia.org/wiki/Modal_window).. As, is written in the wiki: `a modal window is a graphical control element subordinate to an application's main window which creates a mode` **`where the main window can't be used`**.. And, hence selenium can't automate it directly. You can take the help of [Robot class](http://www.java-tips.org/java-se-tips/java.awt/how-to-use-robot-class-in-java.html) or else an UI tool like [**Sikuli**](https://code.google.com/p/sikuli-api/wiki/BasicUsage) to interact with it and proceed with the rest actions. – Subh Feb 13 '15 at 11:37
  • Hi, I 've already tried using Sikuli as well Robot class also but none of them helped me out :( Sikuli can able to identify element on child window not on the Grandchild window. following are the code snippets i have used, For Sikuli, Screen s = new Screen(); s.find(image path); s.click(image path); For Robot, Robot rb =new Robot(); rb.keyPress(KeyEvent.VK_ALT); rb.keyPress(KeyEvent.VK_ENTER); (Since give Alt and Enter will select the first option in the list) is there anything i can fine tune in the above ?? – Sivashankar Mani Feb 13 '15 at 12:05
  • **If you use Robot**, Try giving a delay of 3 seconds after the code that opens the grandchild window executes, like: `rb.delay(3000);` or better yet try with `Thread.sleep(3000L)`. This will give some amount of time to Robot in order to take actions on the Grandchild window. – Subh Feb 13 '15 at 12:08
  • Thx for your wonderful help :) I have done the same with multi threading concept :) – Sivashankar Mani Feb 13 '15 at 15:31
  • Awesome.. You worked it out yourself finally.. (y) – Subh Feb 13 '15 at 16:15

1 Answers1

0

I faced this problem because of Modal window,Which can be resolved by using Multi threading concept. Usually once a modal window has evoked it will block its parent window , which meant that we cant move our driver to the new child window.So after the modal window evoked our code stops to work.

Try to refer the below sample code for multi threading ,which i've found in net.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

class NewThread implements Runnable {
static WebDriver driver = new FirefoxDriver();
Thread t;

NewThread() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
// Start the thread
t.start();
}

// This is the entry point for the second thread.
public void run() {
try {

System.out.println("This thread has got the control now");
Thread.sleep(5000);

} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}

for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

String nextHandle = driver.getWindowHandle();
System.out.println("nextHnadle" + nextHandle);

try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

driver.findElement(By.id("foo")).clear();
driver.findElement(By.id("foo")).sendKeys("4");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

driver.findElement(By.linkText("link")).click();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

driver.quit();
System.out.println("Exiting child thread.");
}

// execution will start from here
public static void main(String args[]) throws InterruptedException {
try {
// open the webpage
driver.get("https://developer.mozilla.org/samples/domref/showModalDialog.html");

// get window handle
String currenthandle = driver.getWindowHandle();
System.out.println("currenthandle" + currenthandle);

//start a new thread
new NewThread();

// click on the button to open model window dialog
driver.findElement(By.tagName("input")).click();

} catch (Exception e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}

}