Automate Adding High School Records with Selenium and Java
This tutorial shows how to use Java and Selenium WebDriver to programmatically add a high‑school record with random scores and rankings, streamlining pagination testing by generating the required data automatically instead of entering each entry manually.
When learning Selenium with Java, testing pagination that displays 20 rows per page can become cumbersome if each record must be entered manually; to solve this, the author created a reusable method that automatically adds a complete high‑school record with randomly generated values.
The core method addRecord(WebDriver driver, int num) performs the following steps:
public static void addRecord(WebDriver driver, int num) throws InterruptedException {
findElementByIdAndClick(driver, "btn-user"); // Open personal center
findElementByIdAndClick(driver, "btn-uc-record"); // Open high‑school records
for (int n = 0; n < num; n++) {
findElementByIdAndClick(driver, "btnAddRecord"); // Click Add Record
findElementByIdAndClick(driver, "button-toggle-semester_id"); // Choose semester
findElementByXpathAndClick(driver, ".//*[@id='dropdown-semester_id']/li[" + getRandomInt(5) + "]/a");
findElementByIdAndClick(driver, "button-toggle-exam_id"); // Choose exam type
findElementByXpathAndClick(driver, ".//*[@id='dropdown-exam_id']/li[" + getRandomInt(7) + "]/a");
findElementByIdAndClick(driver, "button-toggle-year"); // Choose year
findElementByXpathAndClick(driver, ".//*[@id='dropdown-year']/li[" + getRandomInt(5) + "]/a");
// Fill scores for each subject (e.g., Chinese, Math, English, etc.)
for (int i = 1; i < 7; i++) {
findElementByIdAndClearSendkeys(driver, "input-score" + i, getRandomInt(100));
findElementByIdAndClearSendkeys(driver, "input-total_score" + i, 100);
}
findElementByIdAndClearSendkeys(driver, "input-additional_score", getRandomInt(10)); // Policy bonus
findElementByIdAndClearSendkeys(driver, "input-ranking_province", getRandomInt(10000)); // Province rank
findElementByIdAndClearSendkeys(driver, "input-ranking_city", getRandomInt(1000)); // City rank
findElementByIdAndClearSendkeys(driver, "input-ranking_district", getRandomInt(500)); // District rank
findElementByIdAndClearSendkeys(driver, "input-ranking_school", getRandomInt(200)); // School rank
findElementByIdAndClearSendkeys(driver, "input-ranking_class", getRandomInt(50)); // Class rank
findElementByIdAndClick(driver, "btnSave"); // Save record
sleep(1);
findElementByXpathAndClick(driver, "html/body/div[3]/div[7]/div/button"); // Confirm save
sleep(1);
findElementByXpathAndClick(driver, "html/body/div[3]/div[7]/div/button"); // Dismiss success dialog
}
}The helper method used to generate random integers is defined as:
public static int getRandomInt(int num) {
return new Random().nextInt(num) + 1;
}By invoking addRecord(driver, N), testers can quickly create N realistic high‑school records, each populated with random scores and ranking data, thereby facilitating efficient pagination and data‑driven test scenarios.
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.
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.
