Test Automation with Java Enumerated Types

We invite future students of the course "Java QA Automation Engineer" to take part in the open lesson on the topic "HTTP. Postman, newman, fiddler (charles), curl, SOAP. SOAPUI".







And now we suggest that you familiarize yourself with the translation of useful material.










Storing test data usually requires a data type that:





  • allows multiple properties to be declared;





  • has minimal or no behavior;





  • allows you to easily create multiple identical entities.





The objects almost meet these requirements. But then, to create several entities, it would be necessary to create several objects with a small number of properties and minimal behavior (or none at all). By minimal behavior, I mean a small number of methods. Basically, for every entity you need, you would have to create a new object, which is a waste of resources. Instead, you can use Enum



an object of a special type.





Enum



, , , . Enum



, . GitHub, . Enum



.





enum- Java :

, . : , ,  — , . .





  , . , : , . , . Enum



. (), : AT, EE ES.





Enum



:





public enum Country {
    AT("Austria", Arrays.asList("Vienna", "Salzburg", "Innsbruck"), 43),
    EE("Estonia", Arrays.asList("Tallinn", "Haapsalu", "Tartu"), 372),
    ES("Spain", Arrays.asList("Malaga","Madrid","Valencia","Corralejo"), 34);

    public final String label;
    public final List<String> cities;
    public int phoneNumberPrefix;

    Country(String label, List<String> cities, int phoneNumberPrefix) {
        this.label = label;
        this.cities = cities;
        this.phoneNumberPrefix = phoneNumberPrefix;
    }
}
      
      



, , . , label



, cities



phoneNumberPrefix



. : String



, List<String>



int



.





Enum



. , , AT



, : label



«», cities



(), : «», «» «», phoneNumberPrefix



«43».





, Enum



, : Country..



. : Country.AT.label



  «». , Country



.





, , .





Page



:





@FindBy(css = "#country") private WebElement countryDropdown;
@FindBy(css = "#city") private WebElement cityDropdown;
@FindBy(css = "#phone") public WebElement phoneNumberField;
@FindBy(css = "[type='submit']") public WebElement submitButton;

public Select countrySelect() {
    return new Select(countryDropdown);
}

public Select citySelect() {
    return new Select(cityDropdown);
}
      
      



countrySelect()



Select



. citySelect()



Select



. WebElement phoneNumberField



.





, , - . GitHub, .





 1.

, . , , 10 . :





, :





@Test
void selectCountryCityAndTypePhoneNumber() {
}
      
      



. , label



ES Enum



. : Country.ES.label



. :





page.countrySelect().selectByVisibleText(Country.ES.label);
      
      



, . : Country.ES.cities



. , ( ), : Country.ES.cities.get(2)



. :





page.citySelect().selectByVisibleText(Country.ES.cities.get(2));
      
      



, . Enum



: Country.ES.phoneNumberPrefix



. , 10 : Country.ES.phoneNumberPrefix + randomNumeric(8)



.





randomNumeric



, Apache Commons :





import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
      
      



, . Maven pom.xml



( ):





<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>
      
      



:





@Test
void selectCountryCityAndTypePhoneNumber() {
    page.countrySelect().selectByVisibleText(Country.ES.label);
    page.citySelect().selectByVisibleText(Country.ES.cities.get(2));
    page.phoneNumberField.sendKeys(Country.ES.phoneNumberPrefix + randomNumeric(8));
}
      
      



 2.

, . : ( ), , . , - .





.  , Enum



label



. , . , .





. , . , .





, Enum



. , , .





 





2, . (expected) . , label



Enum



, , . , -  Selenium, , (String), . -, , . .





List<String> expectedCountries = new ArrayList<>();
expectedCountries.add("");
      
      



label



, Enum



. Enum



label



. Enum



, Country.values()



.





for (Country country : Country.values()) {
    expectedCountries.add(country.label);
}
      
      



: «», «», «», «».





- (actual) . Select



, WebElements



, Select



. getText()



(option) .





List<String> actualCountries = new ArrayList<>();
for (WebElement option : page.countrySelect().getOptions()) {
    actualCountries.add(option.getText());
}
      
      



, , , Enum . , .





Collections.sort(expectedCountries);
Collections.sort(actualCountries);
assertEquals(expectedCountries, actualCountries);
      
      



:





@Test
void checkCountries() {
    List<String> expectedCountries = new ArrayList<>();
    expectedCountries.add("");
    for (Country country : Country.values()) {
        expectedCountries.add(country.label);
    }
    List<String> actualCountries = new ArrayList<>();
    for (WebElement option : page.countrySelect().getOptions()) {
        actualCountries.add(option.getText());
    }
    Collections.sort(expectedCountries);
    Collections.sort(actualCountries);
    assertEquals(expectedCountries, actualCountries);
}
      
      



 3.

. , . JavaScript, .





Enum



:





for (Country country : Country.values()) {
      
      



, for



, label



Enum



:





page.countrySelect().selectByVisibleText(country.label);
      
      



, , , . , , (actual) . -:





List<String> actualCities = new ArrayList<>();
for (WebElement option : page.citySelect().getOptions()) {
    actualCities.add(option.getText());
}
      
      



. Enum List<String> cities



. , . addAll()



cities



.





List<String> expectedCities = new ArrayList<>();
expectedCities.add(0, "");
expectedCities.addAll(country.cities);
      
      



. , .





Collections.sort(expectedCities);
Collections.sort(actualCities);
assertEquals(expectedCities, actualCities);
      
      



, , , . . , . :





@Test
void checkCities() {
    for (Country country : Country.values()) {
        page.countrySelect().selectByVisibleText(country.label);
        List<String> actualCities = new ArrayList<>();
        for (WebElement option : page.citySelect().getOptions()) {
            actualCities.add(option.getText());
        }
        List<String> expectedCities = new ArrayList<>();
        expectedCities.add(0, "");
        expectedCities.addAll(country.cities);
        Collections.sort(expectedCities);
        Collections.sort(actualCities);
        assertEquals(expectedCities, actualCities);
    }
}
      
      



, Enum



. GitHub:





  • HTML-  .





  • Page  .





  • Enum  ,





  •  .










"Java QA Automation Engineer".



"HTTP. Postman, newman, fiddler (charles), curl, SOAP. SOAPUI".












All Articles