code: public Locomotive waitForWindow(String regex) {
          Set<String> windows = driver.getWindowHandles();
  
          for (String window : windows) {
              try {
                  driver.switchTo().window(window);
  
                  p = Pattern.compile(regex);
                  m = p.matcher(driver.getCurrentUrl());
  
                  if (m.find()) {
                      attempts = 0;
                      return switchToWindow(regex);
                  }
                  else {
                      // try for title
                      m = p.matcher(driver.getTitle());
  
                      if (m.find()) {
                          attempts = 0;
                          return switchToWindow(regex);
                      }
                  }
              } catch(NoSuchWindowException e) {
                  if (attempts <= MAX_ATTEMPTS) {
                      attempts++;
  
                      try {Thread.sleep(1000);}catch(Exception x) { x.printStackTrace(); }
  
                      return waitForWindow(regex);
                  } else {
                      fail("Window with url|title: " + regex + " did not appear after " + MAX_ATTEMPTS + " tries. Exiting.");
                  }
              }
          }
  
          // when we reach this point, that means no window exists with that title..
          if (attempts == MAX_ATTEMPTS) {
              fail("Window with title: " + regex + " did not appear after " + MAX_ATTEMPTS + " tries. Exiting.");
              return this;
          } else {
              System.out.println("#waitForWindow() : Window doesn't exist yet. [" + regex + "] Trying again. " + (attempts+1) + "/" + MAX_ATTEMPTS);
              attempts++;
              try {Thread.sleep(1000);}catch(Exception x) { x.printStackTrace(); }
              return waitForWindow(regex);
          }
      }
docs: /* Window / Frame Switching