简体   繁体   中英

how to parametrise the variable in the abstract class

I have the below abstract class where character_1, character_2, character_3 are decleared.

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']";
}

How can I parameterise that and passing through to properties.

String[] myStringArray = {"character_1","character_2","character_3"};
    for (int i = 0; i < myStringArray.length; i++)
    {
      String value = myStringArray[i];
        String altvalue =   driver.findElement(By.xpath(Properties.value)).getAttribute("alt");
        System.out.print(altvalue);
    }

You could use an enum. Something along the lines of

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

}

Example

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

I'd say the easiest is to use a 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);
}

You can easily use above code to solve your problem.

However, your quote indicates that you already have something in mind:

I need how to send the value (ie myStringArray[i] ) to Properties. ???????? so that when i = 0, String altvalue = driver.findElement(By.xpath(Properties.character_1)).getAttr‌​ibute("alt"); when i=1 , String altvalue = driver.findElement(By.xpath(Properties.character_2)).getAttr‌​ibute("alt"); like that.

While it is possible with reflection, it is heavily discouraged: you lose compile-time type safety, it causes bugs when refactoring, and performance is slower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM