Adjusting GUI and API Test Cases with ChatGPT
This article explains how to handle leading and trailing spaces in user input by applying the .trim() method in both front‑end JavaScript validation functions and back‑end Java controllers, updates test data and cases, and verifies that all GUI and API tests pass successfully.
Problem: Leading/Trailing Spaces in User Input
When users accidentally add spaces before or after data in forms, validation may fail. The article shows how to strip spaces using .trim() in both front‑end JavaScript validation functions and back‑end Java controllers.
Front‑end Fix
In index.js the functions validateRegisterForm, validateLoginForm, validateVeriCodeForm and validateRecoverForm are updated to call .trim() on each retrieved value before passing to the check functions.
function validateRegisterForm(){
const username = document.getElementById('username') ? document.getElementById('username').value.trim() : '';
const password = document.getElementById('password') ? document.getElementById('password').value.trim() : '';
const confirmPassword = document.getElementById('confirmPassword') ? document.getElementById('confirmPassword').value.trim() : '';
const phone = document.getElementById('phone') ? document.getElementById('phone').value.trim() : '';
const email = document.getElementById('email') ? document.getElementById('email').value.trim() : '';
return checkRegister(username, password, confirmPassword, phone, email);
}
function validateLoginForm(){
const username = document.getElementById('username') ? document.getElementById('username').value.trim() : '';
const password = document.getElementById('password') ? document.getElementById('password').value.trim() : '';
return checkLogin(username, password);
}
function validateVeriCodeForm(){
const contact = document.getElementById('contact') ? document.getElementById('contact').value.trim() : '';
return checkVeriCode(contact);
}
function validateRecoverForm(){
const identifyingCode = document.getElementById('identifyingCode') ? document.getElementById('identifyingCode').value.trim() : '';
const newPassword = document.getElementById('newPassword') ? document.getElementById('newPassword').value.trim() : '';
const confirmPassword = document.getElementById('confirmPassword') ? document.getElementById('confirmPassword').value.trim() : '';
return checkRecover(identifyingCode, newPassword, confirmPassword);
}Back‑end Fix
In Java controllers the request parameters are retrieved and .trim() is applied, with a null‑check to avoid NullPointerException.
// Example in RegisterController.java
private User getUser(){
String username = this.request.getParameter("username");
String password = this.request.getParameter("password");
String phone = this.request.getParameter("phone");
String email = this.request.getParameter("email");
// Trim where appropriate
username = username != null ? username.trim() : null;
phone = phone != null ? phone.trim() : null;
email = email != null ? email.trim() : null;
// Password is hashed with SHA256, so do not trim it
User user = new User(username, password, phone, email);
return user;
}
// Example in LoginController.java
String username = this.request.getParameter("username");
username = username != null ? username.trim() : null;
String password = this.request.getParameter("password");
password = password != null ? password.trim() : null;Test Data Constants
The globals.py file defines constants that contain valid values surrounded by spaces, used to verify that trimming works correctly.
class Config:
# URL definitions omitted for brevity
VALID_USERNAME = "validUser123"
VALID_PASSWORD = "Valid123!"
VALID_PHONE = "13812345677"
VALID_EMAIL = "[email protected]"
# Space‑surrounded constants
SPACE_USERNAME = " " + VALID_USERNAME + " "
SPACE_PASSWORD = " " + VALID_PASSWORD + " "
SPACE_PHONE = " " + VALID_PHONE + " "
SPACE_EMAIL = " " + VALID_EMAIL + " "Updated Test Cases
Test scripts such as Test_Register_GUI.py, Test_Login_GUI.py and Test_Recover_GUI.py are modified to include the space‑containing inputs and the expected outcomes. Running the tests with pytest shows all cases passing.
... pytest output ...
===================== 16 passed in 8.94s =====================
... pytest output ...
===================== 7 passed in 4.51s =====================
... pytest output ...
===================== 9 passed in 24.65s =====================Summary
The adjustments ensure that both GUI and API layers correctly handle inputs with surrounding whitespace, preventing validation failures while preserving password hashing logic. All updated test cases confirm the effectiveness of the changes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Woodpecker Software Testing
The Woodpecker Software Testing public account shares software testing knowledge, connects testing enthusiasts, founded by Gu Xiang, website: www.3testing.com. Author of five books, including "Mastering JMeter Through Case Studies".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
