Example usage for org.openqa.selenium WebDriver quit

List of usage examples for org.openqa.selenium WebDriver quit

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver quit.

Prototype

void quit();

Source Link

Document

Quits this driver, closing every associated window.

Usage

From source file:org.openqa.grid.e2e.misc.GridViaCommandLineTest.java

License:Apache License

@Test
public void testRegisterNodeToHub() throws Exception {
    String[] hubArgs = { "-role", "hub" };
    GridLauncherV3.main(hubArgs);/* ww  w .  ja v a 2s .c  om*/
    UrlChecker urlChecker = new UrlChecker();
    urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL("http://localhost:4444/grid/console"));

    String[] nodeArgs = { "-role", "node", "-hub", "http://localhost:4444", "-browser",
            "browserName=chrome,maxInstances=1" };
    GridLauncherV3.main(nodeArgs);
    urlChecker.waitUntilAvailable(100, TimeUnit.SECONDS, new URL("http://localhost:5555/wd/hub/status"));

    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
            DesiredCapabilities.chrome());

    try {
        driver.get("http://localhost:4444/grid/console");
        Assert.assertEquals("Should only have one chrome registered to the hub", 1,
                driver.findElements(By.cssSelector("img[src$='chrome.png']")).size());
    } finally {
        try {
            driver.quit();
        } catch (Exception e) {
        }
    }

}

From source file:org.openqa.grid.e2e.misc.Issue1586.java

License:Apache License

@Test(enabled = false)
public void test() throws MalformedURLException {
    DesiredCapabilities ff = DesiredCapabilities.firefox();
    WebDriver driver = null;
    try {/*  ww w  . j  av  a  2  s .c o  m*/
        driver = new RemoteWebDriver(new URL(hub.getUrl() + "/grid/driver"), ff);
        for (int i = 0; i < 20; i++) {
            driver.get("http://code.google.com/p/selenium/");
            WebElement keywordInput = driver.findElement(By.name("q"));
            keywordInput.clear();
            keywordInput.sendKeys("test");
            WebElement submitButton = driver.findElement(By.name("projectsearch"));
            submitButton.click();
            driver.getCurrentUrl(); // fails here
        }
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}

From source file:org.openqa.grid.e2e.node.SmokeTest.java

License:Apache License

@Test
public void firefoxOnWebDriver() throws MalformedURLException {
    WebDriver driver = null;
    try {/* w ww. j a  v a  2  s  .  c o m*/
        DesiredCapabilities ff = DesiredCapabilities.firefox();
        driver = new RemoteWebDriver(new URL(hub.getUrl() + "/wd/hub"), ff);
        driver.get(hub.getUrl() + "/grid/console");
        Assert.assertEquals(driver.getTitle(), "Grid overview");
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}

From source file:org.orcid.integration.blackbox.api.v2.rc1.BlackBoxBase.java

License:Open Source License

public static void revokeApplicationsAccess(String... clientIdsParam) {
    // Nothing to remove
    if (clientIdsParam == null) {
        return;//from w  w  w.jav  a 2s . c  om
    }
    List<String> clientIds = new ArrayList<String>();
    for (String clientId : clientIdsParam) {
        if (!PojoUtil.isEmpty(clientId)) {
            clientIds.add(clientId);
        }
    }

    String userName = System.getProperty("org.orcid.web.testUser1.username");
    String password = System.getProperty("org.orcid.web.testUser1.password");
    String baseUrl = "https://localhost:8443/orcid-web";
    if (!PojoUtil.isEmpty(System.getProperty("org.orcid.web.base.url"))) {
        baseUrl = System.getProperty("org.orcid.web.base.url");
    }

    WebDriver webDriver = new FirefoxDriver();

    int timeout = 4;
    webDriver.get(baseUrl + "/userStatus.json?logUserOut=true");
    webDriver.get(baseUrl + "/my-orcid");
    SigninTest.signIn(webDriver, userName, password);

    // Switch to accounts settings page
    By accountSettingsMenuLink = By.id("accountSettingMenuLink");
    (new WebDriverWait(webDriver, timeout))
            .until(ExpectedConditions.presenceOfElementLocated(accountSettingsMenuLink));
    WebElement menuItem = webDriver.findElement(accountSettingsMenuLink);
    menuItem.click();

    try {
        boolean lookAgain = false;
        do {
            // Look for each revoke app button
            By revokeAppBtn = By.id("revokeAppBtn");
            (new WebDriverWait(webDriver, timeout))
                    .until(ExpectedConditions.presenceOfElementLocated(revokeAppBtn));
            List<WebElement> appsToRevoke = webDriver.findElements(revokeAppBtn);
            boolean elementFound = false;
            // Iterate on them and delete the ones created by the specified
            // client id
            for (WebElement appElement : appsToRevoke) {
                String nameAttribute = appElement.getAttribute("name");
                if (clientIds.contains(nameAttribute)) {
                    appElement.click();
                    Thread.sleep(1000);
                    // Wait for the revoke button
                    By confirmRevokeAppBtn = By.id("confirmRevokeAppBtn");
                    (new WebDriverWait(webDriver, timeout))
                            .until(ExpectedConditions.presenceOfElementLocated(confirmRevokeAppBtn));
                    WebElement trash = webDriver.findElement(confirmRevokeAppBtn);
                    trash.click();
                    Thread.sleep(2000);
                    elementFound = true;
                    break;
                }
            }

            if (elementFound) {
                lookAgain = true;
            } else {
                lookAgain = false;
            }
        } while (lookAgain);

    } catch (Exception e) {
        // If it fail is because it couldnt find any other application
    } finally {
        webDriver.get(baseUrl + "/userStatus.json?logUserOut=true");
        webDriver.quit();
    }
}

From source file:org.orcid.integration.blackbox.api.v2.rc2.BlackBoxBase.java

License:Open Source License

public static void revokeApplicationsAccess(String... clientIdsParam) {
    // Nothing to remove
    if (clientIdsParam == null) {
        return;/*  w  ww  .  ja va 2s  . c  o  m*/
    }
    List<String> clientIds = new ArrayList<String>();
    for (String clientId : clientIdsParam) {
        if (!PojoUtil.isEmpty(clientId)) {
            clientIds.add(clientId);
        }
    }

    String userName = System.getProperty("org.orcid.web.testUser1.username");
    String password = System.getProperty("org.orcid.web.testUser1.password");
    String baseUrl = "https://localhost:8443/orcid-web";
    if (!PojoUtil.isEmpty(System.getProperty("org.orcid.web.base.url"))) {
        baseUrl = System.getProperty("org.orcid.web.base.url");
    }

    WebDriver webDriver = new FirefoxDriver();

    int timeout = 4;
    webDriver.get(baseUrl + "/userStatus.json?logUserOut=true");
    webDriver.get(baseUrl + "/my-orcid");
    SigninTest.signIn(webDriver, userName, password);

    // Switch to accounts settings page
    By accountSettingsMenuLink = By.id("accountSettingMenuLink");
    (new WebDriverWait(webDriver, timeout))
            .until(ExpectedConditions.presenceOfElementLocated(accountSettingsMenuLink));
    WebElement menuItem = webDriver.findElement(accountSettingsMenuLink);
    menuItem.click();

    try {

        boolean lookAgain = false;

        do {
            // Look for each revoke app button
            By revokeAppBtn = By.id("revokeAppBtn");
            (new WebDriverWait(webDriver, timeout))
                    .until(ExpectedConditions.presenceOfElementLocated(revokeAppBtn));
            List<WebElement> appsToRevoke = webDriver.findElements(revokeAppBtn);
            boolean elementFound = false;
            // Iterate on them and delete the ones created by the specified
            // client id
            for (WebElement appElement : appsToRevoke) {
                String nameAttribute = appElement.getAttribute("name");
                if (clientIds.contains(nameAttribute)) {
                    appElement.click();
                    Thread.sleep(1000);
                    // Wait for the revoke button
                    By confirmRevokeAppBtn = By.id("confirmRevokeAppBtn");
                    (new WebDriverWait(webDriver, timeout))
                            .until(ExpectedConditions.presenceOfElementLocated(confirmRevokeAppBtn));
                    WebElement trash = webDriver.findElement(confirmRevokeAppBtn);
                    trash.click();
                    Thread.sleep(2000);
                    elementFound = true;
                    break;
                }
            }

            if (elementFound) {
                lookAgain = true;
            } else {
                lookAgain = false;
            }

        } while (lookAgain);

    } catch (Exception e) {
        // If it fail is because it couldnt find any other application
    } finally {
        webDriver.get(baseUrl + "/userStatus.json?logUserOut=true");
        webDriver.quit();
    }
}

From source file:org.orcid.integration.blackbox.BlackBoxBase.java

License:Open Source License

public static void revokeApplicationsAccess(String... clientIdsParam) {
    // Nothing to remove
    if (clientIdsParam == null) {
        return;/*from w  ww  . java  2 s  .  c om*/
    }
    List<String> clientIds = new ArrayList<String>();
    for (String clientId : clientIdsParam) {
        if (!PojoUtil.isEmpty(clientId)) {
            clientIds.add(clientId);
        }
    }

    String userName = System.getProperty("org.orcid.web.testUser1.username");
    String password = System.getProperty("org.orcid.web.testUser1.password");
    String baseUrl = "http://localhost:8080/orcid-web";
    if (!PojoUtil.isEmpty(System.getProperty("org.orcid.web.base.url"))) {
        baseUrl = System.getProperty("org.orcid.web.base.url");
    }

    WebDriver webDriver = new FirefoxDriver();

    int timeout = 4;
    webDriver.get(baseUrl + "/userStatus.json?logUserOut=true");
    webDriver.get(baseUrl + "/my-orcid");
    SigninTest.signIn(webDriver, userName, password);

    // Switch to accounts settings page
    By accountSettingsMenuLink = By.id("accountSettingMenuLink");
    (new WebDriverWait(webDriver, timeout))
            .until(ExpectedConditions.presenceOfElementLocated(accountSettingsMenuLink));
    WebElement menuItem = webDriver.findElement(accountSettingsMenuLink);
    menuItem.click();

    try {

        boolean lookAgain = false;

        do {
            // Look for each revoke app button
            By revokeAppBtn = By.id("revokeAppBtn");
            (new WebDriverWait(webDriver, timeout))
                    .until(ExpectedConditions.presenceOfElementLocated(revokeAppBtn));
            List<WebElement> appsToRevoke = webDriver.findElements(revokeAppBtn);
            boolean elementFound = false;
            // Iterate on them and delete the ones created by the specified
            // client id
            for (WebElement appElement : appsToRevoke) {
                String nameAttribute = appElement.getAttribute("name");
                if (clientIds.contains(nameAttribute)) {
                    appElement.click();
                    Thread.sleep(1000);
                    // Wait for the revoke button
                    By confirmRevokeAppBtn = By.id("confirmRevokeAppBtn");
                    (new WebDriverWait(webDriver, timeout))
                            .until(ExpectedConditions.presenceOfElementLocated(confirmRevokeAppBtn));
                    WebElement trash = webDriver.findElement(confirmRevokeAppBtn);
                    trash.click();
                    Thread.sleep(2000);
                    elementFound = true;
                    break;
                }
            }

            if (elementFound) {
                lookAgain = true;
            } else {
                lookAgain = false;
            }

        } while (lookAgain);

    } catch (Exception e) {
        // If it fail is because it couldnt find any other application
    } finally {
        webDriver.get(baseUrl + "/userStatus.json?logUserOut=true");
        webDriver.quit();
    }
}

From source file:org.qe4j.web.OpenWebDriverTest.java

License:Open Source License

@Test
public void multipleInstances() throws IOException {
    Properties properties = getProperties();
    WebDriver driver1 = new OpenWebDriver(properties);
    WebDriver driver2 = new OpenWebDriver(properties);
    Assert.assertNotSame(driver1, driver2, "drivers not the same");
    Assert.assertNotNull(driver1, "driver1 exists");
    Assert.assertNotNull(driver2, "driver2 exists");
    Assert.assertNotSame(driver1, driver2, "different drivers made");
    driver1.get(URL);//  w  w w .ja  v a  2s  .com
    driver2.get(URL2);
    Assert.assertEquals(driver1.getTitle(), URL_TITLE, "driver1 title");
    driver1.quit();
    Assert.assertEquals(driver2.getTitle(), URL2_TITLE, "driver2 title");
}

From source file:org.richfaces.tests.metamer.ftest.extension.attributes.collector.vdl.AttributesCollectorFromVDL.java

License:Open Source License

@Override
public Map<String, List<String>> collectAttributes() {
    WebDriver browser = new FirefoxDriver();
    try {/*from  ww  w .j  a v  a2 s.  co m*/
        browser.get(VDL_DOC_URL_ALLTAGS_NOFRAMES);
        List<WebElement> linksElements = browser.findElements(By.cssSelector("li a"));
        Map<String, List<String>> result = Maps.newHashMap();
        List<String> attributes;
        List<WebElement> tables;
        WebElement correctAttributesTable;
        for (String link : getLinksURLFromElements(linksElements)) {
            browser.get(link);
            attributes = Lists.newArrayList();
            tables = browser.findElements(By.cssSelector(TABLE_OVERVIEW_SUMMARY));
            correctAttributesTable = null;
            for (WebElement table : tables) {
                if (table.findElement(By.cssSelector(CAPTION_SPAN)).getText().toLowerCase()
                        .contains(ATTRIBUTES)) {
                    correctAttributesTable = table;
                    break;
                }
            }
            if (correctAttributesTable != null) {
                for (WebElement attributeElement : correctAttributesTable
                        .findElements(By.cssSelector(TBODY_TR))) {
                    attributes.add(attributeElement.getAttribute(ID));
                }
                result.put(getComponentNameFromActualPage(browser), attributes);
            }
        }
        return result;
    } finally {
        browser.quit();
    }
}

From source file:org.safs.selenium.webdriver.lib.SearchObject.java

License:Open Source License

/**
 * Currently, shutdown will "quit()" every webdriver that is stored here.
 * Ideally, we do NOT want to quit instances that are running attached to a
 * Selenium Server--whether it is remote or local.
 * @throws Throwable//from   w  w w. j a va2  s. co m
 */
public synchronized static void shutdown() throws Throwable {
    lastUsedWD = null;
    WebDriver wd = null;
    String id = null;
    Enumeration<String> keys = webDrivers.keys();
    while (keys.hasMoreElements()) {
        id = keys.nextElement();
        wd = removeWebDriver(id);
        wd.quit();
    }
    webDrivers.clear();
    webDriverStack.clear();
    RemoteDriver.deleteSessionFile();
}

From source file:org.safs.selenium.webdriver.lib.WDLibrary.java

License:Open Source License

/**
 * Start browser//from   ww w . ja v a  2s.  com
 * <p>
 * Expects System Properties 'selenium.host' and 'selenium.port' to be set.<br>
 * Otherwise, defaults to 'localhost' on port '4444'.
 * <p>
 * @param BrowserName - Browser name such as InternetExplorer, Chrome and FireFox.
 * @param Url - Url including http protocol prefix.
 * @param Id - Id or Title of the Browser incase of two instances needs.
 * @param timeout - Implicit time out to be waited before throw exception.
 * @param isRemote - Start interactive testcase development mode.
 * @param extraParameters HashMap<String,Object>, can be used to pass more browser parameters, such as a firefox profile to use.
 * @throws Exception
 */
public static void startBrowser(String BrowserName, String Url, String Id, int timeout, boolean isRemote,
        HashMap<String, Object> extraParameters) throws SeleniumPlusException {
    String debugmsg = StringUtils.debugmsg(WDLibrary.class, "startBrowser");
    //previousDriver is the possible WebDriver with the same ID stored in the Cache.
    WebDriver previousDriver = null;

    String host = System.getProperty(SelectBrowser.SYSTEM_PROPERTY_SELENIUM_HOST);
    if (host == null || host.isEmpty())
        host = SelectBrowser.DEFAULT_SELENIUM_HOST;
    String port = System.getProperty(SelectBrowser.SYSTEM_PROPERTY_SELENIUM_PORT);
    if (port == null || port.isEmpty())
        port = SelectBrowser.DEFAULT_SELENIUM_PORT;

    if (BrowserName == null || BrowserName.length() == 0) {
        BrowserName = System.getProperty(SelectBrowser.SYSTEM_PROPERTY_BROWSER_NAME);
        if (BrowserName == null || BrowserName.length() == 0) {
            BrowserName = SelectBrowser.BROWSER_NAME_FIREFOX;
            System.setProperty(SelectBrowser.SYSTEM_PROPERTY_BROWSER_NAME, BrowserName);
        }
    }
    if (Id == null || Id.equals("")) {
        Id = String.valueOf("".hashCode());
    }

    if (!isRemote) {
        IndependantLog.warn(debugmsg + "attempting to start a local (not remote) browser instance...");
        SelectBrowser sb = new SelectBrowser();
        previousDriver = addWebDriver(Id, sb.getBrowserInstance(BrowserName, extraParameters));
        lastUsedWD.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
        lastUsedWD.manage().window().setSize(new Dimension(1024, 768)); // default window size
        if (Url != null && Url.length() > 0)
            lastUsedWD.get(Url);

    } else {
        IndependantLog.warn(debugmsg + "attempting to start new session on remote server");
        try {
            SelectBrowser sb = new SelectBrowser();
            DesiredCapabilities capabilities = sb.getDesiredCapabilities(BrowserName, extraParameters);
            capabilities.setJavascriptEnabled(true);
            capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
            capabilities.setCapability(RemoteDriver.CAPABILITY_ID, Id); // custom id for session tracking
            capabilities.setCapability(RemoteDriver.CAPABILITY_RECONNECT, false); // custom id
            capabilities.setCapability(RemoteDriver.CAPABILITY_REMOTESERVER, host); // custom id
            //capabilities.setBrowserName(BrowserName); now it set from capabilities
            previousDriver = addWebDriver(Id,
                    new RemoteDriver(new URL("http://" + host + ":" + port + "/wd/hub"), capabilities));
            lastUsedWD.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);

            try {
                //setSize() is not supported by "chromedriver" for "android chrome"
                lastUsedWD.manage().window().setSize(new Dimension(1024, 768)); // default window size
            } catch (Exception e) {
                IndependantLog.warn(debugmsg + StringUtils.debugmsg(e));
            }

            if (Url != null && Url.length() > 0)
                lastUsedWD.get(Url);
        } catch (Throwable t) {
            IndependantLog.warn(debugmsg + "new RemoteDriver failure.  RemoteServer may not be running!");
            IndependantLog.warn(debugmsg + "Caused By: " + t.getClass().getName() + ", " + t.getMessage());
            Throwable c = t;
            do {
                c = c.getCause();
                if (c instanceof Throwable) {
                    IndependantLog
                            .warn(debugmsg + "Caused By: " + c.getClass().getName() + ", " + c.getMessage());
                }
            } while (c != null);
            throw new SeleniumPlusException(t);
        }
    }

    if (previousDriver != null) {
        //Just simply stop duplicate session.
        IndependantLog.warn(debugmsg + "There is a WebDriver previously stored in cache with id '" + Id
                + "', going to stop it.");
        previousDriver.quit();
    }
    //Initialize javascript's variables.
    js_initialize();
}