Example usage for org.openqa.selenium.firefox FirefoxDriver get

List of usage examples for org.openqa.selenium.firefox FirefoxDriver get

Introduction

In this page you can find the example usage for org.openqa.selenium.firefox FirefoxDriver get.

Prototype

@Override
    public void get(String url) 

Source Link

Usage

From source file:sf.wicklet.gwt.client.test.Test01.java

License:Apache License

@Test
public void testHome01() throws IOException {
    final StepWatch timer = new StepWatch(true);
    final FirefoxProfile profile = new FirefoxProfile(firefoxProfileDir);
    profile.setPreference("network.dns.disableIPv6", true);
    // final FirefoxDriver driver = new FirefoxDriver(new FirefoxBinary(firefoxBinary), profile);
    final FirefoxDriver driver = new FirefoxDriver(profile);
    try {/*from w  w  w  .ja  v a 2 s.c  om*/
        if (DEBUG.isDebug()) {
            System.out.println(timer.toString("# Client start"));
        }
        driver.get(BASEURL);
        final String title = driver.getTitle();
        if (DEBUG.isDebug()) {
            System.out.println(timer.toString("Page title is: " + title));
        }
        assertEquals("HomePage", title);
        if (DEBUG.isDebug()) {
            final String text = driver.getPageSource();
            System.out.println(text);
            FileUtil.writeFile(testHome01Html, false, text);
            takeScreenshot(driver, testHome01Png);
        }
        toppanel(driver);
        logindialog(driver);
        menubar(driver);
        stackpanel(driver);
        ajaxpanel(driver);
    } catch (final Throwable e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (DEBUG.isDebug()) {
            System.out.println(timer.toString("# Client done"));
        }
        if (DEBUG.isDebugServer()) {
            System.in.read();
        } else {
            driver.quit();
        }
    }
}

From source file:sf.wicklet.gwt.site.test.firefox.TestEditUser01.java

License:Apache License

@Test
public void testEditUser01() throws LifecycleException, IOException {
    final StepWatch timer = new StepWatch(true);
    final FirefoxProfile profile = new FirefoxProfile(firefoxProfileDir);
    profile.setPreference("network.dns.disableIPv6", true);
    final FirefoxDriver driver = new FirefoxDriver(profile);
    // driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
    try {/*from  w  w w  .  j  a v  a2s  .com*/
        // Check that acess to admin page redirect to https connection.
        debugprint(timer, "# Client start");
        driver.get(BASEURL + "p/admin");
        debugprint(timer.toString("# Client start"));
        assertEquals("Login", driver.getTitle());
        login(driver, USER_ADMIN, PASS_ADMIN);
        assertEquals("Admin", driver.getTitle());
        final String url = driver.getCurrentUrl();
        debugprint("# URL=" + url);
        assertTrue(url.startsWith("https"));
        // Test edit roles
        finder(driver).wait(By.cssSelector("table.zebraTable"), 1) //
                .finder(0).find(By.linkText("edit"), 4).click(0);
        final WebElement dialog = finder(driver).find(By.cssSelector(".gwt-DialogBox"), 1).get(0);
        finder(dialog).find(By.name("username"), 1);
        final WebElement roleselm = finder(dialog).find(By.name("roles"), 1).get(0);
        final String roles = roleselm.getAttribute("value");
        debugprint("# unedited roles=" + roles);
        assertFalse(roles, roles.indexOf("editor") >= 0);
        enterText(roleselm, roles + ",editor");
        finder(dialog).find(By.tagName("button"), 2).filterText("^OK$").click(0);
        TestUtil.assertWithinTimeout(new IPredicate() {
            @SuppressWarnings("synthetic-access")
            @Override
            public boolean ok() {
                final String editedroles = finder(driver)
                        .wait(By.cssSelector("table.zebraTable tr:first-child+tr td"), 3).get(1).getText();
                debugprint("# edited roles: " + editedroles);
                return editedroles.indexOf("editor") >= 0;
            }
        });
        // Test the delete link
        finder(driver).find(By.cssSelector("table.zebraTable tr")).count(5) //
                .find(By.cssSelector("table.zebraTable"), 1) //
                .finder(0).find(By.linkText("delete"), 4).click(3);
        finder(driver).wait(By.cssSelector(".gwt-DialogBox"), 1) //
                .finder(0).find(By.tagName("button"), 2).filterText("^OK$").click(0);
        finder(driver).wait(By.cssSelector("table.zebraTable tr"), 4);
    } finally {
        if (DEBUG.isDebug()) {
            final String text = driver.getPageSource();
            System.out.println(text);
            FileUtil.writeFile(htmlfile("TestEditUser01"), false, text);
            System.out.println(timer.toString("# Client done"));
        }
        if (DEBUG.isDebugServer()) {
            System.in.read();
        } else {
            driver.quit();
        }
    }
}

From source file:sf.wicklet.gwt.site.test.firefox.TestFirefox01.java

License:Apache License

@Test
public void testWiki01() throws IOException {
    final StepWatch timer = new StepWatch(true);
    final FirefoxProfile profile = new FirefoxProfile(firefoxProfileDir);
    profile.setPreference("network.dns.disableIPv6", true);
    final FirefoxDriver driver = new FirefoxDriver(profile);
    // driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
    try {/*ww  w .j a  va2  s . c om*/
        debugprint(timer, "# Client start");
        driver.get(BASEURL);
        final String title = driver.getTitle();
        debugprint(timer, "# Page title is: " + title);
        assertEquals("Wicklet", title);
        findAndClick(driver.findElementById("leftTopPanel"), By.linkText("Projects"));
        // No edit link without login
        findAndCount(driver.findElementById("rightPanel"), By.linkText("Edit"), 0);
        // login
        findAndClick(driver, By.linkText("Login"));
        login(driver, "writer", "writer");
        //
        findAndClick(driver.findElementById("rightPanel"), By.linkText("Edit"));
        waitAndCount(driver, By.cssSelector(".gwt-RichTextToolbar"), 1);
        findAndCount(driver, By.cssSelector("iframe.gwt-RichTextArea"), 1);
        takeSnapshot(driver, htmlfile("TestWiki01"), pngfile("TestWiki01"));
        final String top = driver.getWindowHandle();
        driver.switchTo().frame("richtextarea");
        waitAndCount(driver, By.tagName("h1"), 1);
        driver.switchTo().window(top);
        findAndClick(driver, By.linkText("Projects"));
        waitAndClick(driver.findElementById("rightPanel"), By.linkText("Edit"));
        waitAndCount(driver, By.cssSelector("iframe.gwt-RichTextArea"), 1);
        driver.switchTo().frame("richtextarea");
        final List<WebElement> h3s = waitAndCount(driver, By.tagName("h3"), 7);
        takeSnapshot(driver, htmlfile("TestWiki01RichTextArea"), pngfile("TestWiki01RichTextArea"));
        final String text = h3s.get(4).getText();
        assertEquals("WickletGwtServer", text);
    } finally {
        debugprint(timer, "# Client done");
        if (DEBUG.isDebugServer()) {
            System.in.read();
        } else {
            driver.quit();
        }
    }
}

From source file:sf.wicklet.gwt.site.test.firefox.TestRestart01.java

License:Apache License

private void test() throws IOException {
    final StepWatch timer = new StepWatch(true);
    final FirefoxProfile profile = new FirefoxProfile(new File("../opt/firefox/7x16slsr.default"));
    profile.setPreference("network.dns.disableIPv6", true);
    final FirefoxDriver driver = new FirefoxDriver(profile);
    try {/*from   w w  w  . j  ava2  s .  c  om*/
        if (TestRestart01.DEBUG.isDebug()) {
            System.out.println(timer.toString("# Client start"));
        }
        driver.get("http://localhost:8080/wicklet-site/");
        final String title = driver.getTitle();
        if (TestRestart01.DEBUG.isDebug()) {
            System.out.println(timer.toString("Page title is: " + title));
        }
        Assert.assertEquals("Wicklet", driver.getTitle());
    } finally {
        if (TestRestart01.DEBUG.isDebug()) {
            final String text = driver.getPageSource();
            System.out.println(text);
            FileUtil.writeFile(TestRestart01.testHome01Html, false, text);
            takeScreenshot(driver, TestRestart01.testHome01Png);
            System.out.println(timer.toString("# Client done"));
        }
        if (TestRestart01.DEBUG.isDebugServer()) {
            System.in.read();
        } else {
            driver.quit();
        }
    }
}

From source file:sLinkValidator.RunnableLinkChecker.java

License:Apache License

public void run() {

    long numThreadId = Thread.currentThread().getId();
    String exp_msg = null;/*from w ww .  ja  v  a 2s  . c  o  m*/
    String strPtnProtocol = "https{0,1}://";

    strFname_ok.set("results" + File.separator + "__tmp_" + Long.toString(numThreadId) + "_" + strThreadID
            + "__healthy_links.csv");
    strFname_error.set("results" + File.separator + "__tmp_" + Long.toString(numThreadId) + "_" + strThreadID
            + "__broken_links.csv");
    strFname_externalLinks.set("results" + File.separator + "__tmp_" + Long.toString(numThreadId) + "_"
            + strThreadID + "__external_links.csv");
    strFname_exceptions.set("results" + File.separator + "__tmp_" + Long.toString(numThreadId) + "_"
            + strThreadID + "__exceptions.txt");

    // shared variables from BrokenLinkChecker class.
    String strRootURL = LinkValidator.getRootURL();
    boolean boolOptAny = LinkValidator.getOptAny();
    boolean boolOptVerbose = LinkValidator.getOptVerboseFlg();
    boolean boolOptCapture = LinkValidator.getOptScreenCaptureFlg();
    boolean boolOptSkipElement = LinkValidator.getOptSkipElementFlg();
    boolean boolOptSitemapMode = LinkValidator.getSitemapModeFlg();

    ConcurrentHashMap<String, Integer> visitedLinkMap = LinkValidator.getVisitedLinkMap();
    //ConcurrentLinkedDeque<String> stack = LinkValidator.getStack();
    ConcurrentLinkedDeque<String> deque = LinkValidator.getDeque();
    ConcurrentLinkedDeque<FirefoxDriver> dqBrowserDrivers = LinkValidator.getDQBrowserDrivers();

    FirefoxDriver browserDriver = dqBrowserDrivers.pop();

    try {

        f_out_ok.set(new FileOutputStream(strFname_ok.get()));
        f_out_error.set(new FileOutputStream(strFname_error.get()));
        f_out_externalLinks.set(new FileOutputStream(strFname_externalLinks.get()));
        f_out_exceptions.set(new FileOutputStream(strFname_exceptions.get()));

        if (boolOptVerbose) {
            System.out.println("[Current Target] : " + this.strURL);
        }
        new PrintStream(f_out_ok.get()).println("[Current Target] : " + this.strURL);

        visitedLinkMap.putIfAbsent(this.strURL, 1);

        String url_get = "";

        if (this.uid != "" || this.password != "") {
            url_get = this.strURL.replaceFirst("(" + strPtnProtocol + ")",
                    "$1" + this.uid + ":" + this.password + "@"); // add id and pass to URL (e.g. https{0,1}:// -> https{0,1}://uid:password@ )
        } else {
            url_get = this.strURL;
        }

        browserDriver.get(url_get);

        if (boolOptCapture) {
            // take the screenshot of the browsing page.

            //
            // replace some characters in url to use it as the capture images filename(png).
            //
            //String url_httpTrimed_01 = this.strURL.replaceFirst("^https{0,1}://[^/]+/", "");
            String url_httpTrimed_01 = this.strURL.replaceFirst("^" + strPtnProtocol + "[^/]+/?", ""); //trim protocol and hostname from URL. (e.g. http(s)://hostname/path -> path)
            String url_httpTrimed_02 = url_httpTrimed_01.replaceAll("[/?\"<>|:*]", "_");

            Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
                    .takeScreenshot(browserDriver);
            File f_ScreenShot = new File("results" + File.separator + "screenshot" + File.separator
                    + URLDecoder.decode(url_httpTrimed_02, "UTF-8") + ".png");
            File parentDir = f_ScreenShot.getParentFile();
            if (parentDir != null && !parentDir.exists()) {
                if (!parentDir.mkdirs()) {
                    throw new IOException("error creating results/screenshot directory");
                }
            }
            if (!f_ScreenShot.exists()) {
                ImageIO.write(fpScreenshot.getImage(), "PNG", f_ScreenShot);
            }

        }

        if (!boolOptSkipElement) {

            ArrayList<WebElement> allLinks = findAllLinks(browserDriver, boolOptAny);

            if (boolOptVerbose) {
                System.out.println("Total number of elements found " + allLinks.size());
            }
            new PrintStream(f_out_ok.get()).println("Total number of elements found " + allLinks.size());
            URL objTgtURL = null;

            for (WebElement element : allLinks) {

                try {
                    String strTagName = element.getTagName();
                    String strTgtURL = null;
                    String linkType = "";
                    String linkText = "";
                    String altText = "";

                    if (strTagName.equalsIgnoreCase("a")) {
                        strTgtURL = element.getAttribute("href");
                        linkType = "<a>";
                        linkText = element.getText();
                    } else if (!boolOptSitemapMode && strTagName.equalsIgnoreCase("img")) {
                        strTgtURL = element.getAttribute("src");
                        linkType = "<img>";
                        altText = element.getAttribute("alt");
                    } else if (!boolOptSitemapMode && strTagName.equalsIgnoreCase("link")) {
                        strTgtURL = element.getAttribute("href");
                        linkType = "<link>";
                        linkText = element.getText();
                    }

                    ResponseDataObj respData;

                    if (strTgtURL != null) {
                        String msg = null;
                        //String noUidPwdURL = strTgtURL.replaceFirst( "(https{0,1}://)" + this.uid + ":" + this.password + "@", "$1" );
                        String noUidPwdURL = strTgtURL.replaceFirst(
                                "(" + strPtnProtocol + ")" + this.uid + ":" + this.password + "@", "$1"); // trim uid and password (e.g. https{0,1}://uid:password@ -> https{0,1}://)
                        String noUidPwdURL_decoded = java.net.URLDecoder.decode(noUidPwdURL,
                                StandardCharsets.UTF_8.name());

                        if (visitedLinkMap.containsKey(noUidPwdURL_decoded)) {

                            //msg = this.strURL + "\t" + linkType + "\t" + noUidPwdURL_decoded + "\t" + "(visited)";
                            msg = "\"" + handleDoubleQuoteForCSV(this.strURL) + "\"" + "," + linkType + ","
                                    + handleDoubleQuoteForCSV(noUidPwdURL_decoded) + "," + "(visited)" + ","
                                    + "," + ",";
                            new PrintStream(f_out_ok.get()).println(msg);

                        } else if (strTagName.equalsIgnoreCase("a")
                                && isExternalSite(strRootURL, noUidPwdURL_decoded)) {
                            // external link

                            // msg = this.strURL + "\t" + linkType + "\t" + noUidPwdURL_decoded + "\t" + "(external link)";
                            msg = "\"" + this.strURL + "\"" + "," + linkType + "," + "\""
                                    + handleDoubleQuoteForCSV(noUidPwdURL_decoded) + "\"" + ","
                                    + "(external link)" + "," + "," + ",";
                            new PrintStream(f_out_externalLinks.get()).println(msg);
                            Integer prevCount = (Integer) numExternalLinks.get();
                            numExternalLinks.set(new Integer(prevCount.intValue() + 1));
                        } else {

                            // (Note)
                            // at this moment, objTgtURL is always null because of finally() part.

                            Matcher mtch_no_http = ptn_no_http.matcher(strTgtURL);
                            if (mtch_no_http.find()) // if strTgtURL was relative.
                            {
                                objTgtURL = new URL(new URL(strRootURL), strTgtURL);
                            } else {
                                objTgtURL = new URL(strTgtURL);
                            }

                            respData = isLinkBroken(objTgtURL, uid, password);
                            visitedLinkMap.put(noUidPwdURL_decoded, 1);

                            if ((this.boolRunAsBFSSearch == true)
                                    && !(strTgtURL.contains("mailto:") || strTgtURL.contains("tel:"))
                                    && strTagName.equalsIgnoreCase("a") && !deque.contains(noUidPwdURL_decoded)
                                    && !(strTgtURL.lastIndexOf("#") > strTgtURL.lastIndexOf("/"))
                                    && !(strTgtURL.endsWith(".png") || strTgtURL.endsWith(".jpg")
                                            || strTgtURL.endsWith(".gif"))) { // Do not access to not-A-tag URL via Firefox driver.
                                //stack.push(noUidPwdURL);  // stack
                                deque.addLast(noUidPwdURL_decoded); // queue
                            }

                            /*
                            msg = this.strURL 
                                  + "\t" + linkType
                                  + "\t" + noUidPwdURL_decoded
                                  + "\t" + respData.getRespMsg() 
                                  + "\t" + respData.getRespCode()
                                  + "\t" + altText.replaceAll("\r", "").replaceAll("\n", "")
                                  + "\t" + linkText.replaceAll("\r", "").replaceAll("\n", "");
                            */
                            msg = "\"" + handleDoubleQuoteForCSV(this.strURL) + "\"" + "," + linkType + ","
                                    + "\"" + handleDoubleQuoteForCSV(noUidPwdURL_decoded) + "\"" + ","
                                    + respData.getRespMsg() + "," + respData.getRespCode() + "," + "\""
                                    + altText.replaceAll("\r", "").replaceAll("\n", "").replaceAll("\"", "\"\"")
                                    + "\"" + "," + "\"" + linkText.replaceAll("\r", "").replaceAll("\n", "")
                                            .replaceAll("\"", "\"\"")
                                    + "\"";

                            if (respData.getRespCode() >= 400) {
                                new PrintStream(f_out_error.get()).println(msg);
                                Integer prevCount = (Integer) numInvalidLink.get();
                                numInvalidLink.set(new Integer(prevCount.intValue() + 1));
                            } else {
                                new PrintStream(f_out_ok.get()).println(msg);
                                Integer prevCount = (Integer) numHealthyLink.get();
                                numHealthyLink.set(new Integer(prevCount.intValue() + 1));
                            }
                        }

                        if (boolOptVerbose) {
                            System.out.println(msg);
                        }

                    }

                } catch (UnsupportedEncodingException e) {
                    // not going to happen - value came from JDK's own StandardCharsets
                    // just for noUidPwdURL_decoded in case something wrong happens
                    exp_msg = this.strURL + "\t" + "At attribute : \"" + element.getAttribute("innerHTML")
                            + "\".\t" + "[UnsupportedEncodingException] Message  :  " + e.getMessage();
                    System.out.println(exp_msg);
                    new PrintStream(f_out_exceptions.get()).println(exp_msg);
                    Integer prevCount = (Integer) numExceptions.get();
                    numExceptions.set(new Integer(prevCount.intValue() + 1));

                } catch (Exception exp) {
                    exp_msg = this.strURL + "\t" + "At attribute : \"" + element.getAttribute("innerHTML")
                            + "\".\t" + "Message  :  " + exp.getMessage();
                    System.out.println(exp_msg);
                    new PrintStream(f_out_exceptions.get()).println(exp_msg);
                    Integer prevCount = (Integer) numExceptions.get();
                    numExceptions.set(new Integer(prevCount.intValue() + 1));
                } finally {
                    objTgtURL = null;
                }

            }
        }

    } catch (Exception exp) {
        exp_msg = "[In Main Loop] An Exception occured at page " + this.strURL + " .\tMessage  :  "
                + exp.getMessage();
        System.out.println(exp_msg);
        new PrintStream(f_out_exceptions.get()).println(exp_msg);
        Integer prevCount = (Integer) numExceptions.get();
        numExceptions.set(new Integer(prevCount.intValue() + 1));
    } finally {

        // push back browserdriver to drivers-dequeue to reuse
        dqBrowserDrivers.addLast(browserDriver);

        // add obtained numbers to values in main class.
        LinkValidator.addAndGetNumHealthyLink(numHealthyLink.get());
        LinkValidator.addAndGetNumInvalidLink(numInvalidLink.get());
        LinkValidator.addAndGetNumExternalLinks(numExternalLinks.get());
        LinkValidator.addAndGetNumExceptions(numExceptions.get());

        try {
            f_out_ok.get().close();
            f_out_error.get().close();
            f_out_externalLinks.get().close();
            f_out_exceptions.get().close();
        } catch (IOException exp) {
            exp.printStackTrace();
            exp_msg = "[finally part in Main run()] An Exception occured at page " + this.strURL
                    + " .\tMessage  :  " + exp.getMessage();
            System.out.println(exp_msg);
            new PrintStream(f_out_exceptions.get()).println(exp_msg);
            Integer prevCount = (Integer) numExceptions.get();
            numExceptions.set(new Integer(prevCount.intValue() + 1));
            LinkValidator.addAndGetNumExceptions(numExceptions.get());
        }

        appendAndDeleteTmpFile(LinkValidator.getFStreamOutOk(), strFname_ok.get());
        appendAndDeleteTmpFile(LinkValidator.getFStreamOutError(), strFname_error.get());
        appendAndDeleteTmpFile(LinkValidator.getFStreamOutExternalSites(), strFname_externalLinks.get());
        appendAndDeleteTmpFile(LinkValidator.getFStreamOutExceptions(), strFname_exceptions.get());

    }

}