1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import java.io.File; import java.nio.file.Paths; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class browserFactory { static WebDriver driver; //Lets clear a method to take browser as a parameter public static WebDriver startBrowser(String browserName, String url) { if(browserName.equalsIgnoreCase("Firefox")) { String ffd = "path to geckodriver.exe"; //System.setProperty("webdriver.gecko.driver", ffd); System.setProperty("webdriver.firefox.marionette", ffd); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.startup.homepage_override.mstone", "ignore"); profile.setPreference("startup.homepage_welcome_url.additional", "about:blank"); capabilities.setCapability("marionette", true); capabilities.setCapability(FirefoxDriver.PROFILE, profile); driver = new FirefoxDriver(capabilities); } else if(browserName.equalsIgnoreCase("Chrome")) { String cd = "path to chromedriver.exe"; System.setProperty("webdriver.chrome.driver", cd); driver=new ChromeDriver(); } else if(browserName.equalsIgnoreCase("IE")) { //Path to IEDriverServer executable String ied = "path to IEDriverServer.exe"; System.setProperty("webdriver.ie.driver", ied); driver=new InternetExplorerDriver(); } else if(browserName.equalsIgnoreCase("Edge")) { //Path to IEDriverServer executable String edged = "path to MicrosoftWebDriver.exe"; System.setProperty("webdriver.edge.driver", edged); driver= new EdgeDriver(); } driver.get(url); driver.manage().window().maximize(); return driver; } } |
WebDriver - Browser Reusable Class
This is how you can create a re-useable class to initiate any browser in Selenium WebDriver. I have created a sample for the most popular browsers:
Subscribe to:
Posts (Atom)
No comments:
Post a Comment