selenium_webdriver_test_01.Selenium_Webdriver_Test_01.java Source code

Java tutorial

Introduction

Here is the source code for selenium_webdriver_test_01.Selenium_Webdriver_Test_01.java

Source

/*
* This section here has been automatically added by NetBeans. 
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates and open the template in the editor.
*/

/*
* This is a first test with Selenium.
* The following code is based on code from the following URL:
* http://www.seleniumhq.org/docs/03_webdriver.jsp
* Original comments modified by me, and additional comments added for clarity.
*/

//This is just a test, to check if GitHub integration works.

/*
* This is the command to create a new package. The package has the project name.
* This is naming scheme does not follow formal nomenclature according to Oracle.
*/
package selenium_webdriver_test_01;

/*
* Importing packages so that their classes can be accessed without referring to the full package name.
*/
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 *
 * @author Manolis Labrakis
 */

/*
* This is the name of the main class
*/
public class Selenium_Webdriver_Test_01 {
    public static void main(String[] args) {
        // Creates a new instance of the Firefox driver
        WebDriver driver = new FirefoxDriver();

        // WebDriver.get - Opens a URL in Firefox
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this:
        // driver.navigate().to("http://www.google.com");

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

        // .sendKeys("text") - Sends text to text input element.
        element.sendKeys("Thurse");

        // .submit() - Submits the form. Form is found automatically from element.
        element.submit();

        // System.out.println("text" + some.object()) - Prints something on the NetBeans output window
        // WebDriver.getTitle() - Gets the title of a web 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("thurse");
            }
        });

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

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