【问题标题】:Selenium UI Testing: TestCleanup() quits rest of the tests after the first test is loadedSelenium UI 测试:TestCleanup() 在加载第一个测试后退出其余测试
【发布时间】:2017-02-11 00:41:38
【问题描述】:

我正在自动化我的 github 配置文件,以下是我的测试用例:

  • 加载浏览器(在 testInitialize() 中定义
  • 加载网址
  • 执行登录 下面是sn-p的代码:

命名空间 GitAutomationTest { 使用 Microsoft.VisualStudio.TestTools.UnitTesting; 使用 OpenQA.Selenium.IE; 使用 OpenQA.Selenium.Remote; 使用系统; [测试类] 公共类 GitTest { 私有字符串 baseURL = "https://github.com/login"; 私有 RemoteWebDriver 驱动程序; 公共 TestContext TestContext { 获取;放; }

     [TestMethod]
     public void LoadURL() {
            driver.Navigate().GoToUrl(baseURL);
            Console.Write("Loaded URL is :" + baseURL);
        }
        [TestMethod]
        public void PerformLogin() { 
            driver.FindElementById("login_field").SendKeys("USERNAME");
            driver.FindElementById("password").SendKeys("PASSWORD");
            Console.Write("password entered \n ");
            driver.FindElementByClassName("btn-primary").Click();
            driver.GetScreenshot().SaveAsFile(@"screenshot.jpg", format: System.Drawing.Imaging.ImageFormat.Jpeg);
            Console.Write("Screenshot Saved: screenshiot.jpg");
        }
        [TestCleanup()]
        public void MyTestCleanup()
        {
            driver.Quit();
        }
        [TestInitialize()]
        public void MyTestInitialize()
        {
            driver = new InternetExplorerDriver();
            driver.Manage().Window.Maximize();
            Console.Write("Maximises The window\n");
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
        }
    }
}

输出
每次我运行所有测试时: - 测试已初始化:Internet Explorer 已加载 - 基础网址已加载 - 然后驱动程序退出 TestCleanUP()

驱动程序下次运行 testperformLogin() - 测试找不到执行登录的用户名和密码元素,因为这次没有加载base url。

我们如何管理 TestInitialize() 类: - 浏览器使用 baseurl 直到所有测试完成。 我们如何管理 TestCleanup() 以便: - 浏览器仅在所有测试完成后关闭。

【问题讨论】:

标签: c# selenium testing selenium-webdriver integration-testing


【解决方案1】:

有一个AssemblyCleanup 属性在所有测试执行后运行。

您可以在此处找到有关属性的更多信息 - Unit Testing Framework

【讨论】:

    【解决方案2】:

    您需要将以下代码移至“PerformLogin”测试方法

    driver.Navigate().GoToUrl(baseURL);
    

    或者另一种方法是在“Mytestinitialize”方法中添加以下代码并删除“LoadURL”方法

    driver.Navigate().GoToUrl(baseURL);
    

    您正面临这个问题,因为在每个 [TestMethod] 之前调用 [TestInitialize] 并且在每个 [TestMethod] 之后调用 [TestCleanup]。

    在您的情况下,“LoadURL”测试能够获取 URL,但“PerformLogin”无法获取 URL,因为“MyTestInitialize”中没有提到它。

    【讨论】:

      猜你喜欢
      • 2018-06-21
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多