Versions used while doing this blog post, (These are the prerequisites to automate firefox browser. Browser and driver versions should match)
selenium-java version – 3.141.59
firefox browser version – 74.0.1 (64-bit)
gecko driver version – 0.26.0 (Download link : https://github.com/mozilla/geckodriver/releases)
Firefox browser can be opened using selenium by following below steps:
1. Map the path of the downloaded geckodriver.exe file to ‘webdriver.gecko.driver’,
System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"\\src\\main\\resources\\geckodriver.exe");
2. Instantiate (creation of object) driver object with FirefoxDriver() class (This step opens the firefox browser)
WebDriver driver = new FirefoxDriver();
3. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected website can be carried out
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.google.com");
4. Quit/close the driver object after automating the necessary flows within the website.
Sample code for reference
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class LaunchFFTest {
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"\\src\\main\\resources\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.google.com");
driver.findElement(By.name("q")).sendKeys("Test");
driver.quit();
}
}