Quality Assurance and Testing with Chai - Run Functional Tests Using a Headless Browser

Tell us what’s happening:
Describe your issue in detail here.
I can’t pass test 5 and 6 in the 2_functional_tests.js, I get an AssertionError: no open window with html document. I checked the forums and saw that some other people had this exact issue but I could find the right solution. In one of the forums it was suggested to use local resources to pass these challenges. So I tried to download the zip from replit and when I try to run the code in my vs code I get ReferenceError: suite is not defined… Any help would be appricated.

Your code so far

const chai = require('chai');
const assert = chai.assert;

const server = require('../server');

const chaiHttp = require('chai-http');
chai.use(chaiHttp);

suite('Functional Tests', function () {
  this.timeout(5000);
  suite('Integration tests with chai-http', function () {
    // #1
    test('Test GET /hello with no name', function (done) {
      chai
        .request(server)
        .get('/hello')
        .end(function (err, res) {
          assert.equal(res.status, 200);
          assert.equal(res.text, 'hello Guest');
          done();
        });
    });
    // #2
    test('Test GET /hello with your name', function (done) {
      chai
        .request(server)
        .get('/hello?name=xy_z')
        .end(function (err, res) {
          assert.equal(res.status, 200);
          assert.equal(res.text, 'hello xy_z');
          done();
        });
    });
    // #3
    test('Send {surname: "Colombo"}', function (done) {
      chai
        .request(server)
        .put('/travellers')
        .send({
          "surname": "Colombo"
        })
        .end(function (err, res) {
          assert.equal(res.status, 200);
          assert.equal(res.type, 'application/json');
          assert.equal(res.body.name, 'Cristoforo');
          assert.equal(res.body.surname, 'Colombo');
          done();
        });
    });
    // #4
    test('Send {surname: "da Verrazzano"}', function (done) {
      chai
        .request(server)
        .put('/travellers')
        .send({
          "surname": "da Verrazzano"
        })
        .end(function (err, res) {
          assert.equal(res.status, 200);
          assert.equal(res.type, 'application/json');
          assert.equal(res.body.name, 'Giovanni');
          assert.equal(res.body.surname, 'da Verrazzano');
          done();
        });
    });
  });
});

const Browser = require('zombie');
Browser.site = '0.0.0.0:3000';//'https://boilerplate-mochachai.garikhk.repl.co';

suite('Functional Tests with Zombie.js', function () {
  this.timeout(5000);

  const browser = new Browser();
  suiteSetup(function(done) {
    browser.visit('/', done);
  });

  suite('Headless browser', function () {
    test('should have a working "site" property', function() {
      assert.isNotNull(browser.site);
    });
  });

  suite('"Famous Italian Explorers" form', function () {
    // #5
    test('Submit the surname "Colombo" in the HTML form', function (done) {
      browser.fill('surname', 'Colombo').then(() => {
        browser.pressButton('submit', () => {
          browser.assert.success();
          browser.assert.text('span#name', 'Cristoforo');
          browser.assert.text('span#surname', 'Colombo');
          browser.assert.element('span#date', 1);
          done();
        });
      });
    });
    // #6
    test('Submit the surname "Vespucci" in the HTML form', function (done) {
       browser.fill('surname', 'Vespucci').then(() => {
        browser.pressButton('submit', () => {
          browser.assert.success();
          browser.assert.text('span#name', 'Amerigo');
          browser.assert.text('span#surname', 'Vespucci');
          browser.assert.element('span#date', 1);
          done();
        });
      });
    });
  });
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Quality Assurance and Testing with Chai - Run Functional Tests Using a Headless Browser

Link to the challenge:

Could you supply the link to your replit so we can review the errors you are getting? (not the link you submit to FCC, but the link to your actual code, the URL in your browser you see while editing)

Also, do you have your VSCode setup to run node.js? I think you have to have npm or something installed, but I could be wrong on that. And then to run the code, you don’t just run the local .js file, you need to start the node.js server.

Ah sorry I forgot,
here is the link boilerplate-mochachai - Replit

It should download the dependencies but I wouldn’t trust it too much. Sometimes you get an error log text file with the zipped files that tells you some dependencies didn’t get zipped.

Delete the node_modules folder and run npm i inside the root folder (assuming you have Node.js installed which you need).

Ok I fixed everything to run the script, but now I get this error :

  Uncaught AssertionError [ERR_ASSERTION]: 1
  + expected - actual

  -0
  +1

Here is my github repo: GitHub - GarikHk/qa_and_testing_with_chai: Quality Assurance (freeCodeCamp challenge)

Ok, the problem is I wrote ‘span#date’ instead of ‘span#dates’, Pretty stupid mistake…

Now everything seems to work fine on my end but freeCodeCamp’s last test fails

image

Hum… code looks good. What shows on your console while the tests are being ran from the fCC submission site?

nothing changes I guess…

Check the method you are using is the correct one.

assert.element(selection, message)
assert.elements(selection, count, message)

The method that accepts the count argument is elements(), not element()

The repl works (I forked, fixed, and tested), on replit, using the actual site URL, if you follow the setup instructions exactly (your code is close but not exact). Like others with similar issues, you have some things that should be at the root level located elsewhere, functions called where they should be passed, and missing done() calls in the asynchronous tests. I’ve posted links to working implementations in other discussions if you really have to see the code.

Getting the fCC tests to actually pass with a repl usually requires you to start your server in one tab and immediately run the tests in another. It sometimes takes a few tries to get the timing just right.

Interestingly, the element/elements assertion works both ways; the wrong element way just uses 1 as a very unhelpful error message.