|
| 1 | +package helper; |
| 2 | + |
| 3 | +import io.appium.java_client.remote.MobileCapabilityType; |
| 4 | +import org.openqa.selenium.WebDriver; |
| 5 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 6 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 7 | +import org.openqa.selenium.firefox.FirefoxProfile; |
| 8 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 9 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 10 | +import org.testng.Reporter; |
| 11 | +import pages.HomePage; |
| 12 | + |
| 13 | +import java.net.MalformedURLException; |
| 14 | +import java.net.URL; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +/** |
| 18 | + * Created by opantsjoha on 02/07/2017. |
| 19 | + */ |
| 20 | +public class Browser { |
| 21 | + |
| 22 | + |
| 23 | + public Browser(String browserName, String port, String baseUrl) { |
| 24 | + Reporter.log("Create new instance of the browser", true); |
| 25 | + setBrowser(browserName); |
| 26 | + setBaseUrl(baseUrl); |
| 27 | + Initialise(getBrowser()); |
| 28 | + } |
| 29 | + |
| 30 | + private void Initialise(String browser) { |
| 31 | + capabilities = new DesiredCapabilities(); |
| 32 | + seleniumFolderPath = System.getProperty("user.home") + "/Documents/services/selenium3/"; |
| 33 | + |
| 34 | + switch (browser) { |
| 35 | + case "Chrome": |
| 36 | + ChromeOptions chrome_options = new ChromeOptions(); |
| 37 | + chrome_options.addArguments("--disable-geolocation"); |
| 38 | + chrome_options.addArguments("--incognito"); |
| 39 | + System.setProperty("webdriver.chrome.driver", seleniumFolderPath + "chromedriver"); |
| 40 | + capabilities.setBrowserName("chrome"); |
| 41 | + capabilities.setCapability(ChromeOptions.CAPABILITY, chrome_options); |
| 42 | +// _driver = new ChromeDriver(capabilities); |
| 43 | + break; |
| 44 | + case "Safari": |
| 45 | + capabilities.setBrowserName("safari"); |
| 46 | + |
| 47 | +// _driver = new SafariDriver(); |
| 48 | + break; |
| 49 | + case "Firefox": |
| 50 | + FirefoxProfile ff_profile = new FirefoxProfile(); |
| 51 | + ff_profile.setPreference("geo.prompt.testing", true); |
| 52 | + ff_profile.setPreference("geo.prompt.testing.allow", true); |
| 53 | + System.setProperty("webdriver.gecko.driver", seleniumFolderPath + "geckodriver"); |
| 54 | + capabilities.setBrowserName("firefox"); |
| 55 | + capabilities.setCapability(FirefoxDriver.PROFILE, ff_profile); |
| 56 | + |
| 57 | +// _driver = new FirefoxDriver(capabilities); |
| 58 | + break; |
| 59 | + case "InternetExplorer": |
| 60 | + System.setProperty("webdriver.ie.driver", seleniumFolderPath + "IEDriverServer.exe"); |
| 61 | + capabilities.setBrowserName("internet explorer"); |
| 62 | + break; |
| 63 | + case "Edge": |
| 64 | + System.setProperty("webdriver.edge.driver", seleniumFolderPath + "MicrosoftWebDriver.exe"); |
| 65 | + capabilities.setBrowserName("edge"); |
| 66 | + break; |
| 67 | + case "Safari_iOS": |
| 68 | + capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 5"); |
| 69 | + capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "1.6.5"); |
| 70 | + capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); |
| 71 | + capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest"); |
| 72 | + capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari"); |
| 73 | + break; |
| 74 | + default: |
| 75 | + System.out.println("Invalid browser passed in: " + browser); |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + Reporter.log("Create new instance of remote web driver", true); |
| 81 | + _driver = new RemoteWebDriver(new URL("http://0.0.0.0:4444/wd/hub"), capabilities); |
| 82 | + } catch (MalformedURLException e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + _driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); |
| 86 | + } |
| 87 | + |
| 88 | + public void navigateTo(String url) { |
| 89 | + _driver.get(url); |
| 90 | + } |
| 91 | + |
| 92 | + public void navigateToBaseUrl() { |
| 93 | + _driver.get(getBaseUrl()); |
| 94 | + } |
| 95 | + |
| 96 | + public String getBrowser() { |
| 97 | + return this.browserName; |
| 98 | + } |
| 99 | + |
| 100 | + private void setBrowser(String browserName) { |
| 101 | + this.browserName = browserName; |
| 102 | + } |
| 103 | + |
| 104 | + private void setBaseUrl(String baseUrl) { |
| 105 | + this.baseUrl = baseUrl; |
| 106 | + } |
| 107 | + |
| 108 | + public String getBaseUrl() { |
| 109 | + return this.baseUrl; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + // All Page Objects are created here... |
| 114 | + public HomePage HomePage() { |
| 115 | + if (homePage == null) { |
| 116 | + homePage = new HomePage(this); |
| 117 | + } |
| 118 | + return homePage; |
| 119 | + } |
| 120 | + |
| 121 | + // Public properties |
| 122 | + public WebDriver _driver; |
| 123 | + |
| 124 | + // Private properties |
| 125 | + private DesiredCapabilities capabilities; |
| 126 | + private String browserName; |
| 127 | + private String baseUrl; |
| 128 | + private String seleniumFolderPath; |
| 129 | + private HomePage homePage; |
| 130 | +} |
0 commit comments