Java列挙型を使用したテストの自動化

Java QA Automation Engineerコースの将来の学生に、「HTTP。Postman、newman、fiddler(charles)、curl、SOAP.SOAPUI」というトピックに関する公開レッスンに参加してもらいます。







そして今、私たちはあなたが有用な資料の翻訳に精通していることを提案します。










テストデータを保存するには、通常、次のようなデータタイプが必要です。





  • 複数のプロパティを宣言できます。





  • 動作が最小限であるか、まったくありません。





  • 複数の同一のエンティティを簡単に作成できます。





オブジェクトはこれらの要件をほぼ満たしています。ただし、複数のエンティティを作成するには、プロパティの数が少なく、動作が最小限である(またはまったくない)オブジェクトをいくつか作成する必要があります。最小限の動作とは、少数のメソッドを意味します。基本的に、必要なエンティティごとに、新しいオブジェクトを作成する必要がありますが、これはリソースの浪費です。代わりに、Enum



特別なタイプのオブジェクトを使用できます





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