Example usage for org.openqa.selenium WebDriver get

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

Introduction

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

Prototype

void get(String url);

Source Link

Document

Load a new web page in the current browser window.

Usage

From source file:com.perceptron.findjesus.Util.java

License:MIT License

public static void goToRandomArticle(WebDriver browser) {
    String link = browser.findElement(By.linkText("Random article")).getAttribute("href");
    browser.get(link);
}

From source file:com.safeway.app.appcert.smoketester.SmokeTester.java

@Test
public void executeSmokeTest() throws Exception {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.

    System.setProperty("webdriver.chrome.driver", "C:\\Nino\\ChromeWebDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    TestCaseReader tcreader = new TestCaseReader();
    List<TestScriptTemplate> tcl = tcreader.readExcel();

    List<TestScriptTemplate> validatedTestScript = new ArrayList();

    String log_execution = "";
    Iterator<TestScriptTemplate> i = tcl.iterator();
    while (i.hasNext()) {
        TestScriptTemplate testscript = i.next();
        //collect the results
        TestScriptTemplate testexecution = new TestScriptTemplate();

        testexecution.setAppCode(testscript.getAppCode());
        log_execution = log_execution + " Start smoke testing for application code: "
                + testexecution.getAppCode();

        //access the URL
        driver.get(testscript.getAppURL());

        //login if not yet
        if (driver.getCurrentUrl().contains("identity.safeway.com")) {
            //key in userid and password
            WebElement weusername = driver.findElement(By.id("username"));
            //System.out.println("tag:" + weusername.getTagName());
            weusername.sendKeys(testscript.getAppUserID());

            WebElement wepassword = driver.findElement(By.id("password"));
            //System.out.println("tag:" + wepassword.getTagName());
            wepassword.sendKeys(testscript.getAppPassword());

            WebElement weloginform = driver.findElement(By.name("loginData"));
            //System.out.println("tag:" + weloginform.getTagName());
            weloginform.submit();/*  w  w  w. j  ava2 s.co  m*/
        }

        //recoding URL; required so no need to check for nullity
        testexecution.setAppURL(driver.getCurrentUrl());
        log_execution = log_execution + " Current URL: " + driver.getCurrentUrl();

        //recoding title; required so no need to check for nullity
        testexecution.setHomePageTitle(driver.getTitle());
        log_execution = log_execution + " Login Successful";
        log_execution = log_execution + " Page Title: " + driver.getTitle();

        if (isElementExist(testscript.getHomePageElementType(), testscript.getHomePageElement(), driver)) {
            System.out.println("Element match!" + testscript.getHomePageElement());
            log_execution = log_execution + " Home Page Element validation...";
            testexecution.setHomePageElement(testscript.getHomePageElement());
        } else {
            testexecution.setHomePageElement("not found");
        }

        //next page validation
        if (!testscript.getLevel1URL().isEmpty() || !testscript.getLevel1URL().equals("")) {
            //go to next level page
            driver.get(testscript.getLevel1URL());
            log_execution = log_execution + " Next Page validation URL: " + testscript.getLevel1URL();

            testexecution.setLevel1URL(driver.getCurrentUrl());
            System.out.println("execution log: current level 1 URL on set" + testexecution.getLevel1URL());

            if (!testscript.getLevel1PageTitle().isEmpty() || !testscript.getLevel1PageTitle().equals("")) {
                testexecution.setLevel1PageTitle(driver.getTitle());
                log_execution = log_execution + " Next Page title validation: " + driver.getTitle();
            }

            if (isElementExist(testscript.getLevel1ElementType(), testscript.getLevel1Element(), driver)) {
                testexecution.setLevel1Element(testscript.getLevel1Element());
                log_execution = log_execution + " Next Page element validation: "
                        + testscript.getLevel1Element();
            } else {
                testexecution.setLevel1Element("not found");
            }

        }
        testexecution.setLogs(log_execution);
        System.out.println("Execution Log: " + log_execution);
        log_execution = "";
        SmokeTestValidator testvalidator = new SmokeTestValidator(testscript);
        TestScriptTemplate testingresult = testvalidator.getTestResult(testexecution);
        validatedTestScript.add(testingresult);

    }

    tcreader.writetoExcel(validatedTestScript);
    //Close the browser
    driver.quit();
    //return log_execution;
}

From source file:com.saucelabs.sauce_ondemand.driver.SauceOnDemandSPIImpl.java

License:Open Source License

@Override
public WebDriver createWebDriver(SeleniumFactory factory, String browserURL, DesiredCapabilities capabilities) {

    String uri = factory.getUri();
    if (!uri.startsWith(SCHEME))
        return null; // not ours

    uri = uri.substring(SCHEME.length());
    if (!uri.startsWith("?"))
        throw new IllegalArgumentException("Missing '?':" + factory.getUri());

    // massage parameter into JSON format
    Map<String, List<String>> paramMap = populateParameterMap(uri);

    DesiredCapabilities desiredCapabilities;
    if (hasParameter(paramMap, OS) && hasParameter(paramMap, BROWSER)
            && hasParameter(paramMap, BROWSER_VERSION)) {
        String browser = getFirstParameter(paramMap, BROWSER);
        desiredCapabilities = new DesiredCapabilities(capabilities);
        desiredCapabilities.setBrowserName(browser);
        desiredCapabilities.setVersion(getFirstParameter(paramMap, BROWSER_VERSION));
        desiredCapabilities.setCapability(CapabilityType.PLATFORM, getFirstParameter(paramMap, OS));
        if (browser.equals("firefox")) {
            setFirefoxProfile(paramMap, desiredCapabilities);
        }/* w w w .  jav a2 s . co m*/
    } else {
        //use Firefox as a default
        desiredCapabilities = capabilities;
        desiredCapabilities.merge(DesiredCapabilities.firefox());
        setFirefoxProfile(paramMap, desiredCapabilities);
    }
    String host = readPropertyOrEnv(SELENIUM_HOST, DEFAULT_WEBDRIVER_HOST);

    String portAsString = readPropertyOrEnv(SELENIUM_PORT, null);

    if (portAsString == null || portAsString.equals("")) {
        portAsString = DEFAULT_WEBDRIVER_PORT;
    }

    try {
        WebDriver driver = new RemoteWebDriverImpl(
                new URL(MessageFormat.format("http://{2}:{3}@{0}:{1}/wd/hub", host, portAsString,
                        getFirstParameter(paramMap, USERNAME), getFirstParameter(paramMap, ACCESS_KEY))),
                desiredCapabilities,
                new Credential(getFirstParameter(paramMap, USERNAME), getFirstParameter(paramMap, ACCESS_KEY)),
                getFirstParameter(paramMap, "job-name"));
        if (browserURL != null) {
            driver.get(browserURL);
        }
        return driver;
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid URL: " + factory.getUri(), e);
    }
}

From source file:com.saucelabs.selenium.client.factory.impl.DefaultSeleniumSPIImpl.java

License:Open Source License

@Override
public WebDriver createWebDriver(SeleniumFactory factory, String browserURL, DesiredCapabilities capabilities) {
    if (!canHandle(factory.getUri()))
        return null; // doesn't belong to us

    try {//from  w  ww .ja  v  a 2  s .c  o m
        URL url = new URL(factory.getUri());

        // since the browser start command parameter can contain arbitrary character that may
        // potentially interfere with the rules of the URL, allow the user to specify it through a property.
        String browserStartCommand = (String) factory.getProperty("browserStartCommand");
        if (browserStartCommand == null)
            // getPath starts with '/', so trim it off
            // do URL decode, so that arbitrary strings can be passed in.
            browserStartCommand = URLDecoder.decode(url.getPath().substring(1), "UTF-8");

        //todo translate browserStartCommand to DesiredCapabilities

        DesiredCapabilities desiredCapabilities = capabilities;
        desiredCapabilities.merge(DesiredCapabilities.firefox());
        WebDriver driver = new RemoteWebDriver(url, desiredCapabilities);
        if (browserURL != null) {
            driver.get(browserURL);
        }
        return driver;
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid URL: " + factory.getUri(), e);
    } catch (UnsupportedEncodingException e) {
        // impossible
        throw new Error(e);
    }
}

From source file:com.saucelabs.selenium.client.htmlunit.HtmlUnitSPIImpl.java

License:Open Source License

@Override
public WebDriver createWebDriver(SeleniumFactory factory, String browserURL, DesiredCapabilities capabilities) {
    if (canHandle(factory.getUri())) {
        WebDriver driver = new HtmlUnitDriver();
        driver.get(browserURL);
        return driver;
    }/*from  w  ww. j  a va  2s  .c o m*/
    return null;
}

From source file:com.saucelabs.selenium.client.logging.LoggingSeleniumTest.java

License:Open Source License

public void testWebDriver() {
    WebDriver s = SeleniumFactory.createWebDriver("log:embedded-rc:*firefox", "http://www.google.com/");

    LoggingSelenium ls = (LoggingSelenium) s;
    Logger l = Logger.getAnonymousLogger();
    ls.setLogger(l);/*from   w w w. j a  v a  2s.c  o  m*/
    l.addHandler(new Handler() {
        @Override
        public void publish(LogRecord record) {
            logs.add(record);
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    });

    s.get("http://www.google.com/");
    assertEquals("Google", s.getTitle());
    s.quit();

    verifyLog();
}

From source file:com.seleniumtests.it.stubclasses.StubTestClassForListener5.java

License:Apache License

private void startDriver() {
    WebDriver driver = WebUIDriver.getWebDriver();
    driver.get(
            "file:///" + Thread.currentThread().getContextClassLoader().getResource("tu/test.html").getFile());
}

From source file:com.spambot.invisicow.SeleniumDriver.java

public void drive(int opt) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://findtheinvisiblecow.com");

    WebElement body = driver.findElement(By.tagName("body"));
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(body));

    //start game     
    WebElement button = driver.findElement(By.xpath("//div[@id='loader']//button"));
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(button));
    button.click();//from ww w  .  j a v  a 2  s  . c o m

    for (int i = 0; i < opt - 1; i++) {
        //so the user sees something
        Thread.sleep(2500);

        //win game
        if (driver instanceof JavascriptExecutor) {
            ((JavascriptExecutor) driver).executeScript("find.gameStop(true);");
        }

        //again?
        WebElement congrats = driver.findElement(By.id("modal-congratulations"));
        new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(congrats));
        Thread.sleep(1500);
        WebElement again = driver.findElement(By.xpath("//div[@id='modal-congratulations']//button"));
        again.click();
    }

    Thread.sleep(2500);
    //win game one last time
    if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor) driver).executeScript("find.gameStop(true);");
    }

    Thread.sleep(3000);

    JOptionPane.showMessageDialog(null, "Goodbye!");

    driver.quit();
}

From source file:com.spambot.invisicow.SeleniumDriver.java

public void driveExample() {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.
    WebDriver driver = new FirefoxDriver();

    // And now use this to visit Google
    driver.get("http://www.google.com");
    // Alternatively the same thing can be done like this
    // driver.navigate().to("http://www.google.com");

    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));

    // Enter something to search for
    element.sendKeys("Cheese!");

    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();/*from w  w  w .j a va2  s . c om*/

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());

    // Google's search is rendered dynamically with JavaScript.
    // Wait for the page to load, timeout after 10 seconds
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return d.getTitle().toLowerCase().startsWith("cheese!");
        }
    });

    // Should see: "cheese! - Google Search"
    System.out.println("Page title is: " + driver.getTitle());

    //Close the browser
    driver.quit();
}

From source file:com.thoughtworks.inproctester.tests.InProcessHtmlUnitDriverTest.java

License:Apache License

@Test
public void shouldSupportGetAndPostRequests() {

    WebDriver htmlUnitDriver = new InProcessHtmlUnitDriver(httpAppTester);

    htmlUnitDriver.get("http://localhost/contacts/add");

    assertThat(htmlUnitDriver.getTitle(), is("Test Application"));
    assertThat(htmlUnitDriver.findElement(By.tagName("h3")).getText(), is("Contact Details"));
    WebElement contactNameElement = htmlUnitDriver.findElement(By.name("contactName"));

    contactNameElement.clear();/*from   www. ja  va2 s . c o m*/
    contactNameElement.sendKeys("My Contact");

    htmlUnitDriver.findElement(By.tagName("form")).submit();

    assertThat(htmlUnitDriver.getCurrentUrl(), is("http://localhost/contacts/1"));
    assertThat(htmlUnitDriver.findElement(By.name("contactName")).getAttribute("value"), is("My Contact"));

}