0

My question seems related to the below question, but I didn't quite get the solution provided Why is console.log() not printing anything?

When I run this program, it prints logs perfectly

const x = 'Hello';
const y = 'World!';
console.log(x, y);

But when I try this, it doesn't print console.log

Given("My demo test", async function (string, string2) {
    console.log("**************** Test Started ********************")

    if(string === "msn"){
        string = "https://msn.com";
    } else if (string === "google"){
        string = "https://google.com";
    }
    gotoPage(string)
    const imageLinkText = getText(':nth-child(2) > .gb_q')
    console.log(" **************** " + imageLinkText + " **************** " )
})

How can I get console.log output in the mac terminal?

I am using the below command to run my cypress tests in the chrome browser

cypress run --headed chrome --env tags="@smoke"

paul
  • 4,333
  • 16
  • 71
  • 144
  • You should try `cy.log(...)` or emitting the console.log from the Cypress queue `cy.then(() => console.log(...))`. – Put that sock away Mar 09 '23 at 09:49
  • 1
    I can't see `getText(':nth-child(2) > .gb_q')` actually returning text, most likely it's an object. Try `getText(':nth-child(2) > .gb_q').then(text => console.log(text))`. – Put that sock away Mar 09 '23 at 09:51
  • `cy.log()` gives logs in the browser console but I want logs in bash or mac terminal. getText() is a function`return cy.get(selector).invoke("text");` – paul Mar 09 '23 at 10:00
  • 1
    `cy.get(selector).invoke("text")` does not give you text, it gives you a Chainer object. You will not see anything useful in the console, even if you get it to print. – TesterDick Mar 09 '23 at 10:29
  • Even if I leave the element's text, general logging is also not working. I tried `cy.then(() => console.log("**************** Test Started ********************"));` and `console.log("**************** Test Started ********************")` – paul Mar 09 '23 at 10:33
  • 2
    When using Cypress run mode you will need to log via a task - please see the duplicate question for details. Note the task setup has changed since that question, you can find details of current implementation [here](https://docs.cypress.io/api/commands/task). – Fody Mar 09 '23 at 10:46
  • @Fody thanks it worked. Only thing is invoke method return an object not text. – paul Mar 09 '23 at 12:12

0 Answers0