The code below is all about 3 steps.
import org.openqa.selenium;
import org.openqa.selenium.chrome;
public class WebDriverTest {
public static void main(String args[]) {
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.quit();
}
}
The above 'program' will navigate to the Google homepage, and then close down the browser before completing.
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
The first line tells the system where to find the ChromeDriver
(chromedriver.exe) executable. We then create our driver object by calling the ChromeDriver()
constructor, again we could be calling our constructor here for any browser/platform.
driver.get("http://www.google.com");
This tells our driver to navigate to the specified url: http://www.google.com. The Java WebDriver API provides the get()
method directly on the WebDriver interface, though further navigation methods can be found via the navigate()
method, e.g. driver.navigate.back()
.
Once the page has finished loading we immediately call:
driver.quit();
This tells the driver to close all open browser windows and dispose of the driver object, as we have no other code after this call this effectively ends the program.
driver.close();
Is an instruction (not shown here) to the driver to close only the active window, in this instance as we only have a single window the instructions would cause identical results to calling quit()
.