internal void Launch(bool mobile, string url = "https://www.bing.com/") { Quit(); _viewModel.ResetProfileCommand.RaiseCanExecuteChanged(); ChromeDriverService service = ChromeDriverService.CreateDefaultService(App.Folder); service.HideCommandPromptWindow = true; ChromeOptions options = new ChromeOptions(); options.AddArgument("start-maximized"); options.AddArgument("user-data-dir=" + App.Folder + "profile"); if (mobile) options.EnableMobileEmulation("Google Nexus 5"); try { _driver = new ChromeDriver(service, options); _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(3)); _builder = new Actions(_driver); LogUpdate("Launching Chrome " + (mobile ? "Mobile" : "Desktop"), Colors.CadetBlue); if (url != null) _driver.Navigate().GoToUrl(url); } catch (Exception ex) { LogUpdate("Error Launching Chrome " + (mobile ? "Mobile" : "Desktop") + "\r\n" + ex.Message, Colors.Red); service.Dispose(); } }
/// <summary> /// Creates and returns ChromeOptions for a mobile device /// </summary> /// <param name="device"></param> /// <returns></returns> public ChromeOptions CreateOptions(string device) { ChromeOptions options = new ChromeOptions(); options.EnableMobileEmulation(device); return options; }
/// <summary> /// Gets the multi browser emulator web driver. /// </summary> /// <param name="testSettings">The test settings.</param> /// <param name="emulator">The emulator.</param> /// <param name="orientation">The device orientation.</param> /// <param name="testOutputHelper">The test output helper.</param> /// <returns></returns> public static ITestWebDriver InitializeMultiBrowserEmulatorDriver(TestSettings testSettings, Emulator emulator, DeviceOrientation orientation, ITestOutputHelper testOutputHelper) { ScreenShotCounter = 0; TestOutputHelper = testOutputHelper; testSettings.BrowserName = emulator + " " + orientation; testSettings = ValidateSavePaths(testSettings); //string driverLocation = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + // "\\MultiBrowser\\Drivers\\ChromeDrivers\\2.20\\chromedriver.exe"; string driverLocation = Path.Combine(AssemblyDirectory, "chromedriver.exe"); driverLocation = ValidateDriverPresentOrUnblocked(WebDriverType.ChromeDriver, driverLocation); var driverService = ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverLocation), Path.GetFileName(driverLocation)); ValidateDriverPresentOrUnblocked(WebDriverType.ChromeDriver, driverLocation); var currentInstallPath = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MultiBrowser", false) ?? Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432node\MultiBrowser", false); string installPathValue = null; if (currentInstallPath != null) { installPathValue = (string) currentInstallPath.GetValue("Path"); } if (installPathValue != null) { if (!installPathValue.EndsWith("\\")) { installPathValue = installPathValue + "\\"; } } #if DEBUG installPathValue = @"C:\Projects\MobileEmulator\bin\Debug\x64\"; #endif var options = new ChromeOptions { LeaveBrowserRunning = false, BinaryLocation = Path.Combine(installPathValue ?? @"C:\Program Files (x86)\MultiBrowser", "MultiBrowser Emulator.exe") }; var emulatorSettings = MultiBrowser.GetMultiBrowserEmulators(emulator); if (orientation == DeviceOrientation.Portrait) { var mobileEmulationSettings = new ChromeMobileEmulationDeviceSettings { UserAgent = emulatorSettings.DeviceUserAgent, Width = emulatorSettings.DeviceWidth, Height = emulatorSettings.DeviceHeight, EnableTouchEvents = true, PixelRatio = emulatorSettings.DevicePixelRatio }; options.EnableMobileEmulation(mobileEmulationSettings); //options.AddAdditionalCapability("mobileEmulation", new //{ // deviceMetrics = new // { // width = emulatorSettings.DeviceWidth, // height = emulatorSettings.DeviceHeight, // pixelRatio = emulatorSettings.DevicePixelRatio // }, // userAgent = emulatorSettings.DeviceUserAgent //}); } else { var mobileEmulationSettings = new ChromeMobileEmulationDeviceSettings { UserAgent = emulatorSettings.DeviceUserAgent, Width = emulatorSettings.DeviceHeight, Height = emulatorSettings.DeviceWidth, EnableTouchEvents = true, PixelRatio = emulatorSettings.DevicePixelRatio }; options.EnableMobileEmulation(mobileEmulationSettings); //options.AddAdditionalCapability("mobileEmulation", new //{ // deviceMetrics = new // { // width = emulatorSettings.DeviceHeight, // height = emulatorSettings.DeviceWidth, // pixelRatio = emulatorSettings.DevicePixelRatio // }, // userAgent = emulatorSettings.DeviceUserAgent //}); } #if DEBUG options.BinaryLocation = @"C:\Projects\MobileEmulator\bin\Debug\x64\MultiBrowser Emulator.exe"; #endif string authServerWhitelist = "auth-server-whitelist=" + testSettings.TestUri.Authority.Replace("www", "*"); string startUrl = "startUrl=" + testSettings.TestUri.AbsoluteUri; string selectedEmulator = "emulator=" + emulatorSettings.EmulatorArgument; var argsToPass = new[] { "test-type", "start-maximized", "no-default-browser-check", "allow-no-sandbox-job", "disable-component-update", "disable-translate", "disable-hang-monitor", authServerWhitelist, startUrl, selectedEmulator }; options.AddArguments(argsToPass); var driver = new ChromeDriver(driverService, options, testSettings.TimeoutTimeSpan); if (testSettings.DeleteAllCookies) { driver.Manage().Cookies.DeleteAllCookies(); } driver.Manage().Timeouts().ImplicitlyWait(testSettings.TimeoutTimeSpan); var extendedWebDriver = new TestWebDriver(driver, testSettings, TestOutputHelper); TestWebDriver = extendedWebDriver; return extendedWebDriver; }