Launching Browsers In Selenium
Launching Browsers In Selenium :
In this article, let us consider a test case in which we will try to automate the following scenarios in the different browsers.
Launch browser.
Maximize the browser.
Open URL: https://www.bookofnetwork.com/
For invoking the chrome browser, we need the Java JDK, Eclipse IDE, Selenium . So firstly install this.
Steps- 1. Open the Eclipse IDE and create a new Java project. Right-click on the “src” folder and create a new Class File from New > Class. Give the Class name and click on the “Finish” button. 2. Add Selenium JAR file into the Java Project. Right-click on Class name and Select “Build Path” and select > configure build path. Then select Libraries > Classpath > and Click “Add External JAR’s”, now add the Selenium Jar and click “Apply and Finish”.
Launching Chrome Browser :
To launch the chrome browser, download the latest ChromeDriver.exe from ChromeDriver Download and place the executable on your local machine.
CODE
public class Launch_Chrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Files\\chromedriver.exe");
// Instantiate a ChromeDriver class.
WebDriver driver = new ChromeDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.bookofnetwork.com/");
}
}
- 1. Set a system property “webdriver.chrome.driver” to the path of your ChromeDriver.exe file : System.setProperty(“webdriver.chrome.driver”,”chromedriver location”);
- 2. instantiate a ChromeDriver class: WebDriver driver = new ChromeDriver();
- 3. Maximize the window: driver.manage().window().maximize();
- 4. To open the URL: driver.get(“URL link”):
Launching FireFox Browser :
To launch the FireFox browser, download the latest geckodriver.exe from GeckoDriver Download . Make sure to download the right driver file based on your platform and OS version.
CODE
public class Launch_Firefox {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\Admin\\Files\\geckodriver.exe");
// Instantiate a FireFoxDriver class.
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.bookofnetwork.com/");
}
}
Launching Internet Explorer Browser :
To launch the Internet Explorer browser, download the latest IEDriverServer.exe from Here .
CODE
public class Launch_IE {
public static void main(String[] args){
System.setProperty("webdriver.ie.driver","C:\\Users\\Admin\\Files\\IEDriverServer.exe");
// Instantiate a InternetExplorerDriver class.
WebDriver driver = new InternetExplorerDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.bookofnetwork.com/");
}
}
Launching Safari Browser :
The Safari browser doesn’t require any additional configuration and can be directly launched by instantiating with SafariDriver.
Prior to Safari automation, enable the Remote Automation feature from the developer menu. To do so, enable the Safari Developer menu first with the steps below:
Go to Safari -> Preferences-> Advanced
Tick mark the Checkbox with the label – Show Develop menu in menu bar
Once done, go to the Develop menu and click on the Allow Remote Automation option to enable it.
Once this is done, users can straightaway get started with the programming part without downloading Safari WebDriver.
CODE
public class Launch_Safari {
public static void main(String[] args){
// Instantiate a SafariDriver class.
WebDriver driver = new SafariDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.bookofnetwork.com/");
}
}