![]() |
Selenium comes with different types of web drivers for different browsers. For example, firefox browser comes with Firefox Driver, Internet Explorer comes with InternetExplorerDriver and Google Chrome comes with ChroemDriver, etc.
Selenium WebDriver commands to Launch browsers:
As you all are aware of, the Selenium WebDriver calls native methods of different browsers in order to invoke the automating scripting. So we have the different web drivers for different browsers like FirefoxDriver for Firefox browser, InternetExplorerDriver for IE, etc.
Now let us consider the example of firefox browser.The command looks like as below:
WebDriver driver=new FirefoxDriver();
The above mentioned command is the java implementation of launching a browser in selenium.
In the above command WebDriver acts as an interface and driver acts as an reference variable for the type WebDriver, that are instantiated for the FireFoxDriver class.
Now let us consider the below command and see how different it is from the above mentioned command.
WebDriver driver=new WebDriver();
As we know that an interface is a set of variables and methods without any body implementation. Here we cannot instantiate objects from interfaces, and the above command shows the compile time error.
So for instantiating the driver objects , we need classes like FirefoxDriver or ChromeDriver which have implemented the WebDriver interface.
Become a master of Selenium by going through this HKR Selenium Training!
Firefox serves as the most prominent browser in doing automation and the following steps are required to launch the firefox browser.
Code Snippet to launch the firefox browser
package testPackage;
import org.openqa.selenium.WebDriver;
public class FirefoxBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting webdriver.gecko.driver property
System.setProperty("webdriver.gecko.driver", "{path to geckodriver}\\geckodriver.exe");
//Instantiating driver object and launching browser
driver = new FirefoxDriver();
//Using get() method to open a webpage
driver.get("https://hkrtrainings.com/");
//Closing the browser
driver.quit();
}
}
In order to run the test just do right-click on Test.java file on the Package Explorer section, hover over “Run As” and select “Java Application”. Firefox browser will launch and open hkrtrainings.com
Related Blogs Selenium Grid Setup !
For running the Chrome browser in Selenium, we need to set the webdriver.chrome.driver system property to point to a chrome driver executable file-
Download the latest ChromeDriver binary from Chromium.org download page and place the executable on your local machine.
Set the webdriver.chrome.driver property to the chromeDriver.exe’s location as-System.setProperty(“webdriver.chrome.driver”, “chromeDriver.exe path”);
Code snippet to launch the 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 property to its executable's location
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");
//Instantiating driver object
driver = new ChromeDriver();
//Using get() method to open a webpage
driver.get("https://artoftesting.com");
//Closing the browser
driver.quit();
}
}
InternetExplorer driver also requires setting up the “webdriver.ie.driver” property with the location of IEDriverServer.exe. The IEDriverServer.exe can be downloaded from here.
Following code, snippet can be used to launch IE browser
Related Blogs Selenium Webdriver Installation !
public class IEBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.ie.driver property to its executable's location
System.setProperty("webdriver.ie.driver", "/lib/IEDriverServer/IEDriverServer.exe");
//Instantiating driver object
driver = new InternetExplorerDriver();
//Using get() method to open a webpage
driver.get("https://artoftesting.com");
//Closing the browser
driver.quit();
}
}
Safari browser canan be directly launched by instantiating with SafariDriver. The following code snippet can be used to launch the Safari browser-
public class SafariBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Instantiating driver object with SafariDriver
driver = new SafariDriver();
//Using get() method to open a webpage
driver.get("https://artoftesting.com");
//Closing the browser
driver.quit();
}
}
Conclusion
In the blog content, I had covered how selenium webdriver commands used to launch different browsers. If you had any doubts regarding it please drop the message in the comments section. For more information regarding selenium simply click here for complete Selenium Tutorial.
Related Articles :
As a senior technical content writer for HRK tainings, srivalli patchava has a greater understanding of today's data-driven environment, which includes key aspects of data management and IT organizations. She manages the task of creating great content in the areas of software testing, DevOps, Robotic process automation. Connects with her on Linkedin and Twitter.
Batch starts on 11th Dec 2023 |
|
||
Batch starts on 15th Dec 2023 |
|
||
Batch starts on 19th Dec 2023 |
|