Protractor - 当 DOM 元素更改时,页面对象不会更新

2023-12-29

我正在测试使用 angular.js 构建的 SPA,并使用页面对象模式来编写我的测试。在应用程序中,我们有许多将要更新的列表。例如,有一个附件列表,当添加/删除附件时,该列表将会更新。要添加附件,我们有一个模式窗口,当我们上传文件并单击“确定”时。文件上传并更新列表。

我编写了 2 个页面对象,一个用于上传模式窗口,另一个用于附件列表的预览。在我的测试中,我首先获取附件的当前数量,然后单击按钮激活模式窗口并附加文件。然后我再次对预览页面中的附件进行计数,并进行比较,使其增加 1。但测试失败。页面对象未更新,并且附件计数仍显示为 2。

Test

it('Should attach a file when a file is selected and OK button is pressed.', function () {
            var currentFileCount = viewMeetingTabPage.getMeetingAttachmentCount();

            viewMeetingPage.clickAddAttachmentButton();
            addAttchmentPage.attachFile();
            addAttchmentPage.clickConfimAttachFileButton();

            currentFileCount.then(function (curCount) {
                viewMeetingTabPage.getMeetingAttachmentCount().then(function (newCount) {
                    expect(newCount).toBe(curCount + 1);
                    //expect(viewMeetingTabPage.getMeetingAttachmentName()).toBe('test-file.pdf');
                });
            });
        });

查看会议选项卡页面

this.getMeetingAttchments = function () {
        return element.all(by.repeater('attachment in meeting.AttachmentViewModelList track by $index'));
    };

this.getMeetingAttachmentCount = function () {
        return this.getMeetingAttchments().count();
    };

我需要的是在上传文件后以某种方式更新页面对象。我怎样才能做到这一点。


这就是如何控制流 https://github.com/angular/protractor/blob/master/docs/control-flow.md作品。执行测试代码会排队一堆承诺。它们按照添加到流程中的顺序得到解决,同时每个都等待前一个完成。

it('Should attach a file when a file is selected and OK button is pressed.', function () {
        # Queues the count promise
        var currentFileCount = viewMeetingTabPage.getMeetingAttachmentCount();

        # Queues some other promises that would start
        # to get executed once the one above finishes
        viewMeetingPage.clickAddAttachmentButton();
        addAttchmentPage.attachFile();
        addAttchmentPage.clickConfimAttachFileButton();

        # This piece of code branches-off the control-flow
        # and gets executed immediately after currentFileCount is resolved
        # i.e. before the clickAddAttachmentButton
        currentFileCount.then(function (curCount) {
            # That's why newCount equals curCount,
            # they are counting the same number of elements
            # since nothing changed in the meantime
            viewMeetingTabPage.getMeetingAttachmentCount().then(function (newCount) {
                expect(newCount).toBe(curCount + 1);
                //expect(viewMeetingTabPage.getMeetingAttachmentName()).toBe('test-file.pdf');
            });
        });
    });

currentFileCount可以被视为测试的设置阶段,因此您可以将其提取到beforeEach block:

var initialFileCount;
beforeEach(function() {
    viewMeetingTabPage.getMeetingAttachmentCount().then(function(count) {
        initialFileCount = count;
    });
});

it('Should attach a file when a file is selected and OK button is pressed.', function () {
    viewMeetingPage.clickAddAttachmentButton();
    addAttchmentPage.attachFile();
    addAttchmentPage.clickConfimAttachFileButton();
    expect(viewMeetingTabPage.getMeetingAttachmentCount()).toBe(initialFileCount + 1);
});

由于 Protractor 修补 jasmine 以便在测试块之间等待控制流清空,因此这可能会起作用。

记住expect还进行了修补以处理承诺,因此您无需将其放在then.

UPDATE:

事实上,你不应该需要beforeEach上面,它也应该像这样工作:

var initialFileCount;

it('Should attach a file when a file is selected and OK button is pressed.', function () {
    viewMeetingTabPage.getMeetingAttachmentCount().then(function(count) {
        initialFileCount = count;
    });
    viewMeetingPage.clickAddAttachmentButton();
    addAttchmentPage.attachFile();
    addAttchmentPage.clickConfimAttachFileButton();
    expect(viewMeetingTabPage.getMeetingAttachmentCount()).toBe(initialFileCount + 1);
});

这就是所谓的框架WebDriverJS 用户指南 https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#framing.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Protractor - 当 DOM 元素更改时,页面对象不会更新 的相关文章

随机推荐

  • 如何检查自定义android是否已root?

    我们购买了一些定制的 Android 平板电脑 我们将把它们用作信息亭平板电脑 为了使我们的自助服务终端应用程序正常工作 该应用程序需要 root 访问权限 如何检查设备是否已正确 root 如果不是的话我该如何root它 表明设备已获得
  • 使用 Pow on Rails 4 进行遥控撬

    我正忙于 最终 升级到 Rails 4 并且遇到了 Pry remote 的问题 Problem 添加时binding remote pry对于我的代码 它会破坏代码 但是当我无法通过键入进入调试器时pry remote 这曾经在运行 Ra
  • 如何检查多维数组是否为空?

    基本上 我有一个多维数组 我需要检查它是否为空 我目前有一个if声明尝试这样做 if empty csv array My code goes here if the array is not empty 虽然 那if statement无
  • 在Android上解密“SunJCE”AES加密数据

    我们需要编写一些 Android 代码来解密从我们的服务器发送的一些数据 我们的服务器团队为我们提供了一些使用 SunJCE 提供程序的示例解密代码 遗憾的是 Android 上不存在该提供程序 Cipher cipher Cipher g
  • mingw C++ 无法编译 j0 函数

    我正在尝试使用 MingW msys2 在 Windows 上编译程序 但由于 j0 函数而失败 在Linux上编译没有问题 当我在编译器上使用 std c 11 标志时 它似乎很讨厌 如何正确编译并打开 std c 11 标志 示例代码
  • jar 内的文件对于 spring 不可见

    All 我创建了一个 jar 文件 其中包含以下 MANIFEST MF Manifest Version 1 0 Ant Version Apache Ant 1 8 3 Created By 1 6 0 25 b06 Sun Micro
  • 基于傅里叶变换创建 iPhone 音乐可视化工具

    我正在为 iPhone 设计一个音乐可视化应用程序 我想通过 iPhone 的麦克风采集数据 对其运行傅里叶变换 然后创建可视化来实现这一点 我能得到的最好的例子是奥里奥图奇 http developer apple com library
  • 从 Android 开始:Java 或 Python (SL4A)

    我刚刚订购了一部 Android 智能手机 并想开始尝试创建自己的应用程序 现在的问题是使用哪种语言 是原生 Java 还是使用 SL4A 以前的 ASE 的 Python 我倾向于使用 Python 因为我比 Java 更了解它 但我想知
  • 401 Unauthorized 与 403 Forbidden:当用户未登录时,哪个是正确的状态代码? [复制]

    这个问题在这里已经有答案了 经过大量谷歌搜索和 Stackoverflowing 后 我仍然不清楚 因为许多文章和问题 答案太笼统了 包括403 Forbidden 与 401 Unauthorized HTTP 响应 https stac
  • R ggplot:图例周围的线

    我正在尝试使用 ggplot2 进行数据绘图 出于纯粹肤浅的原因 我想在图例周围画一条线 以便更好地将其与绘图区分开来 即图例框周围的黑色轮廓 我在任何论坛上都找不到这个问题的答案 但也许你有一个提示 library ggplot2 Res
  • $setIsSubset 用于 Mongo 中的常规查询

    我想做相当于 setIsSubset http docs mongodb org manual reference operator aggregation setIsSubset http docs mongodb org manual
  • 混合模板函数重载和继承

    打印以下代码 generic overload 但我想要的是在这两种情况下都调用重载或专业化 而不是通用的 我并不是想将重载与模板专业化混合在一起 它们在一起是因为没有一个按我的预期工作 有什么模板魔法可以实现这一点吗 include
  • 当我使用 Validator.TryValidateObject 时验证不起作用

    DataAnnotations 不适用于好友类 以下代码始终验证 true 为什么 var isValid Validator TryValidateObject new Customer Context results true 这是好友
  • 添加自定义 DLL 搜索路径@应用程序启动

    我正在绞尽脑汁试图想出一个优雅的解决方案来解决 DLL 加载问题 我有一个应用程序静态链接到加载 DLL 的其他 lib 文件 我没有直接加载 DLL 我希望在可执行文件所在的文件夹之外的另一个文件夹中拥有一些 DLL 例如 working
  • RabbitMQ:快速生产者和慢速消费者

    我有一个应用程序 它使用 RabbitMQ 作为消息队列在两个组件 发送者和接收者 之间发送 接收消息 发送者以非常快的方式发送消息 接收方收到消息后会做一些非常耗时的工作 主要是数据量非常大的数据库写入 由于接收方需要很长时间才能完成任务
  • 方法 JPQL 的查询验证失败

    我正在实现一个查询 该查询返回自定义对象中的输出 通过我的实现 我收到一个错误 方法公共抽象java util List org degs repository ConsolidateresponseRepository transacti
  • 可与类型索引中的和和积合并

    Haskell 中是否有类似于以下类型类的内容 class Mergeable f Type gt Type gt Type where merge f a b gt f c d gt f a c Either b d 特别是 想象有一个S
  • 在使用 VBA 填充列表的 Excel 中输入下拉列表时自动完成

    我正在使用下面的代码将数据从另一张表插入到下拉列表中 当用户从另一个下拉列表中选择某个选项时 即可实现这一点 lstRow Sheets Data Sheet Range D Rows Count End xlUp Row Sheets D
  • 如何使用 grep 查找单词列表

    我有一个文件 A 其中有 100 个单词 并用换行符分隔 我想搜索文件 B 以查看文件 A 中的任何单词是否出现在其中 我尝试了以下方法 但对我不起作用 grep F A B 您需要使用该选项 f grep f A B 选项 F进行固定字符
  • Protractor - 当 DOM 元素更改时,页面对象不会更新

    我正在测试使用 angular js 构建的 SPA 并使用页面对象模式来编写我的测试 在应用程序中 我们有许多将要更新的列表 例如 有一个附件列表 当添加 删除附件时 该列表将会更新 要添加附件 我们有一个模式窗口 当我们上传文件并单击