TransWikia.com

Changing the environment if it sees the incorrect environment in the setting

Code Review Asked by cva6 on December 10, 2021

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();
        }
    }
}

One Answer

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());
    }
}

Answered by Ted Brownlow on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP