Reputation: 1281
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");
});
});
Upvotes: 1
Views: 545
Reputation: 473833
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);
});
Upvotes: 2