Java Code Examples for org.openqa.selenium.WebDriver#getWindowHandles()
The following examples show how to use
org.openqa.selenium.WebDriver#getWindowHandles() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageSteps.java From vividus with Apache License 2.0 | 7 votes |
/** * Closes <b>current window</b> and switches to the window from which rederection to current window was performed * <p> * Each browser <b>window</b> or <b>tab</b> is considered to be a separate <b>window object</b>. This object holds * corresponding <b>Document</b> object, which itself is a html page. So this method applies to both windows and * tabs. * <p> * Actions performed at this step: * <ul> * <li>Receives all opened browser windows * <li>Identifies current window and closes it * <li>Switches back to the window from which rederection to current window was performed * </ul> * @see <a href="https://html.spec.whatwg.org/#browsing-context"><i>Browsing context (Window & Document)</i></a> * @see <a href="https://www.w3schools.com/tags/default.asp"><i>HTML Element Reference</i></a> */ @When("I close the current window") public void closeCurrentWindow() { WebDriver driver = getWebDriver(); String currentWindow = driver.getWindowHandle(); for (String window : driver.getWindowHandles()) { if (!window.equals(currentWindow)) { driver.close(); driver.switchTo().window(window); break; } } highlightingSoftAssert.assertThat("Current window has been closed", String.format("Current window '%s' has been closed", currentWindow), driver.getWindowHandles(), not(contains(currentWindow))); }
Example 2
Source File: EncapsulateOperation.java From LuckyFrameClient with GNU Affero General Public License v3.0 | 6 votes |
private static String windowHandleByTitle(WebDriver driver, String title) { String result = ""; String original = driver.getWindowHandle(); if (title.isEmpty()) { result = original; } else { Set<String> windowHandles = driver.getWindowHandles(); for (String windowHandle : windowHandles) { driver.switchTo().window(windowHandle); if (title.equals(driver.getTitle())) { result = windowHandle; break; } } if (0 < windowHandles.size()){ driver.switchTo().window(original); } } result = result.isEmpty() ? "��ȡ���ھ��ֵʧ�ܣ���Ҫ��ȡ���ھ��ֵ�ı��⡾" + title + "��û���ҵ�" : "��ȡ����ֵ�ǡ�" + result + "��"; return result; }
Example 3
Source File: EncapsulateOperation.java From LuckyFrameClient with GNU Affero General Public License v3.0 | 6 votes |
private static String windowHandleByIndex(WebDriver driver, int index) { String result; try { List<String> windowHandles = new ArrayList<>(driver.getWindowHandles()); if (index > windowHandles.size()) { result = "��ȡ���ھ��ֵʧ�ܣ���Ҫ��ȡ���ھ��ֵ���±" + index + "�����ڵ�ǰ���ھ��������" + windowHandles.size() + "��"; } else { if (0 >= index){ result = "��ȡ����ֵ�ǡ�" + driver.getWindowHandle() + "��"; } else{ result = "��ȡ����ֵ�ǡ�" + windowHandles.get(index - 1) + "��"; } } } catch (IndexOutOfBoundsException e) { LogUtil.APP.error("��ȡ���ھ��ֵ�����쳣����Ҫ��ȡ���ھ��ֵ���±{}��Խ��",index,e); result = "��ȡ���ھ��ֵʧ�ܣ���Ҫ��ȡ���ھ��ֵ���±" + index + "��Խ��"; } return result; }
Example 4
Source File: SeleniumBrowser.java From OpenID-Attacker with GNU General Public License v2.0 | 6 votes |
public static void loginVictimToWordpress() { WebDriver driver = getWebDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("var win = window.open('https://de.wordpress.com/wp-login.php');"); List<String> windowhandles = new ArrayList<>(driver.getWindowHandles()); driver.switchTo().window(windowhandles.get(1)); WebElement element = driver.findElement(By.id("user_login")); element.clear(); element.sendKeys("victim123456789"); element = driver.findElement(By.id("user_pass")); element.clear(); element.sendKeys("Victim1234!"); element.submit(); /*windowhandles.forEach((windowHandle) -> { System.out.println("windowHandle: " + windowHandle); });*/ driver.switchTo().window(windowhandles.get(0)); }
Example 5
Source File: BrowserTabUtil.java From keycloak with Apache License 2.0 | 6 votes |
private BrowserTabUtil(WebDriver driver) { this.driver = driver; if (driver instanceof JavascriptExecutor) { this.jsExecutor = (JavascriptExecutor) driver; } else { throw new RuntimeException("WebDriver must be instance of JavascriptExecutor"); } // HtmlUnit doesn't work very well with JS and it's recommended to use this settings. // HtmlUnit validates all scripts and then fails. It turned off the validation. if (driver instanceof HtmlUnitDriver) { WebClient client = ((DroneHtmlUnitDriver) driver).getWebClient(); client.getOptions().setThrowExceptionOnScriptError(false); client.getOptions().setThrowExceptionOnFailingStatusCode(false); } tabs = new ArrayList<>(driver.getWindowHandles()); }
Example 6
Source File: CloseTab.java From NetDiscovery with Apache License 2.0 | 6 votes |
@Override public SeleniumAction perform(WebDriver driver) { String originalWindowHandle = driver.getWindowHandle(); List<String> windowHandles = new ArrayList<String>(driver.getWindowHandles()); if (windowHandles.size() == 1) { System.out.println("Skipping this step... as it possible that no new tab/window was opened"); goBack().doIt(driver); return this; } windowHandles.remove(originalWindowHandle); for (String currentWindow : windowHandles) { driver.switchTo().window(currentWindow); driver.close(); } driver.switchTo().window(originalWindowHandle); return this; }
Example 7
Source File: TeasyExpectedConditions.java From teasy with MIT License | 6 votes |
public static ExpectedCondition<String> appearingOfWindowByPartialUrl(final String url) { 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.getCurrentUrl().contains(url)) { return handle; } } } driver.switchTo().window(initialHandle); return null; } @Override public String toString() { return String.format("appearing of window by partial url %s and switch to it", url); } }; }
Example 8
Source File: EngineInvoker.java From phoenix.webui.framework with Apache License 2.0 | 6 votes |
/** * 关闭url以指定字符串开头的window * @param phoenix 引擎 * @param params 参数 */ public static void closeWinByUrlStartWith(Phoenix phoenix, String[] params) { String startWith = params[0]; WebDriver driver = phoenix.getEngine().getDriver(); Set<String> handles = driver.getWindowHandles(); Iterator<String> handleIt = handles.iterator(); String currentHandle = driver.getWindowHandle(); while(handleIt.hasNext()) { String handle = handleIt.next(); driver.switchTo().window(handle); if(driver.getCurrentUrl().startsWith(startWith)) { driver.close(); break; } } driver.switchTo().window(currentHandle); }
Example 9
Source File: KWSWindow.java From xframium-java with GNU General Public License v3.0 | 5 votes |
/** * Verify switch window. * * @param webDriver the web driver * @param byTitleOrUrl the by title or url * @param winExpValue the win exp value * @return true, if successful */ private boolean verifySwitchWindow( WebDriver webDriver, String byTitleOrUrl, String winExpValue ) { boolean bSwitchWindow = false; String winActValue = ""; Set<String> availableWindows = webDriver.getWindowHandles(); if ( !availableWindows.isEmpty() ) { for ( String windowId : availableWindows ) { if ( byTitleOrUrl.equalsIgnoreCase( "BY_WINTITLE" ) ) { winActValue = webDriver.switchTo().window( windowId ).getTitle().trim().toLowerCase(); } else { winActValue = webDriver.switchTo().window( windowId ).getCurrentUrl().trim().toLowerCase(); } winExpValue = winExpValue.trim().toLowerCase(); if ( winActValue.contains( winExpValue ) ) { bSwitchWindow = true; break; } } } return bSwitchWindow; }
Example 10
Source File: Windows.java From selenium with Apache License 2.0 | 5 votes |
public void selectPopUp(WebDriver driver, String windowID) { if ("null".equals(windowID) || "".equals(windowID)) { Set<String> windowHandles = driver.getWindowHandles(); windowHandles.remove(originalWindowHandle); if (!windowHandles.isEmpty()) { driver.switchTo().window(windowHandles.iterator().next()); } else { throw new SeleniumException("Unable to find a popup window"); } } else { selectWindow(driver, windowID); } }
Example 11
Source File: WindowsHandler.java From carina with Apache License 2.0 | 5 votes |
public static boolean switchToPopup(WebDriver driver) { try { Set<String> beforeHandles = windows.get((driver).hashCode()); Set<String> afterHandles = driver.getWindowHandles(); afterHandles.removeAll(beforeHandles); String newWindowHandle = afterHandles.iterator().next(); driver.switchTo().window(newWindowHandle); return true; } catch (Exception e) { LOGGER.warn("Switching to top window was not performed!"); return false; } }
Example 12
Source File: TabAndWindowStepDefinition.java From IridiumApplicationTesting with MIT License | 5 votes |
/** * Switchs to the specified tab. This is useful when you open a link that opens in a new window. * * @param tabIndex The index of the new tab. Usually 1 (the original tab will be 0) */ @When("I switch to tab \"(\\d+)\"$") public void switchTabs(final String tabIndex) { final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread(); final List<String> tabs2 = new ArrayList<>(webDriver.getWindowHandles()); webDriver.switchTo().window(tabs2.get(Integer.parseInt(tabIndex))); sleepUtils.sleep(State.getFeatureStateForThread().getDefaultSleep()); }
Example 13
Source File: Windows.java From selenium with Apache License 2.0 | 5 votes |
private void selectWindowWithTitle(WebDriver driver, String title) { String current = driver.getWindowHandle(); for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); if (title.equals(driver.getTitle())) { return; } } driver.switchTo() .window(current); throw new SeleniumException("Unable to select window with title: " + title); }
Example 14
Source File: NoraUiExpectedConditions.java From NoraUi with GNU Affero General Public License v3.0 | 5 votes |
/** * @param currentHandles * is list of opened windows. * @return a string with new Window Opens (GUID) */ public static ExpectedCondition<String> newWindowOpens(final Set<String> currentHandles) { return (@Nullable WebDriver driver) -> { if (driver != null && !currentHandles.equals(driver.getWindowHandles())) { for (String s : driver.getWindowHandles()) { if (!currentHandles.contains(s)) { return s; } } } return null; }; }
Example 15
Source File: EngineInvoker.java From phoenix.webui.framework with Apache License 2.0 | 5 votes |
/** * window窗口切换 * @param phoenix 引擎 */ public static void windowSwitch(Phoenix phoenix) { SeleniumEngine engine = phoenix.getEngine(); WebDriver driver = engine.getDriver(); Set<String> handlers = driver.getWindowHandles(); Iterator<String> it = handlers.iterator(); while(it.hasNext()) { String name = it.next(); driver.switchTo().window(name); } }
Example 16
Source File: GetAllWindowTitles.java From selenium with Apache License 2.0 | 5 votes |
@Override protected String[] handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) { String current = driver.getWindowHandle(); List<String> attributes = new ArrayList<>(); for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); attributes.add(driver.getTitle()); } driver.switchTo().window(current); return attributes.toArray(new String[attributes.size()]); }
Example 17
Source File: EncapsulateOperation.java From LuckyFrameClient with GNU Affero General Public License v3.0 | 5 votes |
private static WebDriver windowByIndex(WebDriver driver, int index) { try { List<String> windowHandles = new ArrayList<>(driver.getWindowHandles()); return driver.switchTo().window(windowHandles.get(index)); } catch (IndexOutOfBoundsException windowWithIndexNotFound) { return null; } }
Example 18
Source File: EncapsulateOperation.java From LuckyFrameClient with GNU Affero General Public License v3.0 | 5 votes |
private static WebDriver windowByTitle(WebDriver driver, String title) { String original = driver.getWindowHandle(); Set<String> windowHandles = driver.getWindowHandles(); for (String windowHandle : windowHandles) { driver.switchTo().window(windowHandle); if (title.equals(driver.getTitle())) { return driver; } } if (0 < windowHandles.size()){ driver.switchTo().window(original); } throw new NoSuchWindowException("Window with title[" + title + "] not found"); }
Example 19
Source File: WindowsActions.java From vividus with Apache License 2.0 | 5 votes |
private String pickWindow(BiFunction<WebDriver, String, String> picker, Supplier<String> defaultValueProvider) { WebDriver driver = getWebDriver(); for (String window : driver.getWindowHandles()) { String value = picker.apply(driver, window); if (null != value) { return value; } } return defaultValueProvider.get(); }
Example 20
Source File: HTMLAnchorElement2Test.java From htmlunit with Apache License 2.0 | 4 votes |
private void javascriptTarget(final String target, final int newWindows, final String frameAlerts, final String windowAlerts) throws Exception { assertTrue(newWindows < 2); // real browsers (selenium) are getting confused by this scenario // and as result the stuck in the code that tries to get the body if (newWindows > 0 && useRealBrowser()) { assertEquals(frameAlerts, "Please run manually"); return; } final String html = "<html>\n" + "<head><title>main</title></head>\n" + "<body title='main'>\n" + " <iframe id='testFrame' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>"; final String secondHtml = "<html>\n" + "<head><title>inner</title></head>\n" + "<body title='inner'>\n" + " <a id='tester' " + target + " href='javascript: try { document.body.setAttribute(\"title\", \"# \" + document.title); } " + "catch(e) { alert(e); }'>no href</a>\n" + "</body>\n" + "</html>"; getMockWebConnection().setResponse(URL_SECOND, secondHtml); final WebDriver driver = loadPage2(html); final String firstWindow = driver.getWindowHandle(); driver.switchTo().frame("testFrame"); assertEquals(1, driver.getWindowHandles().size()); driver.findElement(By.id("tester")).click(); String titleVal = driver.findElement(By.tagName("body")).getAttribute("title"); assertEquals(frameAlerts, titleVal); // we have to switch back to the outer content // otherwise selenium gets confused driver.switchTo().defaultContent(); final Set<String> windows = driver.getWindowHandles(); assertEquals(1 + newWindows, windows.size()); if (newWindows > 0) { windows.remove(firstWindow); driver.switchTo().window(windows.iterator().next()); } titleVal = driver.findElement(By.tagName("body")).getAttribute("title"); assertEquals(windowAlerts, titleVal); }