Sobes.tech
Middle+

Szerinted melyik minta a legfontosabb az automatizált tesztelésben a Page Object minta mellett?

sobes.tech MI

Válasz az MI-től

Builder minta.

Lehetővé teszi összetett tesztadatok vagy objektumok lépésenkénti létrehozását, elválasztva az építési folyamatot a megjelenítésétől.

Előnyök:

  • Rugalmasság: Könnyebbé teszi különböző tesztadat-kombinációk létrehozását.
  • Átláthatóság: Az objektumok létrehozási kódja könnyebben érthetővé válik.
  • Támogatás: Könnyebb módosítani az objektumok létrehozási folyamatát anélkül, hogy az ügyfelet érintené.
  • Újrafelhasználhatóság: Lehetővé teszi tesztadat-készletek létrehozását különböző forgatókönyvekhez.

Példa a tesztfelhasználó létrehozására:

public class User {
    private String username;
    private String password;
    private String email;
    private boolean isActive;

    private User(Builder builder) {
        this.username = builder.username;
        this.password = builder.password;
        this.email = builder.email;
        this.isActive = builder.isActive;
    }

    public static class Builder {
        private String username;
        private String password;
        private String email;
        private boolean isActive = true; // Alapértelmezett érték

        public Builder(String username, String password) {
            this.username = username;
            this.password = password;
        }

        public Builder withEmail(String email) {
            this.email = email;
            return this;
        }

        public Builder withStatus(boolean isActive) {
            this.isActive = isActive;
            return this;
        }

        public User build() {
            return new User(this);
        }
    }

    // Getterek
    public String getUsername() { return username; }
    public String getPassword() { return password; }
    public String getEmail() { return email; }
    public boolean isActive() { return isActive; }
}

// Használat a tesztben
public class UserCreationTest {
    public void testUserCreation() {
        User activeUser = new User.Builder("test_user", "password123")
                .withEmail("test@example.com")
                .build();

        User inactiveUser = new User.Builder("inactive_user", "pass456")
                .withStatus(false)
                .build();
        // Felhasználói objektumok használata a teszt lépéseiben
    }
}

A teszt automatizálásban a Builder gyakran Page Object-tal együtt használják, hogy létrehozzák a tesztadatokat, amelyek szükségesek az oldal elemeivel való interakcióhoz.