【问题标题】:Protractor - Find all elements and loop length of found elements and click button量角器 - 查找所有元素和找到的元素的循环长度并单击按钮
【发布时间】:2020-02-12 13:36:25
【问题描述】:

所以我一直试图弄清楚如何根据 find All 元素的数量单击按钮 x 次。这意味着如果有 3 个元素由相同的类名找到,那么我们循环 3 次,这应该点击按钮 3 次。

我做过这样的事情:

(新的更新,检查底部的编辑帖子)

通常element.all(by.className('btn btn-remove btn-outlined')).getText() 是 3,但可以更改为 6 和随机数,所以我的想法是首先阅读 HTML 中有多少 btn btn-remove btn-outlined,然后多次单击找到的元素。因此,如果找到 3 个,则单击该按钮 3 次。

但是现在的问题是它要找到有多少元素。它也循环了很多次,以 text.length 的形式给出,但在循环内部,由于某些原因,它似乎跳过了 it 函数,我不知道为什么

现在它确实找到了 3 个元素的长度,它似乎循环了很多次,但它跳过了它的功能(这部分):

      it('Click remove button - ' + i + '/' + text.length, function (done) {

            browser.driver
                .then(() => browser.executeScript("arguments[0].click();", element.all(by.className('btn btn-remove btn-outlined').first().getWebElement())));
                .then(() => done());
        });

        it('Wait for fading button to be gone', function (done) {

            setTimeout(function () {
                done();
            }, 1000);

        });

我很害怕我可能做错了我不知道的事情。

如何找到 DOM 中有多少给定元素并循环多次 + 单击删除按钮?

编辑代码:

it('Click remove button', function (done) {

    element.all(by.className('btn btn-remove btn-outlined')).getText().then(function (text) {
        console.log(text.length) //returns 3
        for (var i = 0; i < 4; i++) {

            console.log(i); //Does print 0 1 2

            it('Click remove button - ' + i + '/' + text.length, function (done) {

                console.log("Remove button"); //Doesnt print

                browser.driver
                    .then(() => browser.executeScript("arguments[0].click();", element.all(by.className('btn btn-remove btn-outlined').first().getWebElement())));
                    .then(() => done());
            });

            it('Wait for fading button to be gone', function (done) {

                console.log("TIme out"); //Doesnt print

                setTimeout(function () {
                    done();
                }, 1000);

            });
        }
    })
    done();
});

【问题讨论】:

  • 您有 3 个名为 it 的函数,这可能会导致范围问题。在每个it 函数中添加一个打印语句,以查看正在调用的内容。我建议选择新名称。
  • 另外,您正在定义 it 函数,但它们在哪里被调用?
  • 我认为它实际上是一个 Mocha/Chai 函数。也许我应该提到它,但我会在每个上打印 console.log 并查看它的响应@Jortega
  • @Jortega 我刚刚更新了代码。我通过每个 console.log 添加了 cmets。似乎it 函数永远不会在循环内开始。

标签: javascript selenium protractor mocha.js chai


【解决方案1】:

你的it里面的for循环没有执行的原因是那些it在运行时动态生成并且没有被测试框架,Jasmine,Mocha尊重。

据我所知,Jasmine 在执行测试脚本文件之前需要加载和解析静态it,在加载和解析阶段生成的动态it 将被忽略。因此您需要删除动态it

试试下面的代码

it('Click remove button', function (done) {

    let allBtns = element.all(by.className('btn btn-remove btn-outlined'));

    allBtns.count()
    .then(function (cnt) {

        console.log('Find buttons:', cnt)

        for (let i = 0; i < cnt; i++) { // important to use let but var here.
            console.log('Remove button - ' + i + '/' + cnt);
            browser.executeScript("arguments[0].click();", allBtns.get(i).getWebElement())
            browser.sleep(1000) // sleep 1s
        }
    })
    .then(()=>{
        done();
    })

});

【讨论】:

  • 天哪!这正是我想要的!我真的很欣赏它!将其标记为答案,我越来越理解它。似乎你不能嵌套它我认为你可以的函数。
  • 你可以嵌套it,但它们应该是staticstatic 这里的意思是当 Jasmine 在任何它被执行之前加载测试脚本文件,在解析 Jasmine 之后会知道给定的测试脚本文件中有多少它,只有这些它是静态的。在您的代码中,Jasmine 在加载和解析时无法计算循环内的it,因为只有在 Jasmine 执行此脚本文件时,您才能知道那里有多少个按钮以及应该增加多少个动态按钮。但茉莉会忽略它们
  • 哦,对了,我正在使用 mocha 和 chai,但我相信它是一回事。但我现在确实明白了原因并明白了如何解决它。我确实有另一个问题,但我目前正在另一个线程上写它,你想让我给你解决吗?我会说它是相似的,但更高级。如果你有时间?因为我已经输入了这个作为正确答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多