private void ExampleTestMethod2() { //First get the current environment from the appSettings section var environment = AppConfig.GetAppSettingsValue("Environment"); //Get project/file dir var projectDir = Libary.GetProjectDir(); var dataFileDir = projectDir + @"\AcceptanceTests\DataFiles\" + environment + "\\"; var fileDir = dataFileDir + "XMLTestFile1.xml"; //Load the filename XmlDocument doc = new XmlDocument(); doc.Load(fileDir); //Find the root tag //The XmlNodeList reads all the nodes = "/Student" //This example has (1)-root node = "/Student" // //XmlNodeList allNodes = doc.DocumentElement.SelectNodes("/Student"); XmlNode root = doc.DocumentElement.SelectSingleNode("/Student"); //Read the file data into program variables var firstname = root.SelectSingleNode("firstname").InnerText.Trim(); var lastname = root.SelectSingleNode("lastname").InnerText.Trim(); var age = Int32.Parse(root.SelectSingleNode("age").InnerText.Trim()); //Read (1)-or more Services var services = root.SelectSingleNode("services").InnerText.Trim(); services = services.Replace("\r\n", " ").Replace(" ", ""); List <String> servicesList = services.Split(',').ToList(); }
public void TestChromeDriver() { //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // projectDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string projectDir = parent.Substring(0,parent.Length - 10); //var driverDir = projectDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; //var driverDir = @"C:\Test\Selenium3.141-Framework(Net)\packages\"; var runDir = Libary.GetProjectDir(); var driverDir = runDir + @"\packages\"; //dir to the Chrome driver IWebDriver browser = new ChromeDriver(driverDir); browser.Url = "http://www.google.com"; //Find the Search text box UI Element IWebElement element = browser.FindElement(By.Name("q")); //Search element.SendKeys("books"); // this sends an Enter to the element element.SendKeys(Keys.Enter); //Wait for page to load //System.Threading.Thread.Sleep(5 * 1000); //Wait 5-sec new WebDriverWait(browser, TimeSpan.FromSeconds(5)).Until( d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete")); //Test if main page displayed element = browser.FindElement(By.Id("main")); browser.Close(); }
//private const string IE_DRIVER_PATH = @"C:\ODE-Projects\ODESeleniumFrameWorkProject\packages\Selenium.WebDriver.2.45.0\lib\net40"; //private const string CHROME_DRIVER_PATH = @"C:\ODE-Projects\ODESeleniumFrameWorkProject\packages\WebDriver.ChromeDriver.26.14.313457.1\tools"; /// <summary> /// Internet Explorer Problems /// One setting has to be changed in Internet Explorer /// in order for Selenium WebDriver to work correctly. /// Your security zones need to have protected mode either /// enabled or disabled – it doesn’t matter which, as long /// as it’s the same for each zone /// /// To achieve this follow these steps: /// Open IE and go to Internet Options. /// Go to the ‘Security’ tab. /// Click on each of the zones, /// i.e. ‘Internet’, ‘Local intranet’, /// ‘Trusted sites’ and ‘Restricted sites’ /// and ensure that the ‘Enable Protected Mode /// (requires restarting Internet Explorer)’ check box /// is checked. /// /// Using WebDriver Implicit Wait Time /// </summary> /// <returns></returns> /// ///Explicit Waits ///An explicit wait is code you define to wait for a certain condition ///to occur before proceeding further in the code. The worst case of this ///is Thread.sleep(), which sets the condition to an exact time period to wait. ///There are some convenience methods provided that help you write code that will ///wait only as long as required. WebDriverWait in combination with ExpectedCondition ///is one way this can be accomplished. /// ///Implicit Waits ///An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time ///when trying to find an element or elements if they are not immediately available. ///The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. /// //************************************************************ // Modify the method to accept an optional parameter // passed in web page defaults = null // if (passed in web page = Null) // then use the variable url = app.config web page value // if (passed in web page != Null) // then ignore the variable url = app.config web page value // and use the passed in web page string //************************************************************ public IWebDriver LaunchBrowser(string webPage = null) { //Update class var browserClosed = false; var url = RunTimeVars.TargetURL; browserType = AppConfig.GetAppSettingsValue("BrowserType").ToUpper(); //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // projectDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string projectDir = parent.Substring(0,parent.Length - 10); //var driverDir = projectDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; //var driverDir = @"C:\Test\Selenium3.141-Framework(Net)\packages\"; runDir = Libary.GetProjectDir(); driverDir = runDir + @"\packages\"; switch (browserType) { case "FIREFOX": // Implement FireFox using geckdriver file //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // driverDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string driverDir = parent.Substring(0,parent.Length - 10); //var driverDir = driverDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; // location of the geckodriver.exe file var driverService = FirefoxDriverService.CreateDefaultService(driverDir); //location of installed firefox.exe driverService.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe"; driverService.HideCommandPromptWindow = true; driverService.SuppressInitialDiagnosticInformation = true; browser = new FirefoxDriver(driverService, new FirefoxOptions(), TimeSpan.FromSeconds(60)); DefaultBrowserSize = browser.Manage().Window.Size; //DefaultBrowserSize(Width, Height) //Width = 1440 Heigth = 780 //set browser Height, Width browserHeight = 980; browserHWidth = 1440; break; case "CHROME": //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // driverDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string driverDir = parent.Substring(0,parent.Length - 10); //var driverDir = driverDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.AddArguments("disable-infobars"); //dir to the Chrome driver //browser = new ChromeDriver(driverDir); browser = new ChromeDriver(driverDir, chromeOptions); DefaultBrowserSize = browser.Manage().Window.Size; //DefaultBrowserSize(Width, Height) //Width = 945 Heigth = 1026 //set browser Height, Width browserHeight = 1026; browserHWidth = 1500; break; case "IE": isError = true; errorString = "IE Driver not Implemented"; break; case "EDGE": // location for MicrosoftWebDriver.exe //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // driverDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string driverDir = parent.Substring(0,parent.Length - 10); //var driverDir = driverDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; //******************************************************************************************** //Member name Value Description //Default 0 Indicates the behavior is not set. //Normal 1 Waits for pages to load and ready state to be 'complete'. //Eager 2 Waits for pages to load and for ready state to be 'interactive' or 'complete'. //None 3 Does not wait for pages to load, returning immediately. //******************************************************************************************** var options = new EdgeOptions(); options.PageLoadStrategy = PageLoadStrategy.Normal; options.UseChromium = true; //options.BinaryLocation = driverDir; //location of Edge driver //browser = new EdgeDriver(driverDir); browser = new EdgeDriver(driverDir, options); //browser = new EdgeDriver(options); //driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10)); DefaultBrowserSize = browser.Manage().Window.Size; //DefaultBrowserSize(Width, Height) //Width = 1236 Heigth = 856 //set browser Height, Width // browserHeight = 865; //browserHWidth = 1236; break; default: browser = new ChromeDriver(driverDir); break; } /**********************************/ // set browser to use ImplicitWait /**********************************/ if (browserType.Equals("EDGE")) { //Set page load timeout to x seconds //browser.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(RunTimeVars.PAGELOADWAIT)); browser.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(RunTimeVars.PAGELOADWAIT); } else { // Initialize autocomplete to wait - Implicit Wait (x)-seconds //browser.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(RunTimeVars.WEBDRIVERWAIT)); browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(RunTimeVars.WEBDRIVERWAIT); //Set page load timeout to x seconds //The SetPageLoadTimeout() does not work //use the driver constructor to set the page load time for all other web drivers //browser.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(RunTimeVars.PAGELOADWAIT)); } //Set Default Browser Size //DefaultBrowserSize(Width, Height) //Width = 945 Heigth = 1026 browser.Manage().Window.Size = new System.Drawing.Size(browserHWidth, browserHeight); //browser.Manage().Window.FullScreen(); DefaultBrowserSize = browser.Manage().Window.Size; //******** Modified to use optional parameters ************** // if(optional parameter = null) // then use the app.config value // else // Use the passed in web page url if (webPage == null) { browser.Navigate().GoToUrl(url); } else { browser.Navigate().GoToUrl(webPage); } if (isError) { throw new Exception(errorString); } else { return(browser); } }
public void TestIEDriver() { //******************************************************************************************************* // IE browser is displayed, but throws an exception Message = "Unable to find element on closed window" // When the test completes, the IE browser is still displayed // so implement try, catch, finally block and search for the IE process# // and call the method process.Kill(); //******************************************************************************************************* try { //Download from web page //http://selenium-release.storage.googleapis.com/index.html?path=3.9/ //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // projectDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string projectDir = parent.Substring(0,parent.Length - 10); //var driverDir = projectDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; //var driverDir = @"C:\Test\Selenium3.141-Framework(Net)\packages\"; var runDir = Libary.GetProjectDir(); var driverDir = runDir + @"\packages\"; InternetExplorerOptions ieOptions = new InternetExplorerOptions(); ieOptions.BrowserAttachTimeout = System.TimeSpan.FromSeconds(60); ieOptions.RequireWindowFocus = true; ieOptions.EnablePersistentHover = true; ieOptions.IgnoreZoomLevel = true; //ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true; IWebDriver browser = new InternetExplorerDriver(driverDir, ieOptions, System.TimeSpan.FromSeconds(60)); browser.Url = "http://www.google.com"; //Find the Search text box UI Element IWebElement element = browser.FindElement(By.Name("q")); //Search element.SendKeys("books"); // this sends an Enter to the element element.SendKeys(Keys.Enter); //Wait for page to load //System.Threading.Thread.Sleep(5 * 1000); //Wait 5-sec new WebDriverWait(browser, TimeSpan.FromSeconds(5)).Until( d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete")); //Test if main page displayed element = browser.FindElement(By.Id("main")); } catch (Exception e) { throw new Exception(e.Message); } finally { //webPage.Close(); Process[] ps = Process.GetProcessesByName("IEXPLORE"); foreach (Process p in ps) { p.Kill(); } } }
public void TestFireFoxDriver() { // Implement FireFox using geckdriver file //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // projectDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string projectDir = parent.Substring(0,parent.Length - 10); //var driverDir = projectDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; //var driverDir = @"C:\Test\Selenium3.141-Framework(Net)\packages\"; var runDir = Libary.GetProjectDir(); var driverDir = runDir + @"\packages\"; // location of the geck0driver.exe file var driverService = FirefoxDriverService.CreateDefaultService(driverDir); //location of installed firefox.exe driverService.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe"; driverService.HideCommandPromptWindow = true; driverService.SuppressInitialDiagnosticInformation = true; IWebDriver browser = new FirefoxDriver(driverService, new FirefoxOptions(), TimeSpan.FromSeconds(60)); browser.Url = "http://www.google.com"; //Find the Search text box UI Element IWebElement element = browser.FindElement(By.Name("q")); //Search element.SendKeys("books"); // this sends an Enter to the element element.SendKeys(Keys.Enter); //Wait for page to load //System.Threading.Thread.Sleep(5 * 1000); //Wait 5-sec new WebDriverWait(browser, TimeSpan.FromSeconds(5)).Until( d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete")); //****************************************************************************** //For what ever reason the document.readyState = "complete" //and yet the next statement element = browser.FindElement(By.Id("main")); //throws an exception, because the Id = main is not displayed //Lets wait using page element is displayed using driver.FindElement //****************************************************************************** //Example wait for page element is displayed using driver.FindElement WebDriverWait wait = new WebDriverWait(browser, TimeSpan.FromSeconds(20)); wait.Until(drv => drv.FindElement(By.Id("main"))); //Test if main page displayed element = browser.FindElement(By.Id("main")); browser.Close(); }
public void TestEdgeDriver() { //*************************************************************************************** // .Net 4.6 and NUnit 3.11.0 changed the way the project path is returned // projectDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; // You now have to use //TestContext.CurrentContext.TestDirectory; //*************************************************************************************** //var parent = TestContext.CurrentContext.TestDirectory; //string projectDir = parent.Substring(0,parent.Length - 10); //var driverDir = projectDir + @"\packages\"; //Add framework packages dir to app.config //var driverDir = @"C:\Test\Selenium3.141 Framework(Net)\packages\"; //var driverDir = @"C:\Test\Selenium3.141-Framework(Net)\packages\"; var runDir = Libary.GetProjectDir(); var driverDir = runDir + @"\packages\"; //******************************************************************************************** //Member name Value Description //Default 0 Indicates the behavior is not set. //Normal 1 Waits for pages to load and ready state to be 'complete'. //Eager 2 Waits for pages to load and for ready state to be 'interactive' or 'complete'. //None 3 Does not wait for pages to load, returning immediately. //******************************************************************************************** var options = new EdgeOptions(); options.PageLoadStrategy = PageLoadStrategy.Normal; options.UseChromium = true; //options.BinaryLocation = driverDir; //location of Edge driver //IWebDriver browser = new EdgeDriver(driverDir); IWebDriver browser = new EdgeDriver(driverDir, options); //IWebDriver browser = new EdgeDriver(options); //driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10)); browser.Url = "http://www.google.com"; //Find the Search text box UI Element IWebElement element = browser.FindElement(By.Name("q")); //Search element.SendKeys("books"); // this sends an Enter to the element element.SendKeys(Keys.Enter); //Wait for page to load //System.Threading.Thread.Sleep(5 * 1000); //Wait 5-sec new WebDriverWait(browser, TimeSpan.FromSeconds(5)).Until( d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete")); //Test if main page displayed element = browser.FindElement(By.Id("main")); browser.Close(); }