0

I am using Cypress to test this feature: I need to fetch an OTP from another URL in order to complete the deletion of a bucket. The rest of the script runs perfectly, except for the last line, where it should type the OTP into the field - if I leave the line where it is now, cypress does not run/see it, and if I move it down, the variables are deemed "undefined".

ps - URL to Ops platform removed and usernames are defined in another file.

Can someone please help me figure out where I am going wrong?

    describe('Delete Bucket', function() {

   it('---4. Deletes a bucket', function() {
    //Click on trash bin icon
    cy.get('[data-testid=bucket-delete-0]').click();
    //Click on Continue
    cy.get('[data-test="continue-bucket-delete"]').click({timeout:10000}).then(() => {
      cy.fixture('users').then((users) => {
        let username = users.paid.username;
        let password = users.paid.password;
        var url = "URL to our internal OPS platform from where OTP is fetched";
        new Cypress.Promise((resolve, reject) => {
          var data = {
            "username": username,
            "password": password,
          };
          var formBody = [];
          for (var property in data) {
            var encodedKey = encodeURIComponent(property);
            var encodedValue = encodeURIComponent(data[property]);
            formBody.push(encodedKey + "=" + encodedValue);
          }
          formBody = formBody.join("&");
          fetch(url, {
            method: 'POST',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: formBody
          })
          .then((resp) => {
            resp.json()
            .then((data) => {
//THIS IS THE PROBLEMATIC LINE - CURRENTLY NOT PICKED UP BY CYPRESS
              **cy.get('[name=otp]').type(otp=["sms"]).tab().tab().click({force:true});**
            }); //close fixture 
          });    
        }); 
      });
    });
    }); //close it
}); //close describe ````
Joshua
  • 3,055
  • 3
  • 22
  • 37

1 Answers1

0

cy.type("some text") expects a simple String as its argument. https://docs.cypress.io/api/commands/type.html#Syntax

You (maybe by mistake?) seem to pass an assignment: type(otp=["sms"]) which would pass the left hand side of the assignment into the method. I don't think this is what you want.

I think what you acutally need is something like this:

.then((data) => {
  // assuming that data is an object with a field otp that has the one time password as its String value
  cy.get('[name=otp]').type(data.otp).tab().tab().click({force:true});
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Robert
  • 1,579
  • 1
  • 21
  • 36
  • Hi Dharman - thank you for the reply. Unfortunately your suggestion did not work. The problem is not with that specific line. Cypress does not even SEE that line. It stops after ```cy.get('[data-test="continue-bucket-delete"]').click({timeout:10000}).then(() => {```. What is puzzling me is why Cypress refuses to run the last assignment line. – Salomie Drake Jul 23 '20 at 10:04