Search in sources :

Example 1 with NoSuchWindowException

use of org.openqa.selenium.NoSuchWindowException in project cerberus-source by cerberustesting.

the class WebDriverService method doSeleniumActionSwitchToWindow.

@Override
public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier identifier) {
    MessageEvent message;
    String windowTitle = identifier.getLocator();
    String currentHandle;
    // Add try catch to handle not exist anymore window (like when popup is closed).
    try {
        currentHandle = session.getDriver().getWindowHandle();
    } catch (NoSuchWindowException exception) {
        currentHandle = null;
        LOG.debug("Window is closed ? " + exception.toString());
    }
    try {
        // Get serials handles list of all browser windows
        Set<String> handles = session.getDriver().getWindowHandles();
        // Loop into each of them
        for (String windowHandle : handles) {
            if (!windowHandle.equals(currentHandle)) {
                session.getDriver().switchTo().window(windowHandle);
                if (checkIfExpectedWindow(session, identifier.getIdentifier(), identifier.getLocator())) {
                    message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWITCHTOWINDOW);
                    message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));
                    return message;
                }
            }
            LOG.debug("windowHandle=" + windowHandle);
        }
    } catch (NoSuchElementException exception) {
        LOG.debug(exception.toString());
    } catch (TimeoutException exception) {
        message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);
        message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));
        LOG.warn(exception.toString());
        return message;
    } catch (WebDriverException exception) {
        LOG.warn(exception.toString());
        return parseWebDriverException(exception);
    }
    message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT);
    message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));
    return message;
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 2 with NoSuchWindowException

use of org.openqa.selenium.NoSuchWindowException in project spring-boot by spring-projects.

the class WebMvcTestWebDriverIntegrationTests method shouldBeADifferentWebClient.

@Test
public void shouldBeADifferentWebClient() throws Exception {
    this.webDriver.get("/html");
    WebElement element = this.webDriver.findElement(By.tagName("body"));
    assertThat(element.getText()).isEqualTo("Hello");
    try {
        ReflectionTestUtils.invokeMethod(previousWebDriver, "getCurrentWindow");
        fail("Did not call quit()");
    } catch (NoSuchWindowException ex) {
    // Expected
    }
    assertThat(previousWebDriver).isNotNull().isNotSameAs(this.webDriver);
}
Also used : NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with NoSuchWindowException

use of org.openqa.selenium.NoSuchWindowException in project cerberus-source by cerberustesting.

the class WebDriverService method doSeleniumActionManageDialog.

@Override
public MessageEvent doSeleniumActionManageDialog(Session session, Identifier identifier) {
    MessageEvent message;
    try {
        if ("ok".equalsIgnoreCase(identifier.getLocator())) {
            // Accept javascript popup dialog.
            session.getDriver().switchTo().alert().accept();
            session.getDriver().switchTo().defaultContent();
            message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSE_ALERT);
            return message;
        } else if ("cancel".equalsIgnoreCase(identifier.getLocator())) {
            // Dismiss javascript popup dialog.
            session.getDriver().switchTo().alert().dismiss();
            session.getDriver().switchTo().defaultContent();
            message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSE_ALERT);
            return message;
        }
    } catch (NoSuchWindowException exception) {
        // Add try catch to handle not exist anymore alert popup (like when popup is closed).
        LOG.debug("Alert popup is closed ? " + exception.toString());
    } catch (TimeoutException exception) {
        LOG.warn(exception.toString());
    } catch (WebDriverException exception) {
        LOG.debug("Alert popup is closed ? " + exception.toString());
        return parseWebDriverException(exception);
    }
    return new MessageEvent(MessageEventEnum.ACTION_FAILED_CLOSE_ALERT);
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) TimeoutException(org.openqa.selenium.TimeoutException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 4 with NoSuchWindowException

use of org.openqa.selenium.NoSuchWindowException in project selenium_java by sergueik.

the class PlunkerTest method testFullScreeenInNewWindow.

// Failed tests: afterMethod(com.mycompany.app.PlunkerTest): no such window:
// target window already closed(..)
@Test(enabled = false)
public void testFullScreeenInNewWindow() {
    projectId = "WFJYrM";
    driver.get(String.format("https://plnkr.co/edit/%s/?p=info", projectId));
    WebElement runButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > nav button i.icon-play")));
    assertThat(runButton, notNullValue());
    highlight(runButton);
    runButton.click();
    WebElement previewIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("iframe[name='plunkerPreviewTarget']")));
    assertThat(previewIframe, notNullValue());
    WebDriver iframe = driver.switchTo().frame(previewIframe);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
    }
    // System.err.println(iframe.getPageSource());
    // the fullscreen button is not in the preview frame
    driver.switchTo().defaultContent();
    WebElement fullScreenButton = null;
    try {
        fullScreenButton = wait.until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver d) {
                Optional<WebElement> e = d.findElements(By.cssSelector("button#expand-preview")).stream().filter(o -> {
                    String t = o.getAttribute("title");
                    return (Boolean) (t.contains("Launch the preview in a separate window"));
                }).findFirst();
                return (e.isPresent()) ? e.get() : (WebElement) null;
            }
        });
    } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
    }
    assertThat(fullScreenButton, notNullValue());
    String currentHandle = null;
    try {
        currentHandle = driver.getWindowHandle();
    // System.err.println("Thread: Current Window handle: " + currentHandle);
    } catch (NoSuchWindowException e) {
    }
    // open fullscreen view in a new browser window.
    String openinLinkNewBrowserWindow = Keys.chord(Keys.SHIFT, Keys.RETURN);
    fullScreenButton.sendKeys(openinLinkNewBrowserWindow);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
    }
    // confirm it opens in a new browser window.
    windowHandles = driver.getWindowHandles();
    assertThat(windowHandles.size(), equalTo(2));
    Iterator<String> windowHandleIterator = windowHandles.iterator();
    while (windowHandleIterator.hasNext()) {
        String handle = windowHandleIterator.next();
        if (!handle.equals(currentHandle)) {
            driver.switchTo().window(handle);
            assertThat(getBaseURL(), equalTo("https://run.plnkr.co"));
            // System.err.println(getBaseURL());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
            driver.close();
            // System.err.println("After close");
            try {
                driver.switchTo().defaultContent();
                System.err.println("After defaultcontext");
            } catch (NoSuchWindowException e) {
            }
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) URL(java.net.URL) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) WebDriver(org.openqa.selenium.WebDriver) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Matcher(java.util.regex.Matcher) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AfterClass(org.testng.annotations.AfterClass) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) Keys(org.openqa.selenium.Keys) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) By(org.openqa.selenium.By) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) Test(org.testng.annotations.Test)

Example 5 with NoSuchWindowException

use of org.openqa.selenium.NoSuchWindowException in project selenium_java by sergueik.

the class PlunkerTest method testFullScreeen.

@Test(enabled = true)
public void testFullScreeen() {
    projectId = "WFJYrM";
    driver.get(String.format("https://plnkr.co/edit/%s/?p=info", projectId));
    WebElement runButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > nav button i.icon-play")));
    assertThat(runButton, notNullValue());
    highlight(runButton);
    runButton.click();
    WebElement previewIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("iframe[name='plunkerPreviewTarget']")));
    assertThat(previewIframe, notNullValue());
    WebDriver iframe = driver.switchTo().frame(previewIframe);
    sleep(500);
    // System.err.println(iframe.getPageSource());
    // the fullscreen button is not in the preview frame
    driver.switchTo().defaultContent();
    WebElement fullScreenButton = null;
    try {
        fullScreenButton = wait.until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver d) {
                Optional<WebElement> e = d.findElements(By.cssSelector("button#expand-preview")).stream().filter(o -> {
                    String t = o.getAttribute("title");
                    return (Boolean) (t.contains("Launch the preview in a separate window"));
                }).findFirst();
                return (e.isPresent()) ? e.get() : (WebElement) null;
            }
        });
    } catch (Exception e) {
        System.err.println("Exception: " + e.toString());
    }
    assertThat(fullScreenButton, notNullValue());
    // confirm it opens in a new tab.
    // fullScreenButton.click();
    // alternatively, enforce open in a new tab:
    String openinLinkNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
    fullScreenButton.sendKeys(openinLinkNewTab);
    sleep(500);
    // confirm it opens in a new tab.
    String currentHandle = null;
    try {
        currentHandle = driver.getWindowHandle();
    // System.err.println("Thread: Current Window handle: " + currentHandle);
    } catch (NoSuchWindowException e) {
    }
    windowHandles = driver.getWindowHandles();
    assertThat(windowHandles.size(), equalTo(2));
    // String handle = windowHandleIterator.next();
    for (String handle : windowHandles) {
        if (!handle.equals(currentHandle)) {
            driver.switchTo().window(handle);
            assertThat(getBaseURL(), equalTo("https://run.plnkr.co"));
            // System.err.println(getBaseURL());
            driver.switchTo().defaultContent();
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) URL(java.net.URL) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) WebDriver(org.openqa.selenium.WebDriver) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Matcher(java.util.regex.Matcher) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AfterClass(org.testng.annotations.AfterClass) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) Keys(org.openqa.selenium.Keys) ExpectedConditions(org.openqa.selenium.support.ui.ExpectedConditions) By(org.openqa.selenium.By) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) Test(org.testng.annotations.Test)

Aggregations

NoSuchWindowException (org.openqa.selenium.NoSuchWindowException)5 WebElement (org.openqa.selenium.WebElement)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 TimeUnit (java.util.concurrent.TimeUnit)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 MessageEvent (org.cerberus.engine.entity.MessageEvent)2 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)2 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2