codeceptjs / CodeceptJS

Supercharged End 2 End Testing Framework for NodeJS
http://codecept.io
MIT License
4.11k stars 724 forks source link

how to implement if else conditional in test #3458

Closed Maxianrodrigues closed 1 year ago

Maxianrodrigues commented 1 year ago

What are you trying to achieve?

I need to create a condition to perform a procedure if an alert appears and if it doesn't, follow the test

What do you get instead?

The test breaks when it enters the if

Provide console output if related. Use --verbose mode for more details.

# paste output here
Eu aguardo pelo elemento "//android.widget.TextView[@text="Normal"]", 5
      Eu vejo elemento "//android.widget.TextView[@text="Normal"]"
      Eu tap "//android.widget.TextView[@text="Normal"]"
    login_page: validarbase 
      Eu aguardo pelo elemento "#br.com.maximasistemas.maxpedido:id/checkDadosPedido", 5
    [1] Retrying... Attempt #2
    [1] Retrying... Attempt #3
    [1] Retrying... Attempt #4
    [1] Error | Error: element (//*[@resource-id='br.com.maximasistemas.maxpedido:id/checkDadosPedido']) still not present on page after 5 sec
    [1] Error | Error: element (//*[@resource-id='br.com.maximasistemas.maxpedido:id/checkDadosPedido']) still not present on page after 5 sec

Provide test source code if related

// paste test
const { I } = inject();

module.exports = {

  // insert your locators and methods here
  buttons: {
    ok:'#android:id/button3',
    ocultardadospedido: '#br.com.maximasistemas.maxpedido:id/checkDadosPedido',
    ocultardadoscliente: '#br.com.maximasistemas.maxpedido:id/checkDadosCliente',
  },

  checkaberturasucess() {
    I.waitForElement (this.buttons.ok, 5)
    I.seeElement (this.buttons.ok)
    I.wait(4)
    I.tap(this.buttons.ok)
    I.wait(4)
  },

  validarbase() {
    (async () => {  
      try {
        if(I.waitForElement(this.buttons.ocultardadospedido, 5)){
          I.tap(this.buttons.ocultardadospedido)
          I.wait(3)
          I.seeElement(this.buttons.ocultardadoscliente)
          I.tap(this.buttons.ocultardadoscliente)
          console.log('base atualizada')
        }
        else {
          I.sendDeviceKeyEvent(4)
          console.log('base desatualizada faça sinc')
        }
      } catch(e) {
        console.log('não foi possivel validar')
      }
    })();
  },
}

Details

# paste config here

exports.config = {

   //#### CENÁRIO MÁQUINA MAXIAN
    tests: './steps/*_test.js',
  output: './output',
  helpers: {
    Appium: {
      app: 'C:/Users/maxia/Documents/maxsolucoes/maxpedido.apk',
      platform: 'Android',
      desiredCapabilities: {
        appPackage: "br.com.maximasistemas.maxpedido",
        appActivity: "ui.activities.SplashActivity",
        autoGrantPermissions: "true",
        fullReset: "false",
        noReset: "true",
        device: 'POCO X3 Pro',
        //device: 'Pixel4',
        //plataformVersion: "9",
        plataformVersion: "12",
        automationName: 'UiAutomator2',
        enforceAppInstall: "false",

    //#### CENÁRIO MÁQUINA MAXIAN

    /* #### CENÁRIO MÁQUINA RAFAEL 

      //  tests: './*_test.js',
        output: './output',
        helpers: {
  Appium: {
         app: 'C:\\TestesMaxPedidoAutomação\\maxpedido-winthor-v3-teste\\apk\\maxpedido-3.38.6-debug.apk',
        platform: 'Android',
        desiredCapabilities:{
        autoGrantPermissions:"true",
        fullReset: "false",
        noReset: "true",
        appPackage:"br.com.maximasistemas.maxpedido",
        appActivity: "ui.activities.SplashActivity",
        device: "Xiaomi Redmi Note 9S",
        platforVersion:"11",
        automationName: 'UiAutomator2',*/

      }
    }
  },
  include: {
    I: './steps_file.js',
    login_page: './pages/login_page.js',
    pedido_page: './pages/pedido_page.js',
    condicoespedido_page: './pages/condicoespedido_page.js',
    adicionaproduto_page: './pages/adicionaproduto_page.js',
    modulopedido_page: './pages/modulopedido_page.js',
    manterdescinformado_page: './pages/manterdescinformado_page.js',
    manterdescinformadoqt_page: './pages/manterdescinformadoqt_page.js',
    manterdescinformadoqt2_page: './pages/manterdescinformadoqt2_page.js',
    duplicarpedidoclibloq_page: './pages/duplicarpedidoclibloq_page.js',
    manterdescinformadoplpag561_page: './pages/manterdescinformadoplpag561_page.js',
    duplicarpedidorestricaovenda391_page: './pages/duplicarpedidorestricaovenda391_page.js',
    validatelamixcliente_page: './pages/validatelamixcliente_page.js',
    validaexclusaoitenscampanhaMIQFI_page: './pages/validaexclusaoitenscampanhaMIQFI_page.js',
    adcionaprodutocampanha3306_page: './pages/adcionaprodutocampanha3306_page.js',
    duplicpedidoselecaomultipla_page: './pages/duplicpedidoselecaomultipla_page.js',
    gestosdetela_page: './pages/gestosdetela_page.js',
    removeprodutos_page: './pages/removeprodutos_page.js',
  },
  mocha: {},
  bootstrap: null,
  teardown: null,
  hooks: [],
  gherkin: {
    features: './features/*.feature',
    steps: ['./step_definitions/steps.js']
  },
  plugins: {
    screenshotOnFail: {
      enabled: true
    },
    pauseOnFail: {},
    retryFailedStep: {
      enabled: true
    },
    tryTo: {
      enabled: true
    }
  },
 // tests: './steps/*_test.js',
  name: 'maxPedido',
  translation: 'pt-BR'
}
kobenguyent commented 1 year ago

hey @Maxianrodrigues you can try this for your if else conditional in your test

let numOfElements = await I.grabNumberOfVisibleElements('p');
if (numOfElements > 0) {
 /// do something
} else {
  /// do something else
}