Skip to content

Commit

Permalink
Merge pull request #78 from Sprokof/add-ui-test-for-registerPage
Browse files Browse the repository at this point in the history
Write ui test for register page
  • Loading branch information
devondragon authored Apr 29, 2024
2 parents 4587e0c + 6081f59 commit af91b03
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules/user.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.2.RELEASE'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.3.0'


// Other dependencies
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
// runtimeOnly 'io.micrometer:micrometer-registry-new-relic'
Expand All @@ -61,6 +62,8 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'com.h2database:h2:2.2.224'
testImplementation group: 'com.codeborne', name: 'selenide', version: '7.0.0'
testImplementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '5.8.0'
}

test {
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/ui/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ui;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

public abstract class BaseTest {

public void setUp() {
WebDriverManager.chromedriver().setup();
Configuration.browser = "chrome";
Configuration.browserSize = "2560x1440";
Configuration.webdriverLogsEnabled = true;
Configuration.headless = false;
}

@BeforeEach
public void init() {
setUp();
}

@AfterEach
public void tearDown() {
Selenide.closeWebDriver();
}

}
29 changes: 29 additions & 0 deletions src/test/java/ui/SpringUserFrameworkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ui;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import ui.jdbc.Jdbc;
import ui.page.RegisterPage;
import ui.page.SuccessRegisterPage;

import static ui.data.UserTestData.*;

public class SpringUserFrameworkTest extends BaseTest {
private static final String SUCCESS_MESSAGE = "Thank you for registering!";
private static final String URI = "http://localhost:8080/";

@AfterAll
public static void deleteTestUser() {
Jdbc.deleteTestUser();
}

@Test
public void successSignUp() {
RegisterPage registerPage = new RegisterPage(URI + "user/register.html");
registerPage.signUp(FIRST_NAME, LAST_NAME, EMAIL, PASSWORD, CONFIRM_PASSWORD);
SuccessRegisterPage successRegisterPage = new SuccessRegisterPage();
String actual = successRegisterPage.message();
Assertions.assertEquals(actual, SUCCESS_MESSAGE);
}
}
12 changes: 12 additions & 0 deletions src/test/java/ui/data/UserTestData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ui.data;

public class UserTestData {
public static final String FIRST_NAME = "testUser";
public static final String LAST_NAME = "userTest";
public static final String EMAIL = "[email protected]";
public static final String PASSWORD = "testUserPassword";
public static final String CONFIRM_PASSWORD = "testUserPassword";



}
29 changes: 29 additions & 0 deletions src/test/java/ui/jdbc/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ui.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {

static {
initDriver();
}

private static void initDriver() {
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

public static Connection open() {
try {
return DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3306/springuser", "springuser", "springuser");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

}
33 changes: 33 additions & 0 deletions src/test/java/ui/jdbc/Jdbc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ui.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static ui.data.UserTestData.EMAIL;
import static ui.data.UserTestData.FIRST_NAME;

/**
* Using for test user data from db when it storing by UI test
*/
public class Jdbc {
private static final String DELETE_VERIFICATION_TOKEN_QUERY = "DELETE FROM verification_token WHERE user_id = (SELECT id FROM user_account WHERE first_name = ? AND email = ?)";
private static final String DELETE_TEST_USER_ROLE = "DELETE FROM users_roles WHERE user_id = (SELECT id FROM user_account WHERE first_name = ? AND email = ?)";
private static final String DELETE_TEST_USER_QUERY = "DELETE FROM user_account WHERE first_name = ? AND email = ?";
public static void deleteTestUser() {
try(Connection connection = ConnectionManager.open()) {
execute(connection, DELETE_VERIFICATION_TOKEN_QUERY);
execute(connection, DELETE_TEST_USER_ROLE);
execute(connection, DELETE_TEST_USER_QUERY);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private static void execute(Connection connection, String query) throws SQLException {
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, FIRST_NAME);
statement.setString(2, EMAIL);
statement.executeUpdate();
}
}
32 changes: 32 additions & 0 deletions src/test/java/ui/page/RegisterPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ui.page;

import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;

/**
* Register page
*/
public class RegisterPage {
private final SelenideElement FIRST_NAME_FIELD = Selenide.$x("//input[@id='firstName']");
private final SelenideElement LAST_NAME_FIELD = Selenide.$x("//input[@id='lastName']");
private final SelenideElement EMAIL_FIELD = Selenide.$x("//input[@id='email']");
private final SelenideElement PASSWORD_FIELD = Selenide.$x("//input[@id='password']");
private final SelenideElement CONFIRM_PASSWORD_FIELD = Selenide.$x("//input[@id='matchPassword']");
private final SelenideElement SIGN_UP_BUTTON = Selenide.$x("//button[@id='signUpButton']");

public RegisterPage(String url) {
Selenide.open(url);
}
/**
* Filling register form and click signUp button
*/
public void signUp(String firstName, String lastName, String email, String password, String confirmPassword) {
FIRST_NAME_FIELD.setValue(firstName);
LAST_NAME_FIELD.setValue(lastName);
EMAIL_FIELD.setValue(email);
PASSWORD_FIELD.setValue(password);
CONFIRM_PASSWORD_FIELD.setValue(confirmPassword);
SIGN_UP_BUTTON.click();
}

}
13 changes: 13 additions & 0 deletions src/test/java/ui/page/SuccessRegisterPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ui.page;

import com.codeborne.selenide.SelenideElement;

import static com.codeborne.selenide.Selenide.$x;

public class SuccessRegisterPage {
private final SelenideElement SUCCESS_MESSAGE = $x("//section//div//h1");

public String message() {
return SUCCESS_MESSAGE.text();
}
}

0 comments on commit af91b03

Please sign in to comment.