skip to Main Content

Thats what i have when i run tests on github, tests without login passed

The request we sent was:

Method: POST
URL: https://myurl.com/app-auth/login
Headers: {
  "Connection": "keep-alive",
  "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/112.0.5615.49 Safari/537.36",
  "accept": "*/*",
  "content-type": "application/x-www-form-urlencoded",
  "accept-encoding": "gzip, deflate",
  "content-length": 0
}
Body: 

-----------------------------------------------------------

The response we got was:

Status: 422 - Unprocessable Entity
Headers: {
  "server": "nginx",
  "date": "Wed, 12 Apr 2023 16:08:25 GMT",
  "content-type": "application/json",
  "content-length": "172",
  "connection": "keep-alive"
}
Body: {
  "detail": [
    {
      "loc": [
        "body",
        "username"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [
        "body",
        "password"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}

Thats my secret in github

{
    "username": "test1",
    "password": "test2"
}

Thats my yml

name: Cypress Tests

on:
  push:
    branches: [testBranch]

jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Cypress run
        uses: cypress-io/github-action@v3
        with:
          browser: chrome
          env: true
        env: 
         CYPRESS_USERNAME: ${{ secrets.CYPRESS_USER.username }}
         CYPRESS_PASSWORD: ${{ secrets.CYPRESS_USER.password }}

Thats my test

Cypress.Commands.add('apiLogin', () => {
  const username = Cypress.env("CYPRESS_USERNAME");
  const password = Cypress.env("CYPRESS_PASSWORD");
  let access_token = ''
  return cy
    .request({
      method: 'POST',
      url: "https://myurl.com/app-auth/login",
      form: true,
      body: {
        username: username,
        password: password
      }
    }).then(response => {
      expect(response.status).to.equal(200)
      access_token = response.body.access_token;
      sessionStorage.setItem('access_token', access_token)
    })
})

What is wrong? Everything works fine when i declare variables locally in cypress.env.json, but i need to use github variables.
Github variables are not readed, what i need to change?

2

Answers


  1. Chosen as BEST ANSWER

    Closed, workmate just created normal variable not secret that was the problem :D


  2. In your test you must use the environment variable without the CYPRESS_ prefix, like this:

    const username = Cypress.env("USERNAME");
    const password = Cypress.env("PASSWORD");
    

    Cypress automatically removes the leading CYPRESS_ or cypress_
    from any environment variable name specified in this way.

    Source: https://docs.cypress.io/guides/guides/environment-variables#Option-3-CYPRESS_

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search