public void AssertSWATVersionEqual()
        {
            _browser = new WebBrowser(BrowserType.Null);
            string currVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(2);

            _browser.AssertSWATVersionNumber(currVersion);
        }
        public string RunScriptSaveResult(string language, string theScript, WebBrowser browser, string assems)
        {           
            string result = RunCode(theScript, browser, assems);

            if (String.IsNullOrEmpty(result))
                throw new ArgumentException("Code yielded no results.");
            return result;            
        }
 protected abstract string RunCode(string source, WebBrowser browser, string assems);
 public void RunScript(string language, string theScript, string expectedResult, WebBrowser browser, string assems)
 {            
     string result = RunCode(theScript, browser, assems);
     if (result != expectedResult) throw new IndexOutOfRangeException("Mismatching results. Expected: " + expectedResult + " , - Actual: " + result);
 }
 public ComparisonTestFixture()
 {
     _browser = new WebBrowser(BrowserType.FireFox);
 }
 public virtual void insertTestRecord()
 {
     using (WebBrowser testBrowser = new WebBrowser(BrowserType.InternetExplorer))
     {
         testBrowser.ConnectToMssql("localhost", "swat", "swat");
         testBrowser.SetDatabase(localDbName1);
         System.Threading.Thread.Sleep(1000);
         testBrowser.InsertIntoTable("Insert into test1(name) values('steve')");
         testBrowser.Dispose();
     }
 }
        public void TestIEHighlightElementsAsTestsRun(bool passedIn)
        {
            bool userDefault = SWAT.IESettings.HighlightElementsAsTestsRun;

            SWAT.IESettings.HighlightElementsAsTestsRun = passedIn;
            SWAT.UserConfigHandler.Save();

            WebBrowser wb = new WebBrowser(BrowserType.InternetExplorer);

            wb.OpenBrowser();
            wb.NavigateBrowser(String.Format("http://{0}/swat//{1}", Environment.MachineName, "TestPage.htm"));
            wb.SetElementAttribute(IdentifierType.Id, "txtOne", "value", "highlightTest");
            wb.StimulateElement(IdentifierType.Id, "btnClear", "onclick");
            wb.AssertElementExists(IdentifierType.Expression, "id:txtOne;value=", "input");
            wb.StimulateElement(IdentifierType.Id, "btnSetVal", "onclick");
            wb.AssertElementExists(IdentifierType.Expression, "id:txtOne;value=Test1", "input");
            wb.CloseBrowser();

            SWAT.IESettings.HighlightElementsAsTestsRun = userDefault;
            SWAT.UserConfigHandler.Save();
        }
        public void TestIEHighlightSetPropertyFails()
        {
            bool userDefault = SWAT.IESettings.HighlightElementsAsTestsRun;
            string input = "man";
            string exception = "";

            SWAT.IESettings.HighlightElementsAsTestsRun= true;
            SWAT.UserConfigHandler.Save();
            Assert.IsTrue(SWAT.IESettings.HighlightElementsAsTestsRun);

            
            WebBrowser wb = new WebBrowser(BrowserType.InternetExplorer);

            try
            {                
                wb.SetConfigurationItem("HighlightElementsAsTestsRun", input);                
            }
            catch (ConfigurationItemException e)
            {
                exception = e.Message;
            }
            Assert.AreEqual(exception, String.Format("Invalid value for this setting: {0}", input)); 
                      
            SWAT.IESettings.HighlightElementsAsTestsRun = userDefault;
            SWAT.UserConfigHandler.Save();
        }
 public PSRCommandTests()
 {
     _browser = new WebBrowser(BrowserType.InternetExplorer);
 }
        public void CloseBrowsersBeforeTestStartTest(bool closeAll)
        {
            if (!(_browserType == BrowserType.Safari))
            {
                // Save the value to reset it later
                bool reset = SWAT.WantCloseBrowsersBeforeTestStart.CloseBrowsersBeforeTestStart;

                // Get name of the process of the browser
                string browserName = GetBrowserName();

                // Boolean used for the assertions
                bool areAllClosed;

                // Open multiple windows to close
                _browser.OpenBrowser();
                _browser.NavigateBrowser("www.google.com");
                _browser.OpenBrowser();
                _browser.NavigateBrowser("www.w3schools.com");

                // Set the option to close or not when the test starts
                SWAT.WantCloseBrowsersBeforeTestStart.CloseBrowsersBeforeTestStart = closeAll;
                SWAT.UserConfigHandler.Save();

                // Command extractor's constructor should not close any open browsers
                SWAT_Editor.CommandExtractor extractor = new SWAT_Editor.CommandExtractor(_browserType);
                
                // Check if there are any browsers open
                areAllClosed = (Process.GetProcessesByName(browserName).Length == 0);

                try
                {
                    if (closeAll)
                    {
                        // Assert that all the web browsers have been closed
                        Assert.IsTrue(areAllClosed, "CloseBrowsersBeforeTestStart failed: " +
                                        "closed all windows when the user setting was turned off.");
                    }
                    else
                    {
                        // Assert that all the web browsers have not been closed
                        Assert.IsFalse(areAllClosed, "CloseBrowsersBeforeTestStart failed: " +
                            "did not close all windows.");
                    }
                }
                finally
                {
                    // Clean up
                    _browser = new WebBrowser(_browserType);
                    _browser.KillAllOpenBrowsers();
                    this.OpenSwatTestPage();
                }
                // Reset the value
                SWAT.WantCloseBrowsersBeforeTestStart.CloseBrowsersBeforeTestStart = reset;
                SWAT.UserConfigHandler.Save();
            }
            else if (_browserType == BrowserType.Safari)
            {
                // Save the value to reset it later
                bool reset = SWAT.WantCloseBrowsersBeforeTestStart.CloseBrowsersBeforeTestStart;

                // Boolean used for the assertions
                bool isGoogleClosed = false;
                bool isW3Closed = false;

                // Open multiple windows to close
                _browser.OpenBrowser();
                _browser.NavigateBrowser("www.google.com");
                _browser.OpenBrowser();
                _browser.NavigateBrowser("www.w3schools.com");

                // Set the option not to close when the test starts
                SWAT.WantCloseBrowsersBeforeTestStart.CloseBrowsersBeforeTestStart = closeAll;
                SWAT.UserConfigHandler.Save();

                // Command extractor's constructor should not close any open browsers
                SWAT_Editor.CommandExtractor extractor = new SWAT_Editor.CommandExtractor(_browserType);
                
                // Check if there are any browsers open
                try
                {
                    _browser.AssertBrowserExists("Google");
                }
                catch (BrowserExistException)
                {
                    isGoogleClosed = true;
                }
                try
                {
                    _browser.AssertBrowserExists("W3");
                }
                catch (BrowserExistException)
                {
                    isW3Closed = true;
                }

                if (closeAll)
                {
                    // Assert that all the web browsers have been closed
                    Assert.IsTrue(isGoogleClosed && isW3Closed, "CloseBrowsersBeforeTestStart failed: " +
                                "closed all windows when the user setting was turned off.");
                }
                else
                {
                    // Assert that all the web browsers have not been closed
                    Assert.IsFalse(isGoogleClosed || isW3Closed, "CloseBrowsersBeforeTestStart failed: " +
                        "did not close all windows.");
                }
                
                //_browser = new WebBrowser(_browserType);

                // Needed for other tests since we closed open web browsers
                this.OpenSwatTestPage();

                // Reset the value
                SWAT.WantCloseBrowsersBeforeTestStart.CloseBrowsersBeforeTestStart = reset;
                SWAT.UserConfigHandler.Save();
            }

        }
        protected override string RunCode(string source, WebBrowser browser, string assems)
        {
            assems = System.Reflection.Assembly.GetAssembly(typeof(FireFox)).Location + ";" + assems.Trim();
            string[] assemArray = assems.Split(';');
            using (CSharpCodeProvider compiler = new CSharpCodeProvider())
            {
				source = source.Trim();

                //include SWAT.Core.dll when compiling
                CompilerParameters options = new CompilerParameters(assemArray);
                options.GenerateInMemory = true;

				// Put newlines at the end of #define and /define so that they work
				// Defines are always #define _symbol_ or /define:_symbol_[;_symbol_]
				Regex defineFixer = new Regex("((#define +\\S+)|(/d(efine)?:(\\S+;?)+))\\s*");
				source = defineFixer.Replace(source,"\n$1\n");

				// Parentheses matching for preprocessor arguments, since they must 
				// go on their own line
				Regex preprocessors = new Regex("(#if|#elif|#else|#endif)");
				int index = 0;
				while (index != -1)
				{
					Match match = preprocessors.Match(source, index);
					if (!match.Success)
						break;
					index = match.Index;
					source = source.Insert(index, "\n");
					bool noargs = match.ToString().Contains("endif") || match.ToString().Contains("else");
					bool foundSpace = false;
					int parenCount = 0;
					for (; index<source.Length; index++)
					{
						if (source[index] == ' ' && !foundSpace)
						{
							for (; (source[index] == ' ' || source[index] == '\t') && index < source.Length; index++);
							foundSpace = true;

							if (noargs)
								break;
							
							if (source[index] != '(')
							{
								for (; (source[index] != ' ' && source[index] != '\t') && index < source.Length; index++);
								break;
							}
						}
						if (source[index] == '(')
							parenCount++;
						if (source[index] == ')')
							parenCount--;
						if (parenCount == 0 && foundSpace)
						{
							index++;
							break;
						}
					}
					source = source.Insert(index, "\n");
				}

				// The usings go right before the namespace or the class declaration
				// This way they appear right after the #defines
				Regex usingFixer = new Regex("(namespace|public|class)");
				source = source.Insert(usingFixer.Match(source).Index,
					"using System.Collections.Generic;");
				
                //insert a method to add references to this and the runtime variables
				int indexToInsert = -1;
                if (source.Contains("namespace"))
				{
                    //get the second curly bracket so the method is inside a class
                    indexToInsert = source.IndexOf('{', source.IndexOf('{') + 1) + 1;
				}

                else
				{
                    //if there is no namespace, then just find the first curly bracket
                    indexToInsert = source.IndexOf('{') + 1;
				}

                source = source.Insert(indexToInsert, 
					"public static WebBrowser browser; "+
					"public static IVariableRetriever swatVars;" +
                    "public static void SetVars(WebBrowser b, IVariableRetriever vars)"+
						"{browser=b; swatVars=vars;}");

                //Compile
                CompilerResults results = compiler.CompileAssemblyFromSource(options, source);
                if (results.Errors.Count > 0 || results.NativeCompilerReturnValue != 0)
                {
                    StringBuilder errormessage = new StringBuilder("Error in compiling dynamic C#\n");
                    foreach (object error in results.Errors)
                        errormessage.Append(error + "\n");
                    throw new RunScriptCompilerException(errormessage.ToString());
                }

                string classWithMain = null, classWithVars = null;
                Assembly assembly = results.CompiledAssembly;

                //Find the class with the Main method and the SetVars method
                Type[] listOfTypes = assembly.GetTypes();
                foreach (Type t in listOfTypes)
                {
                    MethodInfo[] listOfMethods = t.GetMethods();
                    foreach (MethodInfo m in listOfMethods)
                    {
                        if (m.ToString().Contains("Main"))
                        {
                            classWithMain = t.ToString();
                        }
                        else if (m.ToString().Contains("SetVars"))
                        {
                            classWithVars = t.ToString();
                        }
                    }
                    if (classWithMain != null && classWithVars != null)
                        break;
                }
                if (classWithMain == null)
                    throw new RunScriptCompilerException("Main method not present");

                Type type = assembly.GetType(classWithMain);
                object obj = assembly.CreateInstance(classWithMain);

                //Run SetVars
                MethodInfo method = type.GetMethod("SetVars");
                method.Invoke(obj, new object[] { browser, variables });

                string result;

                //Run the Main
                try
                {
                    method = type.GetMethod("Main");
                    result = method.Invoke(obj, null).ToString();
                }
                catch (Exception ex)
                {
                    throw new RunScriptRuntimeException("Error in the Main Method : " + ex.Message);
                }    
                
                return result;
            }
        }
 public ConfigurationItemTestsTestFixture()
 {
     _browser = new WebBrowser(BrowserType.InternetExplorer);
 }