0

I'm new to the world of coding and this is my first post on stackoverflow. So please bear with me. So here's the problem.

I've got a list of variables for some text values in one language (denoted as EnglishList in the sample code below). and then another list using the same variable names but the values are different e.g. text translation in another language. (denoted as FrenchList in the sample code below).

I am writing an automated test in Java to set the value of some fields in my website.The values will need to be obtained from EnglishList first. Once the values are set, I want to set them to values obtained from FrenchList.

So basically, I have 2 questions:

  1. How do I define these EnglishList and FrenchList. I have declared them as a class in my sample code below but I am sure this is not the right way.
  2. How do I ensure the variables are read from the EnglishList and then from the FrenchList

Please note the is a very simplified version of what I'm trying to achieve. The number of languages, the number of variables in the lists and the number of tests/methods that need to be run in each language will be a lot more and hence the need to ensure I can modularise as much as I can.

import com.codeborne.selenide.SelenideElement;

import static com.codeborne.selenide.Selenide.$;

class EnglishList{
    String text1= "Name";
    String text2 = "JobTitle";
}

class FrenchList{
    String text1= "FrenchName";
    String text2= "FrenchJobTitle";
}

public class Test{

public void main() {

    //I want to read values from EnglishList
    setAndAssert(LangOption.ENGLISH);
    //Now I want to read values from FrenchList
    setAndAssert(LangOption.FRENCH);
    }

    private void setAndAssert(LangOption langOption){
        //How to ensure the values of text1 and text 2 are obtained from correct language option...??
        switch (langOption){
            case ENGLISH: 
                // use EnglishList - How ? ; 
                break;
            case FRENCH: 
                //use FrenchList - How ? ;
                break;
        }
        //set and assert details based on the list chosen above
        setName(text1);
        setJob(text2);
        //more code here to assert....
    }

    private void setName(String text){
        SelenideElement field= $("#someId1");
        field.setValue(text);
    }

    private void setJob(String text){
        SelenideElement field= $("#someId2");
        field.setValue(text);
        }
        
    enum LangOption{
        ENGLISH,
        FRENCH
    }
}
5
  • 1
    stackoverflow.com/questions/10084365/… Commented Mar 3, 2022 at 20:39
  • 1
    javatpoint.com/internationalization-in-java Commented Mar 3, 2022 at 20:40
  • When you say "list," Java programmers will tend to think you mean a class that extends java.util.List, but what you really want is more like a Map. A Properties object is a lot like a map, and you can read one from a properties file. I suggest to take some time to study the information at the links ControlAltDel has kindly provided. Commented Mar 4, 2022 at 0:27
  • Thanks. Using the ResourceBundle seems to be a good solution. Have tried to implement it but when attempting to run the code, I'm getting a java.util.MissingResourceException: Can't find bundle for base name Resources, locale en_GB. I have got the Resources files with the correct names i.e. Resources_en_GB.properties and they are in the same package as my main java class. So wonder why they can't be found. Commented Mar 4, 2022 at 11:40
  • This has now worked. I just updated the basename to ResourceFiles and my properties files to ResourceFiles_en_GB and for some reason it has worked. Really appreciate the help provided ! Commented Mar 4, 2022 at 11:53

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.