Example usage for org.openqa.selenium WebDriver manage

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

Introduction

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

Prototype

Options manage();

Source Link

Document

Gets the Option interface

Usage

From source file:without_package.Nw_Class.java

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver",
            "C:/Users/m.manchanda/Desktop/Mohit/Selenium/chromedriver_win32/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("https://google.co.in");
    System.out.println(driver.getTitle());
    WebDriverWait wait = new WebDriverWait(driver, 60);

    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lst-ib")));
    driver.findElement(By.id("lst-ib")).sendKeys("Hi");
    System.out.println(driver.findElement(By.id("lst-ib")).getLocation());
    System.out.println(driver.findElement(By.id("sfdiv")).getLocation());
    /*// w  ww . j  av a  2  s  .  c  o  m
    List username = new List();
    username.add("ebizlicensee10@licensee.com");
    username.add("Bablu");
    Iterator<String> itr = new username.iterator();
    itr.next();
    while(itr.hasNext())
    {
       System.out.println(itr.next());
    }
    */
    System.out.println("Success dude");
    driver.quit();
}

From source file:wsjformtest.Wsjformtest.java

/**
 * @param args the command line arguments
 *///www  .j a v a2 s  .  c  om

public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://wsj_staging:4%b(q$tm@wsj-staging.developspa.com");
    Thread.sleep(3000);
    driver.findElement(By.name("w2lsubmit")).click();

    //Required_Validation();

    driver.findElement(By.name("w2lsubmit")).click();

    Thread.sleep(3000);

    String text = driver.findElement(By.id("sf_first_name"))
            .findElement(By.xpath("./following-sibling::span[@class='error_message']")).getText();

    Assert.assertTrue(text.equals("This field is required."), "working");

    System.out.print("required field validation works\n");

    Thread.sleep(3000);

    driver.findElement(By.id("sf_first_name")).sendKeys("Meet");

    driver.findElement(By.id("sf_email")).sendKeys("meet@2006");
    Thread.sleep(3000);

    WebElement element = driver.findElement(By.name("w2lsubmit"));
    Actions action = new Actions(driver);
    action.moveToElement(element).click().perform();

    Thread.sleep(1500);

    String Emailtext = driver.findElement(By.id("sf_email"))
            .findElement(By.xpath("./following-sibling::span[@class='error_message']")).getText();

    Assert.assertTrue(Emailtext.equals("The email address you entered is not valid."), "working");

    System.out.print("Email Id validation works\n");

    Thread.sleep(3000);

    driver.findElement(By.id("sf_first_name")).sendKeys("Meet");
    driver.findElement(By.id("sf_email")).clear();
    driver.findElement(By.id("sf_email")).sendKeys("meet2006@gmail.com");
    driver.findElement(By.id("sf_phone")).sendKeys("1111111111");
    driver.findElement(By.id("sf_description")).sendKeys("Test Message");

    WebElement element1 = driver.findElement(By.name("w2lsubmit"));
    Actions action1 = new Actions(driver);
    action1.moveToElement(element1).click().perform();

    Thread.sleep(1500);
    String Formtext = driver.findElement(By.id("salesforce_w2l_lead_1"))
            .findElement(By.xpath("./strong[@class='success_message']")).getText();

    Assert.assertTrue(Formtext.equals("Your message has been submitted successfully."), "working");

    System.out.print("Form submit works\n");

}

From source file:xmlparser.XmlConfigReader.java

License:Apache License

public void commonInject(final WebDriver driver, String injection, int delay) {
    if (delay != 0) {
        try {//from  w w w .ja v a2  s.c  o m
            Thread.sleep(delay);
        } catch (InterruptedException e) {
            Debug.printError("Error: " + e.getMessage());
            HaltHandler.quit_nok();
        }
    }

    if (method == 1) {
        String target_url = "";

        try {
            if (GetParamToInject().equals("")) {
                target_url = (GetTargetUrl().endsWith("/") ? GetTargetUrl() : GetTargetUrl() + "/")
                        + URLEncoder.encode(injection, "UTF-8");
            } else
                target_url = getCompleteTargetURL() + "&" + GetParamToInject() + "="
                        + URLEncoder.encode(injection, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            Debug.printError("\nERROR: unable to encode the target URL");
            HaltHandler.quit_nok();
        }

        // handle DoS vectors
        try {
            /*
             * Here follows a simple workaround to make it work in Google Chrome > 21.
             * It seems that starting from Chrome 22, the following code will block the
             * webdriver navigation:
             * 
             * driver.get(targetURL);
             * driver.switchTo().alert();
             * => an exception is launched!  
             *
             * targetURL is a web page containing an alert dialog window, such as
             * <script>alert(1)</script>
             * 
             * Since it works in the case the targeURL is reached from a link, then
             * we can force the browser to produce an injection starting from a link in a data URI web page.
             * 
             * Note that the injection is obviously url-encoded two times! 
             */
            if (Starter.getCurrentBrowser() != null && Starter.getCurrentBrowser().contains("Chrome")
                    && (GetReflectionUrl() == null || GetReflectionUrl().equals(""))) {
                try {
                    driver.get("data:text/html,<a href='" + URLEncoder.encode(target_url, "UTF-8")
                            + "'>go</click>");
                } catch (UnsupportedEncodingException e) {
                    Debug.printError("\nERROR: unable to encode the target URL");
                    HaltHandler.quit_nok();
                }
                driver.findElements(By.xpath("//a")).get(0).click();
            } else
                driver.get(target_url);

        } catch (UnhandledAlertException e) {
            Starter.refreshDriver();
            Debug.printError(
                    "\nERROR: the last injection resulted in a denial of service! Starting a new driver to continue the injection process");
        } catch (WebDriverException e) {
            Starter.refreshDriver();
            Debug.printError(
                    "\nINFO: an alert dialog window was blocking the injection process, starting a new driver...");
        }

        // <reflectionurl> is setted
        if (GetReflectionUrl() != null && !GetReflectionUrl().equals("")) {
            if (Starter.getCurrentBrowser() != null && Starter.getCurrentBrowser().contains("Chrome")) {
                try {
                    driver.get("data:text/html,<a href='" + URLEncoder.encode(target_url, "UTF-8")
                            + "'>go</click>");
                } catch (UnsupportedEncodingException e) {
                    Debug.printError("\nERROR: unable to encode the target URL");
                    HaltHandler.quit_nok();
                }
                driver.findElements(By.xpath("//a")).get(0).click();
            } else
                driver.get(GetReflectionUrl());
        }

    } else if (method == 2) {
        NodeList commands = root.getElementsByTagName("name");
        NodeList targets = root.getElementsByTagName("target");
        NodeList values = root.getElementsByTagName("value");

        if (commands.getLength() == 0 || targets.getLength() == 0 || values.getLength() == 0) {
            Debug.printError("ERROR: malformed <command> tags ( " + xmlFilename + " )");
            HaltHandler.quit_nok();
        } else if (commands.getLength() - Parameters.size() != targets.getLength()
                || commands.getLength() != values.getLength()) {
            Debug.printError("ERROR: malformed <command> tags ( " + xmlFilename + " )");
            HaltHandler.quit_nok();
        }

        for (int i = Parameters.size(); i < commands.getLength(); i++) {

            String name = commands.item(i).getTextContent();
            String target = root.getElementsByTagName("target").item(i - Parameters.size()).getTextContent();
            String value = root.getElementsByTagName("value").item(i).getTextContent();

            WebElement ele = null;

            try {
                if (target.startsWith("id="))
                    ele = driver.findElement(By.id(target.replace("id=", "")));
                else if (target.startsWith("xpath="))
                    ele = driver.findElement(By.xpath(target.replace("xpath=", "")));
                else if (target.startsWith("name="))
                    ele = driver.findElement(By.name(target.replace("name=", "")));
            } catch (Exception e) {
                Debug.printError("\nERROR: unable select the element [" + target + "]");
                Starter.brokenPage();
            }

            if (name.equals("open")) {

                try {
                    if (value.equals("noforce")) {
                        if (!stripFragmentFromCurrentURL(driver).equals(target))
                            driver.get(target);
                    } else {
                        driver.get(target);
                    }
                } catch (UnhandledAlertException e) {
                    Starter.refreshDriver();
                    Debug.printError(
                            "\nERROR: the last injection resulted in a denial of service! Starting a new browser instance to continue the injection process");
                } catch (WebDriverException e) {
                    Starter.refreshDriver();
                    Debug.printError(
                            "\nERROR: the last injection resulted in a denial of service! Starting a new browser instance to continue the injection process");
                }

            } else if (name.equals("type")) {
                try {
                    if (value.startsWith("${") && value.endsWith("}")) {

                        if (value.substring(2, value.length() - 1).equals("RANDOM"))
                            ele.sendKeys("" + (new Random()).nextInt(100000));
                        else if (value.substring(2, value.length() - 1).equals("RANDOM_EMAIL"))
                            ele.sendKeys((new Random()).nextInt(100000) + "@" + (new Random()).nextInt(100000)
                                    + ".org");
                        else if (value.substring(2, value.length() - 1).equals("INJECTION"))
                            ele.sendKeys((injection != null) ? injection : "");
                        else
                            ele.sendKeys(Parameters.get(
                                    value.toLowerCase().replace("$", "").replace("{", "").replace("}", "")));

                    } else if (value.equals("")) {
                        ele.clear();
                    } else
                        ele.sendKeys(value);
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }

            } else if (name.equals("submit")) {
                try {
                    ele.submit();
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }
            } else if (name.equals("deleteCookies")) {
                try {
                    driver.manage().deleteAllCookies();
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }
            } else if (name.equals("click")) {
                try {
                    ele.click();
                } catch (NullPointerException e) {
                    Debug.printError("\nERROR: cannot find the element defined through [" + target + "]");
                    Starter.brokenPage();
                }
            }
        }
    }
}

From source file:zz.pseas.ghost.browser.BrowserFactory.java

License:Apache License

public static CookieStore getCookieStore(WebDriver driver) {
    CookieStore store = new BasicCookieStore();
    Set<Cookie> cs = driver.manage().getCookies();
    System.out.println(cs.size());
    for (Cookie c : cs) {
        BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue());
        if (c.getDomain() == null) {
            System.out.println(c.getName() + "->" + c.getValue());
        } else {/*from  w w  w .j av  a2  s  .  com*/
            System.out.println(c.getDomain());
        }

        c1.setDomain(c.getDomain() == null ? "my.alipay.com" : c.getDomain());
        c1.setPath(c.getPath());
        c1.setExpiryDate(c.getExpiry());
        store.addCookie(c1);
    }
    System.out.println(store.getCookies().size());
    return store;
}

From source file:zz.pseas.ghost.login.taobao.LogInTaobao.java

License:Apache License

@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {

    String tbuserNmae = "TBname";
    String tbpassWord = "TBpasssword";

    String url = "https://login.taobao.com/member/login.jhtml";
    WebDriver ie = BrowserFactory.getIE();
    ie.get(url);/*from   w w  w. j ava2  s.co  m*/

    Thread.sleep(5000L);

    Set<Cookie> cookies = ie.manage().getCookies();
    CookieStore store = DownloadUtil.convertToCookieStore(cookies);

    GhostClient client = new GhostClient("utf-8", store);

    String html = ie.getPageSource();
    String pbk = Jsoup.parse(html).select("input#J_PBK").attr("value");
    String pwd1 = RsaUtil.enCode(pbk, "10001", tbuserNmae);

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("TPL_username", tbuserNmae);
    map.put("TPL_password", tbpassWord);
    map.put("fc", "default");
    map.put("from", "tb");
    map.put("gvfdcname", "10");
    map.put("keyLogin", "false");
    map.put("loginASR", "1");
    map.put("loginASRSuc", "1");
    map.put("loginType", "3");
    map.put("loginsite", "0");
    map.put("naviVer", "firefox|47");

    String ncoToken = Jsoup.parse(html).select("input#J_NcoToken").attr("value");
    map.put("ncoToken", ncoToken);

    map.put("newMini", "false");
    map.put("newMini2", "false");
    map.put("newlogin", "0");
    map.put("osVer", "windows|6.1");
    map.put("oslanguage", "zh-cn");
    map.put("qrLogin", "true");
    map.put("slideCodeShow", "false");
    map.put("sr", "1920*1080");

    String ua = Jsoup.parse(html).select("input#UA_InputId").attr("value");
    //map.put("ua", ua);

    String umToken = Jsoup.parse(html).select("input[name=um_token]").attr("value");
    map.put("um_token", umToken); // TODO get um_token

    String ans = client.post("https://login.taobao.com/member/login.jhtml", map);
    System.out.println(ans);
}

From source file:zz.pseas.ghost.login.weibo.WeiboLogin.java

License:Apache License

public static void main(String[] args) {
    WebDriver driver = BrowserFactory.getIE();

    driver.get("http://weibo.com/");

    Set<Cookie> cookies = driver.manage().getCookies();
    BasicCookieStore cookieStore = new BasicCookieStore();
    for (Cookie c : cookies) {
        BasicClientCookie c1 = new BasicClientCookie(c.getName(), c.getValue());
        c1.setDomain(c.getDomain() == null ? "weibo" : c.getDomain());
        c1.setPath(c.getPath());/*from   w ww  . j a v  a 2  s  .com*/
        Date d = c.getExpiry();
        if (d != null) {
            c1.setExpiryDate(d);
        }
        cookieStore.addCookie(c1);
    }
    driver.quit();

    GhostClient client = new GhostClient("utf-8", cookieStore);
    String url = "http://weibo.com/p/10080813dc27e2acb1441006674c8aa2ef07d4/followlist?page=1#Pl_Core_F4RightUserList__38";
    //HttpGet get = new HttpGet(url);
    String s = client.get(url);
    System.out.println(s);
    String next = StringUtil.regex(s, "(?<=replace\\(\").*?(?=\")");
    String html = client.get(next);
    System.out.println(html);
}