Working with Different Browsers in Selenium
Working with Different Browsers in Selenium
In this article, we will learn how to instantiate different browsers that we commonly test using automation. Code snippets to demonstrate the launch of different browsers using Selenium webdriver.
Firefox Browser
Example code snippet to launch the Firefox browser and open the Twitter website.
package com.testingdocs.selenium.demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.firefox.FirefoxDriver; public class FirefoxDemo { public static void main(String[] args) { FirefoxDriver driver=new FirefoxDriver(); driver.get("https://www.twitter.com"); System.out.println("TestingDocs >> Launched Twitter.com Website"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.quit(); } }
Internet Explorer
Example code snippet to launch Internet Explorer browser and open Facebook website.
package com.testingdocs.selenium.demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.ie.InternetExplorerDriver; public class IEDemo { public static void main(String[] args) { System.setProperty("webdriver.ie.driver","Your_Path_to_IEdriverServer.exe"); InternetExplorerDriver driver=new InternetExplorerDriver(); driver.get("https://www.facebook.com"); System.out.println("TestingDocs >> Launched Facebook Website"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.quit(); } }
Safari Browser
Example code snippet to launch Safari browser and open LinkedIn website.
import java.util.concurrent.TimeUnit; import org.openqa.selenium.safari.SafariDriver; public class SafariDemo { public static void main(String[] args) { SafariDriver driver=new SafariDriver(); driver.get("https://www.linkedin.com"); System.out.println("TestingDocs >> Launched LinkedIn Website"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.quit(); } }
Chrome Browser
Steps to Download Chrome are listed out here:
(https://www.testingdocs.com/questions/how-to-download-chrome-browser-on-windows/)
Example code snippet to launch the Chrome browser and open the MSN website.
package com.testingdocs.selenium.demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeDemo { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","Your_Path_To_chromedriver.exe"); ChromeDriver driver=new ChromeDriver(); driver.get("https://www.msn.com"); System.out.println("TestingDocs >> Launched Google Website"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.quit(); } }