为什么selenium测试用例在不访问cookie的情况下运行?

时间:2017-01-22 14:02:54

标签: c# asp.net-mvc selenium cookies nunit

Cookie中有一个authtoken用于验证用户,但是当我尝试使用Nunit和cmd运行selenium C#测试用例代码时,chrome实例启动并且没有cookie,因此它将我重定向到登录页面。问题是为什么在测试期间启动的实例中没有cookie以及我如何解决这个问题。 这是我的代码。

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace AutomationTest
{
    [TestFixture]
    public class SeleniumTest
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetupTest()
        {
            driver = new ChromeDriver();
            baseURL = "http://localhost/";
            verificationErrors = new StringBuilder();
        }

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

        [Test]
        public void TheSTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/TrailHead/");
            driver.FindElement(By.Id("addNewLeadButton")).Click();
            driver.FindElement(By.CssSelector("td.formTitle > input.td-button")).Click();
            driver.FindElement(By.Id("township")).Clear();
            driver.FindElement(By.Id("township")).SendKeys("2n");
            driver.FindElement(By.Id("range")).Clear();
            driver.FindElement(By.Id("range")).SendKeys("2e");
            driver.FindElement(By.Id("section")).Clear();
            driver.FindElement(By.Id("section")).SendKeys("2");
            driver.FindElement(By.Id("legal")).Clear();
            driver.FindElement(By.Id("legal")).SendKeys("2");
            driver.FindElement(By.Id("NRI")).Clear();
            driver.FindElement(By.Id("NRI")).SendKeys("2");
            driver.FindElement(By.Id("NMA")).Clear();
            driver.FindElement(By.Id("NMA")).SendKeys("2");
            driver.FindElement(By.Id("tractAskedPrice")).Clear();
            driver.FindElement(By.Id("tractAskedPrice")).SendKeys("2");
            driver.FindElement(By.CssSelector("div.modalFooter > div.footer-right-button-save")).Click();
            driver.FindElement(By.XPath("//div[@onclick='saveAndExit()']")).Click();
            // Warning: assertTextPresent may require manual changes
            Assert.IsTrue(Regex.IsMatch(driver.FindElement(By.CssSelector("BODY")).Text, "^[\\s\\S]*$"));
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

        private bool IsAlertPresent()
        {
            try
            {
                driver.SwitchTo().Alert();
                return true;
            }
            catch (NoAlertPresentException)
            {
                return false;
            }
        }

        private string CloseAlertAndGetItsText()
        {
            try
            {
                IAlert alert = driver.SwitchTo().Alert();
                string alertText = alert.Text;
                if (acceptNextAlert)
                {
                    alert.Accept();
                }
                else
                {
                    alert.Dismiss();
                }
                return alertText;
            }
            finally
            {
                acceptNextAlert = true;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Chrome WebDriver使用临时会话来完成所有工作。因此,您设置为用户的任何Cookie都不会随身携带。

如果要覆盖它,则可以使用user-data-dir属性。

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");

所有这一切都来自于他们的docs

答案 1 :(得分:0)

您可以采用多种策略来解决此问题:

  1. 如果您的登录功能是API,请直接调用此方法获取Cookie并将其添加到浏览器测试会话中。这样做的好处是它可以重复用于登录后的任何功能测试。您需要实现HttpWebRequest,但不应该太困难。
  2. 如果身份验证cookie没有更改,您可以手动获取名称和值,并将其添加到测试中,方法是将其添加到会话中:

    var cookie = new Cookie("CookieName","CookieValue"); driver.Manage().Cookies.AddCookie(cookie);

  3. 我们公司有选项2,我们正在实施选项1。