public void ShouldBeAbleToSendFileToRemoteServer() { IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection; if (fileDetectionDriver == null) { Assert.Fail("driver does not support file detection. This should not be"); } fileDetectionDriver.FileDetector = new LocalFileDetector(); driver.Url = uploadPage; IWebElement uploadElement = driver.FindElement(By.Id("upload")); uploadElement.SendKeys(testFile.FullName); driver.FindElement(By.Id("go")).Submit(); driver.SwitchTo().Frame("upload_target"); IWebElement body = driver.FindElement(By.XPath("//body")); Assert.IsTrue(LoremIpsumText == body.Text, "Page source is: " + driver.PageSource); driver.SwitchTo().DefaultContent(); uploadElement = driver.FindElement(By.Id("upload")); Console.WriteLine(uploadElement.Text); }
public void ShouldUpload() { Common.GetLocalHostPageContent("ng_upload1.htm"); // NOTE: does not work with Common.GetPageContent("ng_upload1.htm"); IWebElement file = driver.FindElement(By.CssSelector("div[ng-controller = 'myCtrl'] > input[type='file']")); Assert.IsNotNull(file); StringAssert.AreEqualIgnoringCase(file.GetAttribute("file-model"), "myFile"); String localPath = Common.CreateTempFile("lorem ipsum dolor sit amet"); IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection; if (fileDetectionDriver == null) { Assert.Fail("driver does not support file detection. This should not be"); } fileDetectionDriver.FileDetector = new LocalFileDetector(); try { file.SendKeys(localPath); } catch (WebDriverException e) { // the operation has timed out Console.Error.WriteLine(e.Message); } NgWebElement button = ngDriver.FindElement(NgBy.ButtonText("Upload")); button.Click(); NgWebElement ng_file = new NgWebElement(ngDriver, file); Object myFile = ng_file.Evaluate("myFile"); if (myFile != null) { Dictionary <String, Object> result = (Dictionary <String, Object>)myFile; Assert.IsTrue(result.Keys.Contains("name")); Assert.IsTrue(result.Keys.Contains("type")); Assert.IsTrue(result.Keys.Contains("size")); } else { Console.Error.WriteLine("myFile is null"); } String script = "var e = angular.element(arguments[0]); var f = e.scope().myFile; if (f){return f.name} else {return null;}"; try { Object result = ((IJavaScriptExecutor)driver).ExecuteScript(script, ng_file); if (result != null) { Console.Error.WriteLine(result.ToString()); } else { Console.Error.WriteLine("result is null"); } } catch (InvalidOperationException e) { Console.Error.WriteLine(e.Message); } }
//Open desired browser private IWebDriver zOpenBrowser(String Browser) //we dont want user to invoke browser { switch (Browser.ToUpper()) //To hadle any case mistmatch { //case BrowserType.IE: case "IE": // http://stackoverflow.com/questions/14952348/not-able-to-launch-ie-browser-using-selenium2-webdriver-with-java //Please follow above instructions above to setup IE if (Settings.Grid == "Yes") { InternetExplorerOptions options = new InternetExplorerOptions(); options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; options.PlatformName = "windows"; _driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities(), TimeSpan.FromSeconds(600)); IAllowsFileDetection allowsDetection = _driver as IAllowsFileDetection; if (allowsDetection != null) { allowsDetection.FileDetector = new LocalFileDetector(); } } else { InternetExplorerOptions options = new InternetExplorerOptions(); options.IgnoreZoomLevel = true; //options.EnsureCleanSession = true; options.EnableNativeEvents = true; options.EnablePersistentHover = true; options.RequireWindowFocus = false; options.BrowserAttachTimeout = TimeSpan.FromSeconds(120); _driver = new InternetExplorerDriver(options); } break; case "FIREFOX": _driver = new FirefoxDriver(); break; case "CHROME": _driver = new ChromeDriver(); break; case "SAFARI": _driver = new SafariDriver(); break; case "EDGE": _driver = new EdgeDriver(); break; case "OPERA": _driver = new OperaDriver(); break; default: _driver = new ChromeDriver(); break; } return(_driver); }
public static void LocalFileDetect() { IAllowsFileDetection allowsDetection = WebDriver as IAllowsFileDetection; if (allowsDetection != null) { allowsDetection.FileDetector = new LocalFileDetector(); } }
/// <summary> /// Initializes a new instance of the <see cref="PageManager"/> class. /// </summary> /// <param name="capabilities"> /// The capabilities. /// </param> /// <param name="baseUrl"> /// The base url. /// </param> /// <param name="hubUrl"> /// The hub url. /// </param> public PageManager(DesiredCapabilities capabilities, string baseUrl, string hubUrl) { if (string.IsNullOrEmpty(hubUrl) && capabilities.BrowserName.ToLower() != "microsoftedge") { new WebDriverManager().SetupDriver(capabilities.BrowserName); } else { // capabilities.SetCapability("enableVideo", true); capabilities.SetCapability("enableVNC", true); //capabilities.Platform = new Platform(PlatformType.Windows); } this.Driver = WebDriverFactory.GetDriver(hubUrl, capabilities); this.Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20); this.Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30); Exception exc = null; int counter = 0; do { try { this.Driver.Manage().Window.Maximize(); exc = null; } catch (Exception e) { exc = e; Thread.Sleep(1000); } }while (exc != null && counter++ < 10); if (counter >= 10) { throw exc; } IAllowsFileDetection allowsDetection = this.Driver as IAllowsFileDetection; if (allowsDetection != null) { allowsDetection.FileDetector = new LocalFileDetector(); } if (!this.Driver.Url.StartsWith(baseUrl)) { this.Driver.Navigate().GoToUrl(baseUrl); } }
public void FileUpload(string filePath) { this.TestInfo("Uploading File : " + filePath); //IJavaScriptExecutor js = CurrentDriver as IJavaScriptExecutor; //js.ExecuteScript("var myZone, blob, base64Image; myZone = Dropzone.forElement('#receiptDropzone');" + // @"base64Image = '/9j/4AAQSkZJRgABAQEASABIAAD/2wCEAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDIBCQkJDAsMGA0NGDIhHCEyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMv/AABEIABYAFgMBEQACEQEDEQH/xABQAAADAQAAAAAAAAAAAAAAAAAAAQIHEAEBAAAAAAAAAAAAAAAAAAAAEQEBAQEAAAAAAAAAAAAAAAAAAAECEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwDH6jR0BQSBgATQMAD/2Q==';" + // "function base64toBlob(r,e,n){e=e||\"\",n=n||512;for(var t=atob(r),a=[],o=0;o<t.length;o+=n){for(var l=t.slice(o,o+n),h=new Array(l.length),b=0;b<l.length;b++)h[b]=l.charCodeAt(b);var v=new Uint8Array(h);a.push(v)}var c=new Blob(a,{type:e});return c}" + // "blob = base64toBlob(base64Image, 'image / png');" + // "blob.name = 'testfile.png';" + // "myZone.addFile(blob);" //); IAllowsFileDetection allowsDetection = (IAllowsFileDetection)this.CurrentDriver; allowsDetection.FileDetector = new LocalFileDetector(); //this.fileUploader.Click(); this.fileUploader.SendKeys(filePath); }
internal void AddCategory(string p6) { this.CurrentDriver.FindElement(By.Id("ProfileMasterContentHolder_imgbtnAddCategory"), 50).Click(); var popCategoryHeader = this.CurrentDriver.FindElement(By.Id("ProfileMasterContentHolder_lblPopupUpdateCategoryHeader"), 50); if (popCategoryHeader.Text == "Add Category") { var randomSeed = this.GetRandomSeed(); var splitCategory = p6.Split(','); this.txtCategoryName.SendKeys(splitCategory[0] + randomSeed); this.txtColorCode.SendKeys(splitCategory[1]); IAllowsFileDetection allowsDetection = (IAllowsFileDetection)this.CurrentDriver; allowsDetection.FileDetector = new LocalFileDetector(); this.fileUploader.SendKeys(splitCategory[2]); this.CurrentDriver.FindElement(By.Id("ProfileMasterContentHolder_btnUpdateCategorySubmit")).Click(); this.CurrentDriver.FindElement(By.Id("ProfileMasterContentHolder_btnClose"), 50).Click(); } }
public void OpenURL() { if (Settings.Grid == "Yes") { InternetExplorerOptions options = new InternetExplorerOptions(); options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; options.PlatformName = "windows"; _driver2 = new RemoteWebDriver(new Uri("http://192.168.10.108:4444/wd/hub"), options.ToCapabilities(), TimeSpan.FromSeconds(600)); IAllowsFileDetection allowsDetection = _driver2 as IAllowsFileDetection; if (allowsDetection != null) { allowsDetection.FileDetector = new LocalFileDetector(); } } else { _driver2 = new InternetExplorerDriver(); } _driver2.Navigate().GoToUrl(generatedURL); _driver2.WaitForPageLoad(); test.Log(Status.Info, "Generated URL opened " + _driver.Title); }
/// <summary> /// This method is used to initialized webdriver instance /// </summary> /// <param name="Browser"></param> /// <returns></returns> private IWebDriver OpenBrowser(String Browser) { switch (Browser.ToUpper()) { case "IE": if (Settings.Grid == "Yes") { InternetExplorerOptions options = new InternetExplorerOptions(); options.IntroduceInstabilityByIgnoringProtectedModeSettings = true; options.PlatformName = "windows"; _driver = new RemoteWebDriver(new Uri("http://192.168.10.56:4444/wd/hub"), options.ToCapabilities(), TimeSpan.FromSeconds(600)); IAllowsFileDetection allowsDetection = _driver as IAllowsFileDetection; if (allowsDetection != null) { allowsDetection.FileDetector = new LocalFileDetector(); } } else { InternetExplorerOptions options = new InternetExplorerOptions(); options.IgnoreZoomLevel = true; options.EnableNativeEvents = true; options.EnablePersistentHover = true; options.RequireWindowFocus = false; options.BrowserAttachTimeout = TimeSpan.FromSeconds(120); string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase; string finalpth = path.Substring(0, path.LastIndexOf("bin")); string localpath = new Uri(finalpth).LocalPath; _driver = new InternetExplorerDriver(localpath, options, TimeSpan.FromSeconds(140)); } break; case "FIREFOX": _driver = new FirefoxDriver(); break; case "CHROME": { ChromeOptions options = new ChromeOptions(); options.PlatformName = "Windows"; string rootPath = Directory.GetParent(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)).Parent.Parent.FullName; _driver = new ChromeDriver(rootPath + "Demo\\Drivers", options, TimeSpan.FromSeconds(140)); break; } case "SAFARI": _driver = new SafariDriver(); break; case "EDGE": _driver = new EdgeDriver(); break; case "OPERA": _driver = new OperaDriver(); break; default: break; } return(_driver); }
public static IWebDriver Create(BrowserConfiguration executionConfiguration) { ProcessCleanupService.KillAllDriversAndChildProcessesWindows(); DisposeDriverService.TestRunStartTime = DateTime.Now; BrowserConfiguration = executionConfiguration; var wrappedWebDriver = default(IWebDriver); _proxyService = ServicesCollection.Current.Resolve <ProxyService>(); var webDriverProxy = new OpenQA.Selenium.Proxy { HttpProxy = $"http://127.0.0.1:{_proxyService.Port}", SslProxy = $"http://127.0.0.1:{_proxyService.Port}", }; switch (executionConfiguration.ExecutionType) { case ExecutionType.Regular: wrappedWebDriver = InitializeDriverRegularMode(executionConfiguration, webDriverProxy); break; case ExecutionType.Grid: case ExecutionType.SauceLabs: var gridUrl = ConfigurationService.GetSection <WebSettings>().ExecutionSettings.Url; if (gridUrl == null || !Uri.IsWellFormedUriString(gridUrl.ToString(), UriKind.Absolute)) { throw new ArgumentException("To execute your tests in WebDriver Grid mode you need to set the gridUri in the browserSettings file."); } DebuggerPort = GetFreeTcpPort(); if (executionConfiguration.IsLighthouseEnabled && (executionConfiguration.BrowserType.Equals(BrowserType.Chrome) || executionConfiguration.BrowserType.Equals(BrowserType.ChromeHeadless))) { executionConfiguration.DriverOptions.AddArgument("--remote-debugging-address=0.0.0.0"); executionConfiguration.DriverOptions.AddArgument($"--remote-debugging-port={DebuggerPort}"); } Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var options = executionConfiguration.DriverOptions; options.AddLocalStatePreference("browser", new { enabled_labs_experiments = GetExperimentalOptions() }); wrappedWebDriver = new RemoteWebDriver(new Uri(gridUrl), options.ToCapabilities(), TimeSpan.FromSeconds(180)); IAllowsFileDetection allowsDetection = wrappedWebDriver as IAllowsFileDetection; if (allowsDetection != null) { allowsDetection.FileDetector = new LocalFileDetector(); } break; } var gridPageLoadTimeout = ConfigurationService.GetSection <WebSettings>().TimeoutSettings.PageLoadTimeout; wrappedWebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(gridPageLoadTimeout); var gridScriptTimeout = ConfigurationService.GetSection <WebSettings>().TimeoutSettings.ScriptTimeout; wrappedWebDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(gridScriptTimeout); if (executionConfiguration.BrowserType != BrowserType.Edge) { FixDriverCommandExecutionDelay(wrappedWebDriver); ////DriverCommandExecutionService commandExecutionService = new DriverCommandExecutionService((RemoteWebDriver)wrappedWebDriver); ////commandExecutionService.InitializeSendCommand((RemoteWebDriver)wrappedWebDriver); } ChangeWindowSize(executionConfiguration.Size, wrappedWebDriver); return(wrappedWebDriver); }