Stabilize reservation test by preparing data for visible user

This commit is contained in:
2026-04-15 23:23:02 +03:00
parent 0cd4e29d02
commit aaf63cc438
3 changed files with 65 additions and 12 deletions
@@ -33,4 +33,15 @@ public final class DbClient {
throw new IllegalStateException("Failed to prepare test data for account " + account.name(), e); throw new IllegalStateException("Failed to prepare test data for account " + account.name(), e);
} }
} }
public void prepareReservationData(String ownerUsername, String reservationUsername) {
try (Connection connection = DriverManager.getConnection(config.url(), config.user(), config.password());
PreparedStatement statement = connection.prepareStatement(TestAccount.RESERVATION.resetSql())) {
statement.setString(1, ownerUsername);
statement.setString(2, reservationUsername);
statement.execute();
} catch (SQLException e) {
throw new IllegalStateException("Failed to prepare reservation data for owner " + ownerUsername, e);
}
}
} }
@@ -1,10 +1,14 @@
package ru.otus.mobile.page; package ru.otus.mobile.page;
import com.codeborne.selenide.Condition; import com.codeborne.selenide.Condition;
import com.codeborne.selenide.CollectionCondition;
import com.codeborne.selenide.SelenideElement;
import com.google.inject.Inject; import com.google.inject.Inject;
import ru.otus.mobile.component.BottomNavigationComponent; import ru.otus.mobile.component.BottomNavigationComponent;
import ru.otus.mobile.config.MobileConfig; import ru.otus.mobile.config.MobileConfig;
import java.time.Duration;
public final class UsersPage extends AbsBasePage { public final class UsersPage extends AbsBasePage {
@Inject @Inject
public UsersPage(MobileConfig config, BottomNavigationComponent bottomNavigation) { public UsersPage(MobileConfig config, BottomNavigationComponent bottomNavigation) {
@@ -18,19 +22,51 @@ public final class UsersPage extends AbsBasePage {
public void openUser(String username) { public void openUser(String username) {
String appPackage = config().appPackage(); String appPackage = config().appPackage();
String selector = "new UiScrollable(new UiSelector().resourceId(\"" + appPackage + ":id/users\"))" String xpath = "//android.widget.TextView[@resource-id='" + appPackage + ":id/username'"
+ ".setMaxSearchSwipes(12)" + " and @text=" + xpathLiteral(username) + "]";
+ ".scrollIntoView(new UiSelector().resourceId(\"" + appPackage + ":id/username\")" SelenideElement user = byXpath(xpath);
+ ".textContains(\"" + username.replace("\"", "\\\"") + "\"))"; if (user.exists()) {
byUiAutomator(selector).shouldBe(Condition.visible).click(); user.shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
} else {
scrollToText(username).shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
}
byTextContains(username).shouldBe(Condition.visible, Duration.ofSeconds(15));
}
public String firstVisibleUsernameExcluding(String excludedUsername) {
var usernames = allById("username")
.shouldHave(CollectionCondition.sizeGreaterThan(0), Duration.ofSeconds(15));
for (SelenideElement usernameElement : usernames) {
String username = usernameElement.getText().trim();
if (!username.isEmpty() && !username.equals(excludedUsername)) {
return username;
}
}
throw new IllegalStateException("No visible username found excluding '" + excludedUsername + "'.");
} }
public void openFirstWishlist() { public void openFirstWishlist() {
allById("wishlist_item").first().shouldBe(Condition.visible).click(); allById("wishlist_item")
.shouldHave(CollectionCondition.sizeGreaterThan(0), Duration.ofSeconds(15))
.first()
.shouldBe(Condition.visible, Duration.ofSeconds(15))
.click();
}
public void openWishlist(String name) {
scrollToText(name).shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
} }
public void openFirstGift() { public void openFirstGift() {
allById("gift_item").first().shouldBe(Condition.visible).click(); allById("gift_item")
.shouldHave(CollectionCondition.sizeGreaterThan(0), Duration.ofSeconds(15))
.first()
.shouldBe(Condition.visible, Duration.ofSeconds(15))
.click();
}
public void openGift(String name) {
scrollToText(name).shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
} }
public boolean isReserved() { public boolean isReserved() {
@@ -5,8 +5,8 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import ru.otus.mobile.annotations.MobileUser; import ru.otus.mobile.annotations.MobileUser;
import ru.otus.mobile.config.MobileConfig;
import ru.otus.mobile.config.TestAccount; import ru.otus.mobile.config.TestAccount;
import ru.otus.mobile.db.DbClient;
import ru.otus.mobile.extensions.MobileExtension; import ru.otus.mobile.extensions.MobileExtension;
import ru.otus.mobile.page.UsersPage; import ru.otus.mobile.page.UsersPage;
@@ -15,19 +15,25 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
@ExtendWith(MobileExtension.class) @ExtendWith(MobileExtension.class)
@MobileUser(TestAccount.RESERVATION) @MobileUser(TestAccount.RESERVATION)
public class ReservationTest { public class ReservationTest {
private static final String OWNER_WISHLIST_NAME = "Owner Wishlist";
private static final String OWNER_GIFT_NAME = "Owner Gift";
@Inject @Inject
private UsersPage usersPage; private UsersPage usersPage;
@Inject @Inject
private MobileConfig mobileConfig; private DbClient dbClient;
@Test @Test
@DisplayName("Изменение статуса резервирования подарка другого пользователя") @DisplayName("Изменение статуса резервирования подарка другого пользователя")
void changeReservationStatus() { void changeReservationStatus() {
usersPage.open(); usersPage.open();
usersPage.openUser(mobileConfig.reservationOwnerUsername()); String reservationOwner = usersPage.firstVisibleUsernameExcluding(TestAccount.RESERVATION.username());
usersPage.openFirstWishlist(); dbClient.prepareReservationData(reservationOwner, TestAccount.RESERVATION.username());
usersPage.openFirstGift(); usersPage.open();
usersPage.openUser(reservationOwner);
usersPage.openWishlist(OWNER_WISHLIST_NAME);
usersPage.openGift(OWNER_GIFT_NAME);
boolean before = usersPage.isReserved(); boolean before = usersPage.isReserved();
usersPage.toggleReservation(); usersPage.toggleReservation();
assertNotEquals(before, usersPage.isReserved(), "Reservation status was not changed"); assertNotEquals(before, usersPage.isReserved(), "Reservation status was not changed");