Stabilize reservation test by preparing data for visible user
This commit is contained in:
@@ -33,4 +33,15 @@ public final class DbClient {
|
||||
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;
|
||||
|
||||
import com.codeborne.selenide.Condition;
|
||||
import com.codeborne.selenide.CollectionCondition;
|
||||
import com.codeborne.selenide.SelenideElement;
|
||||
import com.google.inject.Inject;
|
||||
import ru.otus.mobile.component.BottomNavigationComponent;
|
||||
import ru.otus.mobile.config.MobileConfig;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public final class UsersPage extends AbsBasePage {
|
||||
@Inject
|
||||
public UsersPage(MobileConfig config, BottomNavigationComponent bottomNavigation) {
|
||||
@@ -18,19 +22,51 @@ public final class UsersPage extends AbsBasePage {
|
||||
|
||||
public void openUser(String username) {
|
||||
String appPackage = config().appPackage();
|
||||
String selector = "new UiScrollable(new UiSelector().resourceId(\"" + appPackage + ":id/users\"))"
|
||||
+ ".setMaxSearchSwipes(12)"
|
||||
+ ".scrollIntoView(new UiSelector().resourceId(\"" + appPackage + ":id/username\")"
|
||||
+ ".textContains(\"" + username.replace("\"", "\\\"") + "\"))";
|
||||
byUiAutomator(selector).shouldBe(Condition.visible).click();
|
||||
String xpath = "//android.widget.TextView[@resource-id='" + appPackage + ":id/username'"
|
||||
+ " and @text=" + xpathLiteral(username) + "]";
|
||||
SelenideElement user = byXpath(xpath);
|
||||
if (user.exists()) {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
|
||||
@@ -5,8 +5,8 @@ import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import ru.otus.mobile.annotations.MobileUser;
|
||||
import ru.otus.mobile.config.MobileConfig;
|
||||
import ru.otus.mobile.config.TestAccount;
|
||||
import ru.otus.mobile.db.DbClient;
|
||||
import ru.otus.mobile.extensions.MobileExtension;
|
||||
import ru.otus.mobile.page.UsersPage;
|
||||
|
||||
@@ -15,19 +15,25 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
@ExtendWith(MobileExtension.class)
|
||||
@MobileUser(TestAccount.RESERVATION)
|
||||
public class ReservationTest {
|
||||
private static final String OWNER_WISHLIST_NAME = "Owner Wishlist";
|
||||
private static final String OWNER_GIFT_NAME = "Owner Gift";
|
||||
|
||||
@Inject
|
||||
private UsersPage usersPage;
|
||||
|
||||
@Inject
|
||||
private MobileConfig mobileConfig;
|
||||
private DbClient dbClient;
|
||||
|
||||
@Test
|
||||
@DisplayName("Изменение статуса резервирования подарка другого пользователя")
|
||||
void changeReservationStatus() {
|
||||
usersPage.open();
|
||||
usersPage.openUser(mobileConfig.reservationOwnerUsername());
|
||||
usersPage.openFirstWishlist();
|
||||
usersPage.openFirstGift();
|
||||
String reservationOwner = usersPage.firstVisibleUsernameExcluding(TestAccount.RESERVATION.username());
|
||||
dbClient.prepareReservationData(reservationOwner, TestAccount.RESERVATION.username());
|
||||
usersPage.open();
|
||||
usersPage.openUser(reservationOwner);
|
||||
usersPage.openWishlist(OWNER_WISHLIST_NAME);
|
||||
usersPage.openGift(OWNER_GIFT_NAME);
|
||||
boolean before = usersPage.isReserved();
|
||||
usersPage.toggleReservation();
|
||||
assertNotEquals(before, usersPage.isReserved(), "Reservation status was not changed");
|
||||
|
||||
Reference in New Issue
Block a user