使用Switch替换元素而不是if / elseif

时间:2016-01-13 14:37:37

标签: java webdriver

public void profileSection(String displayName, String fName, String lName, String Address_1,) throws Exception
{
    myAccountPageNative(CONSTANTs.MY_ACCOUNT_LINK);
    clickLink(CONSTANTs.MY_PROFILE_SECTION);        

    if(driver.findElement(By.cssSelector(CONSTANTs.DISPLAY_NAME_TXTBOX)).isDisplayed())
    {
        log.step("Optional..... BUT you will need it when Commenting on any article....");
        elementPresent_Click(By.cssSelector(CONSTANTs.DISPLAY_NAME_TXTBOX), displayName);
        Thread.sleep(2000);
    }else if(driver.findElement(By.cssSelector(CONSTANTs.MYACCT_EMAIL_TXTBOX)).isDisplayed())
    {
        log.step("Check if Email txtbox is enabled or not...... pls wait.");
        txtBoxDisabled(By.cssSelector(CONSTANTs.MYACCT_EMAIL_TXTBOX),null);
        Thread.sleep(2000); 
        log.step("Text box is disabled..... move to next step");
        Thread.sleep(2000);
    }else if(driver.findElement(By.cssSelector(CONSTANTs.FIRST_NAME)).isDisplayed())
    {
        log.step("Input First Name......");
        txtBoxDisabled(By.cssSelector(CONSTANTs.FIRST_NAME),fName);
        Thread.sleep(2000);
    }else if(driver.findElement(By.cssSelector(CONSTANTs.LAST_NAME)).isDisplayed())
    {
        log.step("Input Last Name......");
        txtBoxDisabled(By.cssSelector(CONSTANTs.LAST_NAME),lName);
        Thread.sleep(2000);
    }else if(driver.findElement(By.cssSelector(CONSTANTs.ADDRESS_1)).isDisplayed())
    {
        log.step("Input Address 1......");
        txtBoxDisabled(By.cssSelector(CONSTANTs.ADDRESS_1),Address_1);
        Thread.sleep(2000);
    }
        log.step("Click Save Changes Button......");
        txtBoxDisabled(By.cssSelector(CONSTANTs.SAVE_CHANGES_BTN),null);            
    }
  

请尝试使用开关而不是过多的if语句。只是为了让我的代码更具可读性。由于switch评估单个变量,我可以使用带有我的代码cos的开关,我觉得它看起来有点复杂。   请注意,因为所有都有使用css Selector选择的不同元素。

先谢谢。

3 个答案:

答案 0 :(得分:2)

我认为这可能是一个更合适和更优雅的解决方案。构建一个要检查的元素数组并循环遍历这些元素。

public void profileSection(String displayName, String fName, String lName, String Address_1, 
        String Address_2, String city, String country, 
        String postcode, String telephone) throws Exception
{
    myAccountPageNative(CONSTANTs.MY_ACCOUNT_LINK);
    clickLink(CONSTANTs.MY_PROFILE_SECTION);

    String [] elementNames = new String [] {
            CONSTANTs.DISPLAY_NAME_TXTBOX,
            CONSTANTs.MYACCT_EMAIL_TXTBOX,
            CONSTANTs.FIRST_NAME,
            CONSTANTs.LAST_NAME,
            CONSTANTs.ADDRESS_1
    };

    for (String elementName : elementNames) {
        if(driver.findElement(By.cssSelector(elementName)).isDisplayed()) {
            elementPresent_Click(By.cssSelector(elementName), displayName);
            Thread.sleep(2000);
            break; // when found break out of loop
        }
    }

    log.step("Click Save Changes Button......");
    txtBoxDisabled(By.cssSelector(CONSTANTs.SAVE_CHANGES_BTN),null);            
}

答案 1 :(得分:1)

你有一个误解(对于一个新的程序员来说是完全可以理解的)。

您似乎认为使用if / else切换会使您的代码更具可读性,事实上它很可能不会。那么如何让你的代码更具可读性呢?简单,摆脱你所有的重复。

例如,您在每个if / else中调用Thread.sleep。而是将其拉出并放在if / else语句之后。

您可以做的另一件事是您可以将查找元素移动到类顶部的变量中。然后只需调用它们上的isDisplayed

即可

示例:

displayNameTextbox = driver.findElement(By.cssSelector(CONSTANTs.DISPLAY_NAME_TXTBOX));
if(displayNameTextbox.isDisplayed())
{
    log.step("Optional..... BUT you will need it when Commenting on any article....");
    elementPresent_Click(By.cssSelector(CONSTANTs.DISPLAY_NAME_TXTBOX), displayName);

}

由于这似乎是在Selenium中,我实际上建议创建一个页面对象并使用它来简化代码。

另一件需要注意的事情是你可以将每个ifs分成他们自己的方法,并在必要时检查它们,反对一次检查它们。但那更多地取决于你打算如何使用它。

答案 2 :(得分:0)

你已经得到了答案,但我仍然试一试。我将使用enum删除if/else,代码将更加面向对象。顺便说一下 - 除了if / else之外还有许多其他领域可以改进代码,例如命名惯例。

  

MyTest课程

public class MyTest {

    public void profileSection(String displayName, String fName, String lName,
            String Address_1, String Address_2, String city, String country,
            String postcode, String telephone) throws Exception {
        myAccountPageNative(CONSTANTs.MY_ACCOUNT_LINK);
        clickLink(CONSTANTs.MY_PROFILE_SECTION);

        Driver driver = null;
        MyEnum.test(driver, displayName, fName, lName, Address_1, Address_2, city, country, postcode, telephone);
        MyEnum.SAVE_CHANGES_BTN.doOperation(displayName, fName, lName, Address_1, Address_2, city, country, postcode, telephone);

    }

    private void clickLink(String myProfileSection) {
        // TODO Auto-generated method stub
    }

    private void myAccountPageNative(String myAccountLink) {
        // TODO Auto-generated method stub
    }
}
  

MyEnum类

public enum MyEnum {

    DISPLAY_NAME_TXTBOX {
        @Override
        public void doOperation(String displayName, String fName, String lName,
                String Address_1, String Address_2, String city, String country,
                String postcode, String telephone) {
            elementPresent_Click(By.cssSelector(name()),
                    displayName);
        }

        private void elementPresent_Click(Selector cssSelector,
                String displayName) {

        }
    },
    MYACCT_EMAIL_TXTBOX {
        @Override
        public void doOperation(String displayName, String fName, String lName,
                String Address_1, String Address_2, String city, String country,
                String postcode, String telephone) {
            txtBoxDisabled(By.cssSelector(name()), null);
        }

        private void txtBoxDisabled(Selector cssSelector, Object object) {

        }
    },
    FIRST_NAME {
        @Override
        public void doOperation(String displayName, String fName, String lName,
                String Address_1, String Address_2, String city, String country,
                String postcode, String telephone) {
            txtBoxDisabled(By.cssSelector(name()), fName);
        }

        private void txtBoxDisabled(Selector cssSelector, Object object) {

        }
    },
    LAST_NAME {
        @Override
        public void doOperation(String displayName, String fName, String lName,
                String Address_1, String Address_2, String city, String country,
                String postcode, String telephone) {
            txtBoxDisabled(By.cssSelector(name()), lName);
        }

        private void txtBoxDisabled(Selector cssSelector, Object object) {
        }
    },
    ADDRESS_1 {
        @Override
        public void doOperation(String displayName, String fName, String lName,
                String Address_1, String Address_2, String city, String country,
                String postcode, String telephone) {
            txtBoxDisabled(By.cssSelector(name()), Address_1);
        }

        private void txtBoxDisabled(Selector cssSelector, Object object) {
        }
    },
    SAVE_CHANGES_BTN {
        @Override
        public void doOperation(String displayName, String fName, String lName,
                String Address_1, String Address_2, String city, String country,
                String postcode, String telephone) {
        }
    };


    public static void test(Driver driver, String displayName, String fName, String lName,
            String Address_1, String Address_2, String city, String country,
            String postcode, String telephone) {
        for(MyEnum enum1 : MyEnum.values()) {
            if(driver.findElement(By.cssSelector(enum1.name())).isDisplayed()) {
                enum1.doOperation(displayName, fName, lName, Address_1, Address_2, city, country, postcode, telephone);
            }
        }
    }

    abstract public void doOperation( String displayName, String fName, String lName,
            String Address_1, String Address_2, String city, String country,
            String postcode, String telephone);
}