示例#1
0
        public void SetUp()
        {
            // Save a reference to the current Test's log in its TestContext
            TestContext.Set("log", "");

            // Get the Base Address
            string baseAddress = ConfigurationManager.AppSettings["baseAddress"] ?? "";

            // Get the Test's custom Attributes
            IEnumerable <CustomAttributeData> customAttributeDatas = new StackTrace().GetFrame(1).GetMethod().CustomAttributes;

            // Log the custom Attributes
            Log.CustomAttributes(customAttributeDatas);
            // Write an end-line
            Log.WriteLine();
            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(logPadding.Padding + "TestBase.SetUp()");
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Base Address: " + baseAddress);
            sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()");
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Create a new instance of the httpclient
                HttpClient httpClient = new HttpClient();
                if (baseAddress.Length > 0)
                {
                    httpClient.BaseAddress = new Uri(baseAddress);
                }
                // Save that to the test context
                TestContext.Set("httpClient", httpClient);
                // Logging - After action success
                Log.Success(logPadding.Padding);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                sb = Log.Exception(sb, e);
                // Fail current Test
                Assert.Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally(logPadding.Padding);
            }
        }
示例#2
0
        public void Setup()
        {
            // Save a reference to the current Test's log in its TestContext
            TestContext.Set("log", "");

            // Get the Test's custom Attributes
            IEnumerable <CustomAttributeData> customAttributeDatas = new StackTrace().GetFrame(1).GetMethod().CustomAttributes;

            // Log the custom Attributes
            Log.CustomAttributes(customAttributeDatas);
            // Write an end-line
            Log.WriteLine();

            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(logPadding.Padding + "TestBase.Setup()");
            sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()");
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Create a WebDrvier session
                Session.Create();

                // Set WebDriver's window to full screen
                AppBase.Maximize();

                // Delete all cookies
                AppBase.DeleteAllCookies();

                // Logging - After action success
                Log.Success(logPadding.Padding);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                sb = Log.Exception(sb, e);
                // Fail current Test
                Assert.Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally(logPadding.Padding);
            }
        }
示例#3
0
        public void TearDown()
        {
            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(logPadding.Padding + "TestBase.TearDown()");
            sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()");
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Check if the Test that is ending was a failure
                if (NUnit.Framework.TestContext.CurrentContext.Result.Outcome != NUnit.Framework.Interfaces.ResultState.Success)
                {
                    // Take screenshot of the failure state
                    AppBase.TakeScreenshot();
                }
                // Quit this driver, closing every associated window.
                AppBase.Quit();
                // Logging - After action success
                Log.Success(logPadding.Padding);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                sb = Log.Exception(sb, e);
                // Fail current Test
                Assert.Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally("");

                // Print this Test's log (from "TestContext") to the system console
                string log = TestContext.Get("log").ToString();
                if (log.Length > 0)
                {
                    Console.WriteLine(log);
                }
            }
        }
示例#4
0
        public void SetUp()
        {
            // Get the Base Address
            string baseAddress = ConfigurationManager.AppSettings["baseAddress"] ?? "";

            // Save a reference to the current Test's log in its TestContext
            TestContext.Set("log", "");

            // Log the test attributes of the current [Test]
            Log.StandardAttributes();

            // Log Before Action
            Log.BeforeAction(new OrderedDictionary()
            {
                { "baseAddress", baseAddress }
            });

            // Perform the action
            try
            {
                // Create a new instance of the httpclient
                HttpClient httpClient = new HttpClient();
                if (baseAddress.Length > 0)
                {
                    httpClient.BaseAddress = new Uri(baseAddress);
                }
                // Save that to the test context
                TestContext.Set("httpClient", httpClient);
                // Logging - After action success
                Log.Success();
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message);
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
        }
示例#5
0
        public void Setup()
        {
            // Save a reference to the current Test's log in its TestContext
            TestContext.Set("log", "");

            // Log the test attributes of the current [Test]
            Log.StandardAttributes();

            // Log Before Action
            Log.BeforeAction();

            // Perform the action
            try
            {
                // Create a WebDrvier session
                Session.Create();

                // Set WebDriver's window to full screen
                AppBase.Maximize();

                // Delete all cookies
                AppBase.DeleteAllCookies();

                // Logging - After action success
                Log.Success();
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message);
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
        }
示例#6
0
        public void TearDown()
        {
            // Log Before Action
            Log.BeforeAction();

            // Perform the action
            try
            {
                // Check if the Test that is ending was a failure
                if (NUnit.Framework.TestContext.CurrentContext.Result.Outcome != NUnit.Framework.Interfaces.ResultState.Success)
                {
                    // Take screenshot of the failure state
                    AppBase.TakeScreenshot();
                }
                // Quit this driver, closing every associated window.
                AppBase.Quit();
                // Logging - After action success
                Log.Success();
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message);
            }
            finally
            {
                // Logging - After action
                Log.Finally();
                // Print log to console (for Visual Studio and Bamboo)
                string log = TestContext.Get("log").ToString();
                if (log.Length > 0)
                {
                    Console.WriteLine(log);
                }
            }
        }
        /// <summary>
        /// Intantiates a WebDriver (creating a session) using the current App.config.
        /// </summary>
        public static AppiumDriver <AppiumWebElement> Create()
        {
            #region Properties  (from the App.config)

            // Get settings from the App.config
            string app            = ConfigurationManager.AppSettings["app"];
            string appConfig      = ConfigurationManager.AppSettings["appConfig"] ?? "App.config";
            string automationName = ConfigurationManager.AppSettings["automationName"];
            string deviceName     = ConfigurationManager.AppSettings["deviceName"] ?? "n/a";
            Uri    hubUri         = new Uri(ConfigurationManager.AppSettings["hubUri"]);
            string platformName   = ConfigurationManager.AppSettings["platformName"];

            #endregion Properties  (from the App.config)

            // Declare a return value
            AppiumDriver <AppiumWebElement> returnValue = null;

            // Log Before Action
            Log.BeforeAction(new OrderedDictionary()
            {
                { "Automation Name", automationName },
                { "Platform Name", platformName },
                { "Device Name", automationName },
                { "App", app }
            });

            // Perform the action
            try
            {
                // Define the common "Desired Capabilities"
                DesiredCapabilities capabilities = new DesiredCapabilities();
                if (automationName.Length > 0)
                {
                    capabilities.SetCapability("automationName", automationName);
                }
                if (platformName.Length > 0)
                {
                    capabilities.SetCapability("platformName", platformName);
                }
                if (deviceName.Length > 0)
                {
                    capabilities.SetCapability("deviceName", deviceName);
                }
                if (app.Length > 0)
                {
                    capabilities.SetCapability("app", app);
                }
                capabilities.SetCapability("noReset", true);
                // Initialize the driver
                if (platformName == "Andorid")
                {
                    returnValue = new AndroidDriver <AppiumWebElement>(hubUri, capabilities, TimeSpan.FromMinutes(TestBase.sessionTimeoutInMinutes));
                }
                else if (platformName == "iOS")
                {
                    returnValue = new IOSDriver <AppiumWebElement>(hubUri, capabilities, TimeSpan.FromMinutes(TestBase.sessionTimeoutInMinutes));
                }
                else
                {
                    throw new NotImplementedException("Platform '" + platformName + "' not setup in Session.Create();");
                }
                // Set the global driver variable
                driver = returnValue;
                // Logging - After action success
                Log.Success("Created a new session.");
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message);
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
            // Return the return value
            return(returnValue);
        }
示例#8
0
        /// <summary>
        /// Intantiates a WebDriver (creating a session) using the current App.config.
        /// </summary>
        public static AppiumDriver <AppiumWebElement> Create()
        {
            #region Properties

            // Get settings from the App.config
            string app            = ConfigurationManager.AppSettings["app"];
            string appConfig      = ConfigurationManager.AppSettings["appConfig"];
            string automationName = ConfigurationManager.AppSettings["automationName"];
            string deviceName     = ConfigurationManager.AppSettings["deviceName"];
            Uri    hubUri         = new Uri(ConfigurationManager.AppSettings["hubUri"]);
            string platformName   = ConfigurationManager.AppSettings["platformName"];

            #endregion Properties

            // Declare a return value
            AppiumDriver <AppiumWebElement> returnValue = null;
            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(logPadding.Padding + "Session.Create()");
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Automation Name: " + automationName);
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Platform Name: " + platformName);
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Device Name: " + deviceName);
            sb.AppendLine(logPadding.InfoPadding + "[INFO] App: " + app);
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Define the common "Desired Capabilities"
                DesiredCapabilities capabilities = new DesiredCapabilities();
                if (automationName.Length > 0)
                {
                    capabilities.SetCapability("automationName", automationName);
                }
                if (platformName.Length > 0)
                {
                    capabilities.SetCapability("platformName", platformName);
                }
                if (deviceName.Length > 0)
                {
                    capabilities.SetCapability("deviceName", deviceName);
                }
                if (app.Length > 0)
                {
                    capabilities.SetCapability("app", app);
                }
                capabilities.SetCapability("noReset", true);
                // Initialize the driver
                if (platformName == "Andorid")
                {
                    returnValue = new AndroidDriver <AppiumWebElement>(hubUri, capabilities, TimeSpan.FromMinutes(TestBase.sessionTimeoutInMinutes));
                }
                else if (platformName == "iOS")
                {
                    returnValue = new IOSDriver <AppiumWebElement>(hubUri, capabilities, TimeSpan.FromMinutes(TestBase.sessionTimeoutInMinutes));
                }
                else
                {
                    throw new NotImplementedException("Platform '" + platformName + "' not setup in Session.Create();");
                }
                // Set the global driver variable
                driver = returnValue;
                // Logging - After action success
                Log.Success(logPadding.Padding);
                Log.WriteLine(logPadding.InfoPadding + "[INFO] Created a new session.");
            }
            catch (Exception e)
            {
                // Logging - After action exception
                sb = Log.Exception(sb, e);
                // Fail current Test
                Assert.Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally(logPadding.Padding);
            }
            // Return the return value
            return(returnValue);
        }
示例#9
0
        /// <summary>
        /// Create a TestBase for your specific project (see TestBaseStarAppIos, for example) and call this method.
        /// The Setup annotation was removed to prevent it being called when there is a project specific Setup (see above).
        /// </summary>
        public static void Setup()
        {
            // Log Before Action
            Log.BeforeAction();

            // Perform the action
            try
            {
                // Create a variable to serve as a flag for if a new session needs to be created or not
                bool createNewSession = false;
                // Create a variable to hold the Test's WebDriver
                AppiumDriver <AppiumWebElement> driver = null;
                // Is the global WebDriver varaiable set?
                if (Session.driver != null)
                {
                    // Create a variable to hold the Session Info
                    Dictionary <string, object> sessionInfo = null;
                    // Try to get the Session Info (which only works if there is a valid session) from the global variable
                    // Note: We need to use the same Appium session (driver) for all test or we could lose the device in the cloud.
                    try { sessionInfo = Session.driver.SessionDetails; }
                    catch { /* do nothing */ }
                    // Check the Session Info variable has some content
                    if (sessionInfo != null)
                    {
                        // Use the existing session
                        Log.WriteLine("    [INFO] Using the existing session.");
                        driver = Session.driver;
                    }
                    else
                    {
                        // Set the flag to create a new session to true
                        createNewSession = true;
                    }
                }
                // Should we create a new session?
                if (createNewSession == true)
                {
                    // Wait 60 seconds to timeout any existing sessions (in SauceLab's TestObject)
                    if (ConfigurationManager.AppSettings["appConfig"].ToString().Contains("SauceLabs"))
                    {
                        Sleep.Milliseconds(60000, "Waiting 60 seconds to kill the current session (in SauceLab's TestObject).");
                    }
                    // Create a new session
                    Log.WriteLine("    [INFO] Creating a new session.");
                    driver = Session.Create();
                }
                // Save a reference to the current Test's driver (session) in its TestContext
                TestContext.Set("driver", driver);

                // Reset the app
                AppBase.ResetApp();

                // Logging - After action success
                Log.Success();
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message);
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
        }
示例#10
0
        public void Setup()
        {
            // Save a reference to the current Test's log in its TestContext
            TestContext.Set("log", "");

            // Get the Test's custom Attributes
            IEnumerable <CustomAttributeData> customAttributeDatas = new StackTrace().GetFrame(1).GetMethod().CustomAttributes;

            // Log the custom Attributes
            Log.CustomAttributes(customAttributeDatas);
            // Write an end-line
            Log.WriteLine();

            // Figure out the padding (if any) to prepend to the log line
            LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType);
            // Logging - Before action
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(logPadding.Padding + "TestBase.Setup()");
            //sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()");
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Create a variable to serve as a flag for if a new session needs to be created or not
                bool createNewSession = false;
                // Create a variable to hold the Test's WebDriver
                AppiumDriver <AppiumWebElement> driver = null;
                // Is the global WebDriver varaiable set?
                if ((Session.driver != null))
                {
                    // Is there session info?
                    Dictionary <string, object> sessionInfo = null;
                    try { sessionInfo = Session.driver.SessionDetails; }
                    catch { /* do nothing */ }
                    if (sessionInfo != null)
                    {
                        // Use the existing session
                        Log.WriteLine(logPadding.InfoPadding + "[INFO] Using the existing session.");
                        driver = Session.driver;
                    }
                    else
                    {
                        // Set the flag to create a new session to true
                        createNewSession = true;
                    }
                }
                // Should we create a new session?
                if (createNewSession == true)
                {
                    // Wait 60 seconds to kill the current session (the default TimeOut for Appium)
                    Sleep.Milliseconds(60000, "Waiting 60 seconds to kill the current session.");
                    // Create a new session
                    Log.WriteLine(logPadding.InfoPadding + "[INFO] Creating a new session.");
                    driver = Session.Create();
                }
                // Save a reference to the current Test's driver in its TestContext
                TestContext.Set("driver", driver);

                // Reset the app
                AppBase.ResetApp();

                // Logging - After action success
                Log.Success(logPadding.Padding);
            }
            catch (Exception e)
            {
                // Logging - After action exception
                sb = Log.Exception(sb, e);
                // Fail current Test
                Assert.Fail(sb.ToString());
            }
            finally
            {
                // Logging - After action
                Log.Finally(logPadding.Padding);
            }
        }