Visual Studio在进行编码UI测试时错误地将Microsoft Edge检测为Windows应用商店应用程序

本文关键字:检测 Edge Microsoft Windows 应用 应用程序 Studio 编码 UI 错误 测试 | 更新日期: 2023-09-27 18:27:35

我目前正在尝试为Web应用程序设置编码UI测试。我使用Visual Studio Enterprise 2015创建了一个"编码UI测试项目"。

我已经安装了Microsoft WebDriver,并添加了编码UI测试项目所需的4个NuGet包。

当使用"编码的UI测试生成器"时,我从Microsoft Edge上的一个全新选项卡开始,然后在UI测试生成器上点击记录。无论我采取什么行动,建设者都会吐出:

To test Windows Store apps, use the Coded UI Test project template for Windows Store apps under the Windows Store node.

我自己还没有安装Chrome WebDriver,但当我点击Chrome并做各种事情时,它确实能正常工作,并为录制的部分创建代码。有没有一个步骤我可能忘记了让它在Microsoft Edge上正常工作?

Visual Studio在进行编码UI测试时错误地将Microsoft Edge检测为Windows应用商店应用程序

当我提出这个问题时,我假装我可以使用Microsoft Edge的编码UI测试。然而,事实证明,它只是在这个时间点上不兼容。

如果您希望使用Microsoft Edge进行UI测试,则需要创建一个单元测试项目(非编码UI测试项目)。您也将无法使用UI测试生成器。

以下是一个与Microsoft Edge 一起使用的单元测试项目的UI测试示例

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Support.UI;
namespace ExampleUITest
{
    [TestClass]
    public class ExampleUITest
    {
        private IWebDriver driver;
        private string serverPath = "Microsoft Web Driver";
        private string baseUrl = "http://example.com";

        [TestInitialize]
        public void TestInitialize()
        {
            if (Environment.Is64BitOperatingSystem)
            {
                serverPath = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath);
            }
            else
            {
                serverPath = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath);
            }
            var options = new EdgeOptions
            {
                PageLoadStrategy = EdgePageLoadStrategy.Eager
            };
            driver = new EdgeDriver(serverPath, options);
            driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));
        }
        [TestCleanup]
        public void TestFinalize()
        {
            // Automatically closes window after test is run
            driver?.Close();
        }
        [TestMethod]
        public void LoadHomePage()
        {
            driver.Url = baseUrl;
            var element = driver.FindElement(By.LinkText("Example Link Text"));
            element.Click();
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
            wait.Until(w => w.Url == $"{baseUrl}/ExampleLink");
            Assert.AreEqual(driver.Url, $"{baseUrl}/ExampleLink");
        }
    }
}

更多信息可以在这篇博客文章

中找到