code: public static ExpectedCondition<String> appearingOfWindowAndSwitchToIt(final String title) {
          return new ExpectedCondition<String>() {
              @Override
              public String apply(final WebDriver driver) {
                  final String initialHandle = driver.getWindowHandle();
                  for (final String handle : driver.getWindowHandles()) {
                      if (needToSwitch(initialHandle, handle)) {
                          driver.switchTo().window(handle);
                          if (driver.getTitle().equals(title)) {
                              return handle;
                          }
                      }
                  }
                  driver.switchTo().window(initialHandle);
                  return null;
              }
  
              @Override
              public String toString() {
                  return String.format("appearing of window by title %s and switch to it", title);
              }
          };
      }
docs: We don't know the actual window title without switching to one.
  This method was always used to make sure that the window appeared. After it we switched to appeared window.
  Switching between windows is rather time consuming operation
  <p>
  To avoid the double switching to the window we are switching to window in this method
  <p>
  The same approach is applies to all ExpectedConditions for windows
  
  @param title - title of window
  @return - handle of expected window