Launch Browser in Selenium3
How to Launch Browser in Selenium 3 will discuss about how to open different kind of browsers (i.e. Firefox,Chrome,Opera and Edge etc…) using selenium webdriver version 3 and automate the applications. In previous versions of selenium, firefox has different kind of approach to launch and remaining browsers had another kind of approach. But in the latest version of selenium (i.e. Selenium Webdriver 3), all the browsers will be launched almost same way.
In this blog will see how to launch the browsers in Mac and Windows as both have little bit of difference while writing the code for launching the browser.
Mac Machine:
Launch FirefoxBrowser:
To launch firefox browser in selenium 3 we need to download the latest “Mozilla Gecko Driver” and unzip the same and place in “/usr/local/bin” folder. And write the below sample code to launch firefox and navigate to a particular URL.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class LaunchFirefox { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); // This Site is useful to practice basic Selenium driver.get("http://demo.automationtesting.in"); driver.quit(); } }
Launch ChromeBrowser:
To launch chrome browser in selenium 3 we need to download the latest “Chrome Driver” and unzip the same and place in “/usr/local/bin” folder. And write the below sample code to launch chrome and navigate to a particular URL.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class LaunchChrome { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); // This Site is useful to practice basic Selenium driver.get("http://demo.automationtesting.in"); driver.quit(); } }
Launch SafariBrowser:
Launching Safari browser in Selenium 3 is bit different from chrome and firefox as Safari is giving provision to launch the browser in different way. For, this we do not need to download any files. Instead, need to do some configuration in the safari browser. Below are the steps to perform before writing the selenium code:
- We need to enable the Develop menu in the Safari browser. For this need to go to Safari > Preferences. Then go to Advanced tab then check the “Show Develop menu in menu bar” checkbox.
- Tick the “Allow Remote Automation” in Develop menu.
And write the below sample code to launch safari and navigate to a particular URL.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.safari.SafariDriver; public class LaunchSafari { public static void main(String[] args) { WebDriver driver = new SafariDriver(); // This is Site useful to practice basic Selenium driver.get("http://demo.automationtesting.in"); driver.quit(); } }
These are the ways to launch the different types of browsers in Mac Machine.
Windows Machine:
Launch FirefoxBrowser:
To launch firefox browser in selenium 3 we need to download the latest “Mozilla Gecko Driver” and unzip the same and place in any folder which you want. And write the below sample code to launch firefox and navigate to a particular URL.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class LaunchFirefox { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "F:\\Selenium\\Drivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); // This Site is useful to practice basic Selenium driver.get("http://demo.automationtesting.in"); driver.quit(); } }
Launch ChromeBrowser:
To launch chrome browser in selenium 3 we need to download the latest “Chrome Driver” and unzip the same and place in any folder which you want. And write the below sample code to launch chrome and navigate to a particular URL.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class LaunchChrome { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "F:\\Selenium\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // This Site is useful to practice basic Selenium driver.get("http://demo.automationtesting.in"); driver.quit(); } }
Launch EdgeBrowser:
To launch edge browser we need to follow bit different procedure. To get the Edge Driver we need to download the MicrosoftWebDriver from this link. And it will depends upon the Windows 10 OS build number. For this we need follow below steps:
- Go to Start > Settings > System > About and note down the OS Build number.
- Then Download the proper version of the driver from this link
- If the downloaded file is .msi, then install it to get the .exe driver. (Not sure, but for the latest version it will directly download the .exe file).
- Once the MicrosoftWebDriver.exe is downloaded, Then we can use this .exe file to launch the browser.
And write the below sample code to launch Edge and navigate to a particular URL.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class LaunchEdge { public static void main(String[] args) { System.setProperty("webdriver.edge.driver", "F:\\Selenium\\Drivers\\MicrosoftWebDriver.exe"); WebDriver driver = new EdgeDriver(); // This Site is useful to practice basic Selenium driver.get("http://demo.automationtesting.in"); driver.quit(); } }
These are the ways to launch the different types of browsers in Windows Machine.
Please watch the YouTube video for this blog for better understanding.