Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Selenium WebDriver Example (Java)

You need a basic example of how to use the the Selenium Webdriver in Java? Here you will find it.

If you are not sure if you have the right set-up to start with the Selenium WebDriver, please read the Selenium WebDriver Tutorial (Java).

  • Sample-Code for Firefox
  • Sample-Code for Chrome
  • Sample-Code for Internet Explorer

 

Selenium Firefox WebDriver Java Sample-Code:

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * @author Nils via frontendtest.org
 */
public class SeleniumTest {

	/**
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		WebDriver webDriver = new FirefoxDriver();

		// Setting the browser size
		webDriver.manage().window().setSize(new Dimension(1024, 768));

		// Go to wikipedia
		webDriver.navigate().to("https://en.wikipedia.org/wiki/Main_Page");

		// Type in the search-field: "WebDriver"
		webDriver.findElement(By.id("searchInput")).sendKeys("WebDriver");

		// submitting the search query
		webDriver.findElement(By.id("searchInput")).submit();

		// Test if Wikipedia redirects to the correct article:
		// "Selenium (software)"
		String textFound = webDriver.findElement(By.cssSelector("h1"))
				.getText();
		if (textFound.contains("Selenium (software)")) {
			System.out.println("Test passes!");
		} else {
			System.out.println("Test fails!");
		}

		// Waiting a little bit before closing
		Thread.sleep(7000);

		// Closing the browser and webdriver
		webDriver.close();
		webDriver.quit();
	}
}

 

Selenium Chrome WebDriver Java Sample-Code:

Important: Before you can use this example, you need adapt the line where the system properties are set for the location of the Chrome Driver.

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author Nils via frontendtest.org
 */
public class SeleniumTest {

	/**
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		// Setting the location of the chrome driver in the system properties
		System.setProperty(
				"webdriver.chrome.driver",
				"C:/your/path/to/the/ChromeDriver/chromedriver.exe");

		WebDriver webDriver = new ChromeDriver();

		// Setting the browser size
		webDriver.manage().window().setSize(new Dimension(1024, 768));

		// Go to wikipedia
		webDriver.navigate().to("https://en.wikipedia.org/wiki/Main_Page");

		// Type in the search-field: "WebDriver"
		webDriver.findElement(By.id("searchInput")).sendKeys("WebDriver");

		// submitting the search query
		webDriver.findElement(By.id("searchInput")).submit();

		// Test if Wikipedia redirects to the correct article:
		// "Selenium (software)"
		String textFound = webDriver.findElement(By.cssSelector("h1"))
				.getText();
		if (textFound.contains("Selenium (software)")) {
			System.out.println("Test passes!");
		} else {
			System.out.println("Test fails!");
		}

		// Waiting a little bit before closing
		Thread.sleep(7000);

		// Closing the browser and webdriver
		webDriver.close();
		webDriver.quit();
	}
}

 

Selenium Internet Explorer WebDriver Java Sample-Code:

Important: Before you can use this example, you need adapt the line where the system properties are set for the location of the Internet Explorer Driver.

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

/**
 * @author Nils via frontendtest.org
 */
public class SeleniumTest {

	/**
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		// Setting the location of the Internet Explorer driver in the system properties
		System.setProperty(
				"webdriver.ie.driver",
				"C:/your/path/to/the/IEDriver/IEDriverServer.exe");

		WebDriver webDriver = new InternetExplorerDriver();

		// Setting the browser size
		webDriver.manage().window().setSize(new Dimension(1024, 768));

		// Go to wikipedia
		webDriver.navigate().to("https://en.wikipedia.org/wiki/Main_Page");

		// Type in the search-field: "WebDriver"
		webDriver.findElement(By.id("searchInput")).sendKeys("WebDriver");

		// submitting the search query
		webDriver.findElement(By.id("searchInput")).submit();

		// Test if Wikipedia redirects to the correct article:
		// "Selenium (software)"
		String textFound = webDriver.findElement(By.cssSelector("h1"))
				.getText();
		if (textFound.contains("Selenium (software)")) {
			System.out.println("Test passes!");
		} else {
			System.out.println("Test fails!");
		}

		// Waiting a little bit before closing
		Thread.sleep(7000);

		// Closing the browser and webdriver
		webDriver.close();
		webDriver.quit();
	}
}



This post first appeared on FRET, please read the originial post: here

Share the post

Selenium WebDriver Example (Java)

×

Subscribe to Fret

Get updates delivered right to your inbox!

Thank you for your subscription

×