编码的Ui在所有浏览器上都能通过加载屏幕

本文关键字:屏幕 加载 浏览器 Ui 编码 | 更新日期: 2023-09-27 18:29:09

我在Visual Studio 2015 Update 1上使用Microsoft Coded Ui。

我正在为网站构建编码的Ui测试。我在Chrome和Firefox上遇到了问题。该网站是onboard.apasseways.com。有一个div包含以下信息:

<div id="loadingOverlay" data-bind="fadeVisible: loading" style="display: none; width: 100%; height: 100%; position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; z-index: 9999; background: rgb(204, 204, 204);">

Chrome和Firefox渲染页面的速度如此之快,以至于当样式为"display: table"时,它们会看到loadingOverlay。这种加载覆盖几乎覆盖了网站的每一个部分。

我的问题是,我如何告诉Chrome和Firefox等待loadingOverlay不显示,这样它就可以点击背景中的元素。

我尝试过这个代码,但它一直说集合为1。

public static HtmlDiv GetOverlay(UITestControl parent, string id)
    {
        var div = new HtmlDiv(parent);
        div.SearchProperties.Add(HtmlDiv.PropertyNames.Id, id);
        GetOverlayProperty(div);
        return div;
    }
    private static UITestControlCollection GetOverlayProperty(HtmlDiv overlay)
    {
        overlay.SearchProperties.Add(HtmlControl.PropertyNames.ControlDefinition, "display: table",
            PropertyExpressionOperator.Contains);
        UITestControlCollection collection = overlay.FindMatchingControls();
        if (collection.Any())
        {
            GetOverlay(browser, "loadingOverlay");
        }
        return collection;
    }

我的测试初始化

    [TestInitialize]
    public void ClassInitializer()
    {
        BrowserWindow.CurrentBrowser = ConfigurationManager.AppSettings.Get("Browser-Type");
        CodedUIUtils.browser = BrowserWindow.Launch(new Uri(ConfigurationManager.AppSettings.Get("Browser-Url")));
        CodedUIUtils.browser.CloseOnPlaybackCleanup = false;
        CodedUIUtils.browser.Maximized = false;
    }

我的登录测试

    [TestMethod]
    public void LoginAndLogout()
    {
        CodedUIUtils.ClickButton(CodedUIUtils.browser, "PassagewaysLogin");
        CodedUIUtils.LoginGuest(CodedUIUtils.browser);
        CodedUIUtils.ClickLogon(CodedUIUtils.browser, "Logon");
        HtmlDiv overlay = CodedUIUtils.GetOverlay(CodedUIUtils.browser, "loadingOverlay");
        overlay.WaitForControlNotExist();
        CodedUIUtils.ClickLink(CodedUIUtils.browser, "Log Out");
        Playback.Wait(2000);
    }

登录Utils

   public static class CodedUIUtils
   {
    public static BrowserWindow browser;
    public static void LoginGuest(BrowserWindow browser)
    {
        EnterText(browser, "Email", "onboardTestUserId+guest@gmail.com");
        EnterText(browser, "LoginPassword", "Testing2!");
    }
    public static void ClickButton(UITestControl parent, string id)
    {
        var button = new HtmlButton(parent);
        button.SearchProperties.Add(HtmlButton.PropertyNames.Id, id);
        Mouse.Click(button);
    }
    public static void EnterText(UITestControl parent, string id, string value)
    {
        var edit = new HtmlEdit(parent);
        edit.SearchProperties.Add(HtmlEdit.PropertyNames.Id, id);
        edit.Text = value;
    }
    public static void ClickLink(UITestControl parent, string innerText)
    {
        var link = new HtmlHyperlink(parent);
        link.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, innerText);
        link.WaitForControlReady();
        Mouse.Click(link);
    }
    public static void ClickLogon(UITestControl parent, string value)
    {
        var logonInput = new HtmlInputButton(parent);
        logonInput.SearchProperties.Add(HtmlInputButton.PropertyNames.ValueAttribute, value);
        Mouse.Click(logonInput);
    }
   }

编码的Ui在所有浏览器上都能通过加载屏幕

通常的最佳实践是,当某个东西可能有加载时间/介绍动画时,或者通常使用WaitForControlReady或WaitForControl Exist函数进行编码。

因此,除非你特别关心加载覆盖,否则你可以把函数放在覆盖影响你真正想要交互的第一个对象上。

[TestMethod]
    public void StackOverflow()
        {
        BrowserWindow browser = BrowserWindow.Launch(new Uri("https://onboard.passageways.com/"));
        var _hyper = new HtmlButton(browser);
        _hyper.SearchProperties.Add("ID", "PassagewaysLogin");
        _hyper.WaitForControlReady();
        LoadingOverlay(browser);
        Mouse.Click(_hyper);
        var _email = new HtmlEdit(browser);
        _email.SearchProperties.Add("ID", "Email");
        _email.WaitForControlReady();
        LoadingOverlay(browser);
        Keyboard.SendKeys(_email, "testemail@email.com");
        Playback.Wait(10000);
        }
        public static void LoadingOverlay(BrowserWindow browser)
        {
        var _image = new HtmlDiv(browser);
        _image.SearchProperties.Add("ID", "loadingOverlay");
            _image.WaitForControlReady();
        }