【问题标题】:why selenium test case runs without accessing cookies?为什么 selenium 测试用例在不访问 cookie 的情况下运行?
【发布时间】:2017-02-01 21:55:35
【问题描述】:

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;
            }
        }
    }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!看来您需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果之后仍有问题,请随时回来提供更多详细信息。

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


【解决方案1】:

Chrome WebDriver 使用临时会话来完成所有工作。因此,您设置为用户的任何 cookie 都不会继续使用。

如果你想覆盖它,那么你可以使用user-data-dir 属性。

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

所有这些都来自查看他们的docs

【讨论】:

  • 我用过这个,它打开了默认的 chrome 实例,但它没有我需要去的 GoToUrl
【解决方案2】:

您可以实施多种策略来解决此问题:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 2020-08-18
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    相关资源
    最近更新 更多