Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor: there is an error but the test is passed

I'm creating a test tool using protractor and other library like chai, cucumber and gherkin. I have three files:

  1. my_feature.feature - used to specify gherkin features
  2. test_step.js - this file contains the step definitions
  3. index.html - is the web page I'm testing

my_feature.feature

# features/my_feature.feature

Feature: Test cucumber automation
  As a front-end developer
  I want to automate e2e testing

  
  Scenario: Altro test
    Given I go on "file:///Users/Emanuele/Desktop/lavoro/test-automation/app/html/index.html"
    Then The text of the element with selector "#test-button" should be "My button"
    

test_step.js

    'use strict';

var chai = require('chai'),
    chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

var expect = chai.expect;

// Protractor won't wait for Angular
browser.ignoreSynchronization = true;

module.exports = function() {
  
  this.World = require('../support/world').World;

  // default timeout for all step definitions
  this.setDefaultTimeout(20 * 1000);

  /*
  ** Open a page based on an absolute url
  ** Ex. https://www.google.com
  */
  this.Given(/^I go on "([^"]*)"$/, function(url, callback){
    browser.get(url);

    callback();
  });

  /*
  ** Check if the text of the element with a given selector is equal to myText.
  */
  this.Then(/^The text of the element with selector "([^"]*)" should be "([^"]*)"$/, function(elementSelector, myText, callback){
    var selectedElement = element(by.css(elementSelector));

    //Resolve the promise
    selectedElement.getText().then(function(text){
      expect(text).to.equal(myText);
    });
    

    callback();
  });
};

index.html

<html>
<head></head>
<body>

    <button id="test-button">Test button</button> 

</body>
</html>

Now, when I run the test I get a weird result because the two steps for the given scenario are both passed but I have an error in the second one due to expect(text).to.equal(myText); line. This happens because, according with the gherkin feature, the text inside the button should be My button and not Test button.

Here you can find the result shown in my console: enter image description here

I would like to know why the second step is passed even if there is an error? I think that the test should fail because the compared strings are different. Am I wrong? How can I avoid this behaviour?

EDIT:

If i resolve the promise using eventually from chai-as-promised I get the same result. All tests are passed but the error text is a little different: AssertionError: expected 'Test button' to equal 'My Button'

Thank you in advance.

like image 327
Ema.jar Avatar asked Dec 12 '25 13:12

Ema.jar


2 Answers

I've found two fixes for this problem.

First:

selectedElement.getText().then(function(text){
      try{
        expect(text).to.equal(myText); 
      } catch(error){
        callback(new Error("Compare error"));
      }
      callback(); 
});

Here I resolve the promise then I check if the text of the element is equal to the string myText. I don't like this solution even because, resolving the promise manually, I don't use a lot of feature of chai-as-promised.

Second:

expect(selectedElement.getText()).to.eventually.equal(myText).notify(callback); 

I think this is the best solution and is based on notify provided by chai-as-promised. This post about cucumber-protractor has been very helpful for this problem.

like image 142
Ema.jar Avatar answered Dec 15 '25 03:12

Ema.jar


What's happening here is that because your assertion is in the callback of an asynchronous operation, the test completes before encountering the expect(text).to.equal(myText); statement and therefore passes.

You need to identify that the test shouldn't pass until the promise you're waiting on is resolved!

According to the Chai documentation on promises the pattern I believe you should be using is:

expect(selectedElement.getText()).to.eventually.equal(myTest);
like image 34
vpiTriumph Avatar answered Dec 15 '25 04:12

vpiTriumph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!