Before Understanding the browser launching command, we should know what is selenium. Selenium is an open source test automation test suite used for automating web based applications.It enables automation across several browsers, platforms, and computer languages. Using selenium, we can automate the functional tests and easily integrate them with Maven, Jenkins and other bulid automation and continuous integration tools.
Understanding the browser launching command:
As we know that Selenium WebDriver calls the native methods of the different browsers to automate them. Hence, in Selenium we have different WebDrivers for different browsers like - FirefoxDriver for Firefox browser, ChromeDriver for Google Chrome browser, InternetExplorerDriver for Internet Explorer etc. Now we are taking an example of launching Firefox browser and understand the command in detail-
WebDriver driver = new FireFox Driver();
This is the Java implementation of Selenium's browser launcher. Here, 'WebDriver' is an interface, and we're establishing a reference variable 'driver' of type WebDriver that's instantiated using the 'FireFoxDriver' class.
An interface is a collection of variables and methods with no implementation (just the method name and signature). We cannot instantiate objects from interfaces. Hence, the written below line of code is wrong and throws compile time error saying "Cannot instantiate the type WebDriver".
WebDriver driver = new WebDriver();
For instantiation of driver object, we need the classes like FirefoxDriver or ChromeDriver which have implemented the WebDriver interface. In different words, these driver classes have followed the contract of WebDriver by implementing all the methods of the WebDriver interface. Therefore making all the different types of driver classes uniform, following the same protocol.
Please note that we can also create a reference variable of type FirefoxDriver like below code -
FirefoxDriver driver = new FirefoxDriver();
But having a WebDriver reference object will help in multi-browser testing where the same driver object can be used to assign to any of the required browser specific driver.
Launching Firefox Browser:
Firefox is one of the most significantly used browsers in automation.There are Following steps required to launch the firefox browser.
1. Download geckodriver.exe from GeckoDriver Github Release Page. Just make sure to download the right driver file based on your platform and OS version.
2. Set the System Property for "webdriver.gecko.driver" and geckodriver.exe path - System.setProperty("webdriver.gecko.driver","geckodriver.exe path");
3. Code snippet to launch Firefox browser-
public class FirefoxBrowserLaunchDemo { Public static void main(String[] args) { //Creating a driver object referencing WebDriver interface WebDriver driver; // Setting the Webdriver.chrome.driver attribute to the executable's location System.setProperty("webdriver.gecko.driver", "\\geckodriver.exe"); //Intstantiating driver object and launching browser driver = new FirefoxDriver(); //Using get() to open a webpage driver.get("https://www.oodlestechnologies.com"); } }
4.Run the above program as Java Application, Firefox browser will be launching and then website of oodles Technologies will be launching automatically.
Launching Chrome Browser:
For running Chrome browser in Selenium, we will have to set the webdriver.chrome.driver system property to point to a chromeDriver executable file-
1. Download the most recent ChromeDriver binary from Chromium.org's download page and save it to your local system.
2. Set the webdriver.chrome.driver property to the chromeDriver.exe's location as- System.setProperty("webdriver.chrome.driver", "chromeDriver.exe path");
3. Code snippet to launch Chrome browser-
public class ChromeBrowserLaunchDemo { Public static void main(String[] args) { //Creating a driver object referencing WebDriver interface WebDriver driver; // Setting the Webdriver.chrome.driver attribute to the executable's locationSystem.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromeDriver.exe"); //Intstantiating driver object driver = new chromeDriver(); //Using get() to open a webpage driver.get("https://www.oodlestechnologies.com") ; } }
4.Run the above program as Java Application, Chrome Browser will be launching and then website of oodles Technologies will be launching automatically.
Note: In the second part we will learn about Multi browser Testing In Selenium in which the application under test is tested with multiple supported browsers.