简体   繁体   中英

How to properly store element count in Protractor


I'm pretty new to Protractor and I'm not satisfied with the way I wrote the code below (which is actually working).
I would like to simply store the count in a variable (here, "nb_before") and reuse it after, but the promise mechanism won't allow me to do it concisely, so I decided to put everything in the same function. But it's so ugly.
Am I missing something?
Best regards

    // We count the lines, we click on a button which adds a new line,
    // then we verify that the new line has a specific text.

    it("Test case", function () {
        browser.get(browser.baseUrl);
        var button = element(by.xpath("//whatever"));
        var rows = element.all(by.xpath("//whatever"));
        rows.count().then(function(count){
            nb_before = count;
            button.click(); // adds a line
            var nb_after = nb_before + 1;
            var new_line = element(by.xpath("//li[" + nb_after + "]"));
            expect(new_line.getText()).toEqual("yay");
        });
    });

It looks like you could've used .last() method here to get the last row:

it("Test case", function () {
    browser.get(browser.baseUrl);

    var button = element(by.xpath("//whatever"));
    var rows = element.all(by.xpath("//whatever"));

    button.click(); // adds a line

    expect(rows.last().getText()).toEqual("yay");
});

We can, though, get the count before and after the button click and check if it was incremented by one - this will still require us to use .then() to resolve the promise returned by .count() :

var button = element(by.xpath("//whatever"));
var rows = element.all(by.xpath("//whatever"));

rows.count().then(function (countBefore) {
    button.click();
    expect(rows.count()).toEqual(countBefore + 1);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM