4
\$\begingroup\$

I am using the function below to change the environment. If it sees the wrong environment values, it clicks on it and it changes the environment.

In this function, I am just using if and else statement. Is this a good code practice? This does the job however it does not seem clean.

class SettingsPage extends Page {

changeEnvironment(appName) {
        if (appName.$(PreferenceScreen.environmentProduction).isDisplayed()) {
            appName.$(PreferenceScreen.environmentProduction).click();
        }
        else if
            (appName.$(PreferenceScreen.environmentDevelopment).isDisplayed()) {
             appName.$(PreferenceScreen.environmentDevelopment).click();
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ wouldn't switch case be better if you have limited options? \$\endgroup\$
    – tsamridh86
    Dec 20, 2020 at 10:44

1 Answer 1

1
\$\begingroup\$

The built in JS array transformation functions could help you reduce duplicated code.

const environments = ['environmentProduction','environmentDevelopment'];

class SettingsPage extends Page {
    
    changeEnvironment(appName) {
        const all_buttons = environments.map(
            env=>appName.$(PreferenceScreen[env])
        );
        const displayed_buttons = all_buttons.filter(button=>button.isDisplayed());
        displayed_buttons.forEach(button=>button.click());
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I would go for const displayedButton = allButtons.find(button=>button.isDisplayed()); \$\endgroup\$
    – konijn
    Jul 23, 2020 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.