如何在TestMethod CUIT中获得Ajax响应

时间:2018-10-18 14:19:12

标签: asp.net ajax model-view-controller coded-ui-tests

我在测试课程中有测试方法。我使用编码的用户界面。 到目前为止,我测试过的一切似乎都不错。现在,我想在单击“保存”按钮时等待Ajax调用,但是我不知道如何。 有可能这样做吗?而且如果我的Ajax响应超过500毫秒,则测试应该会失败。

这是我的示例课程:

 [TestInitialize]
    public void TestInitialize()
    {
        string serviceName = "xxxxxxx.exe";
        var binPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)));
        string servicePath = @"\ServiceProjects\vvvvvv\bin\Debug\";
        string absolutePath = String.Format("{0}{1}{2}", binPath, servicePath, serviceName);

        Process.Start(absolutePath);

        BrowserWindow.CurrentBrowser = "ie";
        BrowserWindow.Launch(baseAddress);
    }

    [TestMethod]
    public void ManagerTestMethod()
    {

        this.UIMap.ManagerTestMethod_MobilePrefix();

    }

    [TestCleanup]
    public void TestCleanup()
    {

        this.UIMap.TestCleanup();

    }

在此ManagerTestMethod_MobilePrefix()方法中,单击“保存”按钮:

        public void ManagerTestMethod_MobilePrefix()
    {
        #region Variable Declarations
        HtmlDiv uISljedećizahtjevNoPane = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument.UISljedećizahtjevNoPane;
        HtmlButton uIItemButton = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument.UIItemButton;
        HtmlCustom uIItemCustom = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument1.UIŠifarniciCustom.UIItemCustom;
        HtmlHyperlink uIMobilniprefiksHyperlink = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument1.UICodelistsSubMenuCustom.UIMobilniprefiksHyperlink;
        HtmlDiv uIItemPane = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIAps_mng_mobilephonepPane.UIItemPane;
        HtmlSpan uIItemPane1 = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIAps_mng_mobilephonepPane.UIItemPane1;
        HtmlEdit uIApsmngmobilephonepreEdit = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIApsmngmobilephonepreEdit;
        HtmlEdit uIApsmngmobilephonepreEdit1 = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIApsmngmobilephonepreEdit1;
        HtmlEdit uIApsmngmobilephonepreEdit2 = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIApsmngmobilephonepreEdit2;
        HtmlButton uISačuvatiButton = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UISačuvatiButton;
        #endregion

        // Click '+ Sljedeći zahtjev + No' pane
        Mouse.Click(uISljedećizahtjevNoPane, new Point(1651, 234));

        // Click '+ Sljedeći zahtjev + No' pane
        Mouse.Click(uISljedećizahtjevNoPane, new Point(1778, 308));

        // Click '+' button
        Mouse.Click(uIItemButton, new Point(51, 23));

        // Click custom control
        Mouse.Click(uIItemCustom, new Point(48, 30));

        // Click 'Mobilni prefiks' link
        Mouse.Click(uIMobilniprefiksHyperlink, new Point(87, 18));

        // Set flag to allow play back to continue if non-essential actions fail. (For example, if a mouse hover action fails.)
        Playback.PlaybackSettings.ContinueOnError = true;

        // Mouse hover  pane at (1, 1)
        Mouse.Hover(uIItemPane, new Point(1, 1));

        // Reset flag to ensure that play back stops if there is an error.
        Playback.PlaybackSettings.ContinueOnError = false;

        // Click pane
        Mouse.Click(uIItemPane1, new Point(6, 5));

        // Type 'test1' in 'aps.mng.mobilephoneprefix.details.Code' text box
        uIApsmngmobilephonepreEdit.Text = this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEditText;

        // Type '{Tab}' in 'aps.mng.mobilephoneprefix.details.Code' text box
        Keyboard.SendKeys(uIApsmngmobilephonepreEdit, this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEditSendKeys, ModifierKeys.None);

        // Type 'test1' in 'aps.mng.mobilephoneprefix.details.CoreCode' text box
        uIApsmngmobilephonepreEdit1.Text = this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEdit1Text;

        // Type '100' in 'aps.mng.mobilephoneprefix.details.Value' text box
        uIApsmngmobilephonepreEdit2.Text = this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEdit2Text;

        // Click 'Sačuvati' button
        Mouse.Click(uISačuvatiButton, new Point(52, 20));
    }

最后一步对我至关重要:Mouse.Click(uISačuvatiButton,new Point(52,20));在这一步中,我将对我的API控制器进行Ajax调用以插入数据。 我希望能够从表单中捕获验证,也希望能够捕获通过单击保存按钮触发的Ajax调用的响应。

1 个答案:

答案 0 :(得分:-1)

我可能有一个可以尝试的想法。我从未遇到过像您这样的情况,所以我无法确定它是否可以工作。

您可以在该表单上提供一个隐藏控件,其值只有在Ajax调用完成后才能设置。如果您可以通过“编码的UI”测试找到该控件,则可以在单击该保存按钮后将计时器运行500毫秒。