使用Selenium Webdriver在具有持续集成的项目上进行集成测试

时间:2013-11-28 14:39:27

标签: selenium continuous-integration selenium-webdriver nunit cruisecontrol.net

我有一个问题。当我在我的webb应用程序上运行selenium webdrive集成测试时,Web应用程序必须正在运行,因为selenium浏览到我的应用程序的调试版本(从Visual Studio的IIS Express中启动)。问题是,如果我想使用CI开发实践来实现这一点,那么在专用CI机器上,该机器必须运行我的webb应用程序版本,该应用程序基于subversion目录中的当前主线代码。

代码库不断变化,因此从理论上讲,您可以使用subversion目录中的代码运行并重新启动CI计算机上的Web应用程序,以便测试始终涵盖最新的提交。

项目中的每个开发人员在预提交构建/测试上运行集成测试时都没有任何问题。可以使用Cruise Control,MsBuild,subversion和NUnit协同处理单元测试。但是,我想知道在动态代码库上自动运行集成服务器上的集成测试(selenium webdrive测试)。有没有人有这方面的经验,也许有例子?

修改 Arran建议您可以使用专用的测试环境来解决这个问题。我实际上并不了解专用测试环境的工作原理,以及实际解决这个问题的方法。收到的答案似乎不是自动化的,可以通过Continous Integration的并发集成流程来实现。是否有其他人对此事有任何经验或想法?

这是Selenium Webdrive代码供参考:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;
using System.Threading.Tasks;
using System.Threading;

namespace ChatProj.Tests
{
    [TestFixture]
    class WebDriverTestClass
    {
        private IWebDriver _driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetUp()
        {
            _driver = new FirefoxDriver();
            baseURL = "http://localhost:59932/";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                _driver.Quit();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        [Repeat(2)]
        [Test]
        public void TestFirefox()
        {

            _driver.Navigate().GoToUrl(baseURL + "");

            IWebElement userNameInput = _driver.FindElement(By.Name("UserName"));
            userNameInput.SendKeys("Svenneglenne");
            IWebElement passwordInput = _driver.FindElement(By.Name("Password"));
            passwordInput.SendKeys("password");
            _driver.FindElement(By.CssSelector("input[type=\"submit\"]")).Click();


            WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
            IWebElement messageBox = wait.Until<IWebElement>((d) =>
            {
                return d.FindElement(By.Id("message"));
            });

            IWebElement adminMessageWaiter = wait.Until<IWebElement>((d) =>
            {
                return d.FindElement(By.XPath("//ul[@id='discussion']/li[1]"));
            });


            System.Console.Out.WriteLine("STUFF");
            //IWebElement query = driver.FindElement(By.Id("message"));
            String textSnippet = "This is a selenium test";
            adminMessageWaiter.SendKeys("");

            messageBox.SendKeys("This is a selenium test");

            IWebElement waitForJava = wait.Until<IWebElement>((d) =>
            {
                return d.FindElement(By.XPath("//ul[@id='discussion']/li[1]"));
            });

            //Thread.Sleep(2000);

            WaitForPageLoad(10);

            _driver.FindElement(By.Id("sendmessage")).Click();
            //Thread.Sleep(2000);

            _driver.FindElement(By.LinkText("Logg")).Click();

            IWebElement loggWaiter = wait.Until<IWebElement>((d) =>
            {
                return d.FindElement(By.XPath("//div[@id='body']/section/table/tbody/tr/td[2]"));
            });
            Assert.AreEqual(textSnippet, loggWaiter.Text);

            _driver.FindElement(By.LinkText("MPM Graph")).Click();
            Thread.Sleep(2000);

        }

       public void WaitForPageLoad(int maxWaitTimeInSeconds) 
       {
        string state = string.Empty;
        try
        {
            WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(maxWaitTimeInSeconds));

            //Checks every 500 ms whether predicate returns true if returns exit otherwise keep trying till it returns ture
            wait.Until(d =>
            {

                try
                {
                    state = ((IJavaScriptExecutor)_driver).ExecuteScript(@"return document.readyState").ToString();
                }
                catch (InvalidOperationException)
                {
                    //Ignore
                }
                catch (NoSuchWindowException)
                {
                    //when popup is closed, switch to last windows
                    _driver.SwitchTo().Window(_driver.WindowHandles.Last());
                }
                //In IE7 there are chances we may get state as loaded instead of complete
                return (state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase));

            });
        }
        catch (TimeoutException)
        {
            //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
            if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
                throw;
        }
        catch (NullReferenceException)
        {
            //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
            if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
                throw;
        }
        catch (WebDriverException)
        {
            if (_driver.WindowHandles.Count == 1)
            {
                _driver.SwitchTo().Window(_driver.WindowHandles[0]);
            }
            state = ((IJavaScriptExecutor)_driver).ExecuteScript(@"return document.readyState").ToString();
            if (!(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase)))
                throw;
        }
}



    }
}

0 个答案:

没有答案