如何在抽象类中对参数进行参数化

时间:2017-06-01 10:12:29

标签: java selenium selenium-webdriver

我有以下抽象类,其中character_1,character_2,character_3被删除。

  <script>
        function onSubmitFooter(token) {
            document.getElementById("subscribe-form-footer").submit();
        }
    </script>

如何对其进行参数化并传递给属性。

public abstract class Properties {
 public static final String character_1 = "//*[@class='character- nav-slide-normal'][@alt='CHARLIE E LOLA']";
 public static final String character_2 = "//*[@class='character-nav-slide-normal'][@alt='PEPPA']";
 public static final String character_3 = "//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']";
}

2 个答案:

答案 0 :(得分:0)

你可以使用枚举。

的内容
public enum Properties {

CHARACTER_1("//*[@class='character- nav-slide-normal'][@alt='CHARLIE E LOLA']"),
CHARACTER_2("//*[@class='character-nav-slide-normal'][@alt='PEPPA']"),
CHARACTER_3("//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']"),
CHARACTER_4("//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']"),
CHARACTER_5("//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']"),
CHARACTER_6("//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']"),
CHARACTER_7("//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']");

private String xpath;

Properties(String xpath) {
    this.xpath = xpath;
}

public String getXpath() {
    return xpath;
}

}

示例

// Selective properties 
    Properties[] selectiveProperties = {Properties.CHARACTER_1, Properties.CHARACTER_3, Properties.CHARACTER_6, Properties.CHARACTER_7};

    for (Properties property : selectiveProperties) {
        String altvalue = driver.findElement(By.xpath(property.getXpath())).getAttribute("alt");
        System.out.print(altvalue);
    }


    //Iterate through all the properties 
    for (Properties property : Properties.values()) {
        String altvalue = driver.findElement(By.xpath(property.getXpath())).getAttribute("alt");
        System.out.print(altvalue);
    }

答案 1 :(得分:0)

我说最简单的是使用HashMap<String, String>

public class Properties { //no particular use to make this class abstract
    public static final Map<String, String> characters;
    static
    {
        characters = new HashMap<String, String>();
        characters.put("character_1", "//*[@class='character- nav-slide-normal'][@alt='CHARLIE E LOLA']");
        characters.put("character_2", "//*[@class='character-nav-slide-normal'][@alt='PEPPA']");
        characters.put("character_3", "//*[@class='character-nav-slide-normal'][@alt='FESTA HI-5']");
    }
}

//somewhere else...
String[] myStringArray = {"character_1","character_2","character_3"};
for (String stringArrayString : myStringArray)
{
    String xPath = Properties.characters.get(stringArrayString);
    String altvalue = driver.findElement(By.xpath(xPath)).getAttribute("alt");
    System.out.print(altvalue);
}

您可以轻松使用上述代码来解决您的问题。

但是,你的引用表明你已经有了一些想法:

  

我需要如何将值(即myStringArray [i])发送给Properties。   ????????所以当i = 0时,String altvalue =   driver.findElement(By.xpath(Properties.character_1))GETATTR ibute(&#34; ALT&#34)。   当i = 1时,String altvalue =   driver.findElement(By.xpath(Properties.character_2))GETATTR ibute(&#34; ALT&#34)。   那样的。

虽然有可能通过反射,但是非常气馁:你失去了编译时类型的安全性,它在重构时会导致错误,性能也会变慢。