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

Tell us what’s happening:

I can’t pass the freecode camp test in the last two exercise. The test I made with my code are all ok when I run it, but I can’t pass the freecodecamp #3 and #4 test.

My code

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)
        .keepOpen()
        .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)
        .keepOpen()
        .get("/hello?name=Joaquin")
        .end(function (err, res) {
          assert.equal(res.status, 200);
          assert.equal(res.text, "hello Joaquin");
          done();
        });
    });
    // #3
    test('Send {surname: "Colombo"}', function (done) {
      chai
        .request(server)
        .keepOpen()
        .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)
        .keepOpen()
        .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 = "http://localhost:3000";

suite("Functional Tests with Zombie.js", function () {
  const browser = new Browser();

  suiteSetup(function (done) {
    return browser.visit("/", done);
  });

  this.timeout(10000);

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

  test('Submit the surname "Colombo" in the HTML form', function (done) {
    browser
      .fill("surname", "Colombo")
      .then(() => browser.pressButton("submit"))
      .then(()=>{
        console.log("Contenido de span#name:", browser.text("span#name"));
        console.log("Contenido de span#surname:", browser.text("span#surname"));
      })
      .then(() => {
        browser.assert.success();
        browser.assert.text("span#name", "Cristoforo");
        browser.assert.text("span#surname", "Colombo");
        browser.assert.elements("span#dates", 1);
        done();
      })
  });
  // #6
  test('Submit the surname "Vespucci" in the HTML form', function (done) {
    browser
      .fill("surname", "Vespucci")
      .then(() => browser.pressButton("submit"))
      .then(() => {
        browser.assert.success();
        browser.assert.text("span#name", "Amerigo");
        browser.assert.text("span#surname", "Vespucci");
        browser.assert.elements("span#dates", 1);
        done();
      });
  });
});

I put two console log in the #5 test so I manually check if the name get submit correct and it work.
I don’t know how to make pass the freecodecamp test. When I run it’s ok.

Plase send help! :smiling_face_with_tear:

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0

Challenge Information:

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

Hi @joaquinlallana12

Try wrapping the surname in quote marks.

Happy coding

I did it. But doesn’t change anything :man_shrugging:

I can’t pass this test yet. Can someone help me?

Within the then callback, the pressButton method of the browser object is used to invoke the form’s submit event listener.

Your code should have a similar structure to the example code.