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:main.java.qa.android.util.WaitTool.java

License:Open Source License

/**
  * Wait for the element to be present in the DOM, regardless of being displayed or not.
  * And returns the first WebElement using the given method.
  */*  w  w  w . j av  a2s . com*/
  * @param WebDriver   The driver object to be used 
  * @param By   selector to find the element
  * @param int   The time in seconds to wait until returning a failure
  * 
  * @return WebElement   the first WebElement using the given method, or null (if the timeout is reached)
  */
public static WebElement waitForElementPresent(WebDriver driver, final By by, int timeOutInSeconds) {
    WebElement element;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 

        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        element = wait.until(ExpectedConditions.presenceOfElementLocated(by));

        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return element; //return the element
    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return null;
}

From source file:main.java.qa.android.util.WaitTool.java

License:Open Source License

/**
  * Wait for the List<WebElement> to be present in the DOM, regardless of being displayed or not.
  * Returns all elements within the current page DOM. 
  * //from www.j av  a  2 s.  c om
  * @param WebDriver   The driver object to be used 
  * @param By   selector to find the element
  * @param int   The time in seconds to wait until returning a failure
  *
  * @return List<WebElement> all elements within the current page DOM, or null (if the timeout is reached)
  */
public static List<WebElement> waitForListElementsPresent(WebDriver driver, final By by, int timeOutInSeconds) {
    List<WebElement> elements;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 

        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        wait.until((new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driverObject) {
                return areElementsPresent(driverObject, by);
            }
        }));

        elements = driver.findElements(by);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return elements; //return the element   
    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return null;
}

From source file:main.java.qa.android.util.WaitTool.java

License:Open Source License

/**
  * Wait for an element to appear on the refreshed web-page.
  * And returns the first WebElement using the given method.
  *//  w ww  . j  a v  a  2s.c  o  m
  * This method is to deal with dynamic pages.
  * 
  * Some sites I (Mark) have tested have required a page refresh to add additional elements to the DOM.  
  * Generally you (Chon) wouldn't need to do this in a typical AJAX scenario.
  * 
  * @param WebDriver   The driver object to use to perform this element search
  * @param locator   selector to find the element
  * @param int   The time in seconds to wait until returning a failure
  * 
  * @return WebElement   the first WebElement using the given method, or null(if the timeout is reached)
  * 
  * @author Mark Collin 
  */
public static WebElement waitForElementRefresh(WebDriver driver, final By by, int timeOutInSeconds) {
    WebElement element;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                driverObject.navigate().refresh(); //refresh the page ****************
                return isElementPresentAndDisplay(driverObject, by);
            }
        });
        element = driver.findElement(by);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return element; //return the element
    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return null;
}

From source file:main.java.qa.android.util.WaitTool.java

License:Open Source License

/**
  * Wait for the Text to be present in the given element, regardless of being displayed or not.
  *//from ww w.  j  a  v  a2s . c o  m
  * @param WebDriver   The driver object to be used to wait and find the element
  * @param locator   selector of the given element, which should contain the text
  * @param String   The text we are looking
  * @param int   The time in seconds to wait until returning a failure
  * 
  * @return boolean 
  */

public static boolean waitForTextPresent(WebDriver driver, final By by, final String text,
        int timeOutInSeconds) {
    boolean isPresent = false;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return isTextPresent(driverObject, by, text); //is the Text in the DOM
            }
        });
        isPresent = isTextPresent(driver, by, text);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait

        Log.info("Element found: " + by + " with text " + text);
        Reporter.log("Element found: " + by + " with text " + text);

        return isPresent;

    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return false;
}

From source file:main.java.qa.android.util.WaitTool.java

License:Open Source License

/**
  * Wait for the Text to be present in the given element, regardless of being displayed or not.
  *//from  w w w. jav a2  s.c  o m
  * @param WebDriver   The driver object to be used to wait and find the element
  * @param locator   selector of the given element, which should contain the matched regex expression
  * @param String   The regex we are looking
  * @param Int   The time in seconds to wait until returning a failure
  * 
  * @return boolean 
  */

public static boolean waitForTextPresentRegex(WebDriver driver, final By by, final String regex,
        int timeOutInSeconds) {
    boolean isPresent = false;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return isTextPresentRegex(driverObject, by, regex); //is the Text in the DOM
            }
        });
        isPresent = isTextPresentRegex(driver, by, regex);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait

        Log.info("Element found: " + by + " with text " + regex);
        Reporter.log("Element found: " + by + " with text " + regex);

        return isPresent;

    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return false;
}

From source file:main.java.qa.android.util.WaitTool.java

License:Open Source License

/** 
 * Waits for the Condition of JavaScript.  
 *
 *
 * @param WebDriver      The driver object to be used to wait and find the element
 * @param String   The javaScript condition we are waiting. e.g. "return (xmlhttp.readyState >= 2 && xmlhttp.status == 200)" 
 * @param int   The time in seconds to wait until returning a failure
 * /* w  ww.j a v  a 2 s . c o  m*/
 * @return boolean true or false(condition fail, or if the timeout is reached)
 **/

public static boolean waitForJavaScriptCondition(WebDriver driver, final String javaScript,
        int timeOutInSeconds) {
    boolean jscondition = false;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject).executeScript(javaScript);
            }
        });
        jscondition = (Boolean) ((JavascriptExecutor) driver).executeScript(javaScript);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return jscondition;
    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return false;
}

From source file:main.java.qa.android.util.WaitTool.java

License:Open Source License

/** Waits for the completion of Ajax jQuery processing by checking "return jQuery.active == 0" condition.  
 *
 * @param WebDriver - The driver object to be used to wait and find the element
 * @param int - The time in seconds to wait until returning a failure
 * //from  ww w .j a  v  a 2s .  co m
 * @return boolean true or false(condition fail, or if the timeout is reached)
 * */
public static boolean waitForJQueryProcessing(WebDriver driver, int timeOutInSeconds) {
    boolean jQcondition = false;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject).executeScript("return jQuery.active == 0");
            }
        });
        jQcondition = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0");
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return jQcondition;
    } catch (Exception e) {
        Log.info(e.getMessage());
    }
    return jQcondition;
}

From source file:melihovv.AvitoNewMessageChecker.AvitoNewMessageChecker.java

License:Open Source License

public static void main(String[] args) {
    final Logger log = LogManager.getLogger(AvitoNewMessageChecker.class.getName());

    if (args.length != 1) {
        log.fatal("?    config.yaml");
    }/* www  . j  a  v  a  2  s.c  o m*/

    YamlReader reader = null;
    try {
        reader = new YamlReader(new FileReader(args[0]));
    } catch (FileNotFoundException e) {
        log.fatal("?   config.yaml");
        log.fatal(e.getMessage());
        System.exit(1);
    }

    Map config = null;
    try {
        Object temp = reader.read();
        config = (Map) temp;
    } catch (YamlException e) {
        log.fatal("  ??? config.yaml");
        log.fatal(e.getMessage());
        System.exit(1);
    }

    final List<String> keys = Arrays.asList("avito_login", "avito_pass", "smtp_host", "smtp_port", "smtp_login",
            "smtp_pass", "email_from", "email_to");

    for (final String key : keys) {
        if (!config.containsKey(key)) {
            String values = String.join(", ", keys);
            log.fatal("-    : " + values);
            System.exit(1);
        }
    }

    try {
        final WebDriver driver = new FirefoxDriver();
        final String baseUrl = "https://www.avito.ru/";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get(baseUrl + "profile/login");
        driver.findElement(By.name("login")).clear();
        driver.findElement(By.name("login")).sendKeys(config.get("avito_login").toString());
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys(config.get("avito_pass").toString());
        driver.findElement(By.xpath("//button[@type='submit']")).click();
        driver.findElement(By.cssSelector("#sidebar-nav-messenger > " + "a.link.js-sidebar-menu-link")).click();

        final boolean newMessagesPresent = isElementPresent(driver, By.className("is-design-new"));
        driver.get(baseUrl + "profile/exit");
        driver.quit();

        if (newMessagesPresent) {
            log.info("?  ??");
            sendNotification(config.get("smtp_host").toString(), config.get("smtp_port").toString(),
                    config.get("smtp_login").toString(), config.get("smtp_pass").toString(),
                    config.get("email_from").toString(), config.get("email_to").toString(),
                    "New message on avito.ru", "You have unread messages on avito.ru");
        } else {
            log.info("? ? ");
        }
    } catch (Exception e) {
        log.error("-   ");
        log.error(e.getMessage());
        log.error(Arrays.toString(e.getStackTrace()));
        System.exit(1);
    }
}

From source file:minium.web.config.WebDriverFactory.java

License:Apache License

public WebDriver create(WebDriverProperties webDriverProperties) {
    DesiredCapabilities desiredCapabilities = new DesiredCapabilities(
            webDriverProperties.getDesiredCapabilities());
    DesiredCapabilities requiredCapabilities = new DesiredCapabilities(
            webDriverProperties.getRequiredCapabilities());
    WebDriver webDriver = null;
    if (webDriverProperties.getUrl() != null) {
        RemoteWebDriver remoteDriver = new RemoteWebDriver(webDriverProperties.getUrl(), desiredCapabilities,
                requiredCapabilities);/*  w w w.  ja  v a2s. com*/
        remoteDriver.setFileDetector(new LocalFileDetector());
        webDriver = remoteDriver;
    } else {
        String browserName = desiredCapabilities == null ? null : desiredCapabilities.getBrowserName();
        if (Strings.isNullOrEmpty(browserName))
            browserName = BrowserType.CHROME;
        webDriver = WebDriverType.typeFor(browserName).create(this, desiredCapabilities, requiredCapabilities);
    }
    WindowProperties window = webDriverProperties.getWindow();
    if (window != null) {
        DimensionProperties size = window.getSize();
        PointProperties position = window.getPosition();
        if (size != null)
            webDriver.manage().window().setSize(new Dimension(size.getWidth(), size.getHeight()));
        if (position != null)
            webDriver.manage().window().setPosition(new Point(position.getX(), position.getY()));
        if (window.isMaximized()) {
            webDriver.manage().window().maximize();
        }
    }
    return webDriver instanceof TakesScreenshot ? webDriver : new Augmenter().augment(webDriver);
}

From source file:net.atf4j.webdriver.BrowserFactory.java

License:Open Source License

/**
 * Initialise web driver.//  w ww  . j av  a 2 s  . c  om
 *
 * @param webDriver the web driver
 */
private static void initialiseWebDriver(final WebDriver webDriver) {
    assumeNotNull(webDriver);
    final Options manage = webDriver.manage();
    final Timeouts timeouts = manage.timeouts();
    timeouts.pageLoadTimeout(BrowserFactory.config.pageLoadTimeout(), TimeUnit.SECONDS);
    timeouts.implicitlyWait(BrowserFactory.config.implicitWait(), TimeUnit.SECONDS);
    timeouts.setScriptTimeout(BrowserFactory.config.scriptTimeout(), TimeUnit.MILLISECONDS);
}