Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionnal test based on element visibility #648

Closed
adeleco opened this issue Aug 3, 2017 · 6 comments
Closed

Conditionnal test based on element visibility #648

adeleco opened this issue Aug 3, 2017 · 6 comments
Labels

Comments

@adeleco
Copy link

adeleco commented Aug 3, 2017

What are you trying to achieve?

Conditionnal code in a scenario, based on the visibility of an element.

Questions

  1. How to use a conditional testing (if / else)
    If (I.seeElement) {
    // code here
    }
    else {
    // other code here
    }

  2. Is it possible to use exceptions management (try / catch / finally)
    try{
    // code here
    I.seeElement(locator);
    }catch(exception){
    // other code here
    }

best regards
adeleco

@grantvanhelsing
Copy link

@adeleco did you find a way to do this?

@DavertMik
Copy link
Contributor

Conditionals are not allowed in test scenarios. If you really need them you should implment them inside helpers. You will need to implment them using promises inside helpers.

But better don't do them at all in tests

@adeleco
Copy link
Author

adeleco commented Nov 27, 2017

I haven't had time to do this yet.

@1vanhelsing1 if you do this please post a sample.

@adeleco
Copy link
Author

adeleco commented Jan 19, 2018

@1vanhelsing1
I'm back on testing. Have you wrote something about conditional testing ?

@reubenmiller
Copy link
Collaborator

reubenmiller commented Feb 26, 2018

Here is an example showing a conditional action in a Helper.

Custom Helper Example

'use strict';
import assert from 'assert';

let Helper = codecept_helper;

class MyHelper extends Helper {
  async clickIfVisible(selector, ...options) {
    const helper = this.helpers['Puppeteer'];
    try {
      const numVisible = await helper.grabNumberOfVisibleElements(selector);

      if (numVisible) {
        return helper.click(selector, ...options);
      }
    } catch (err) {
      console.log('Skipping operation as element is not visible');
    }
  }
}

module.exports = MyHelper;

@vaijiNeel
Copy link

Hey, how do we call this helper method from another class? I need to check Checkbox only if it is not checked already. How do I implement this in CodeceptJS with webDriverIO? Appreciate any help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants