You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Selenium页面对象模型,验证过滤后页面元素仅显示ACCEPTED文本

Got it, let's tackle this validation step cleanly using your Page Object Model (POM) setup. Here's a maintainable approach to verify every visible job status is ACCEPTED after applying your filter:

Step 1: Update Your Page Object Class

First, encapsulate the element locator and validation logic directly in your job page class. This keeps all page-specific code in one place, which is the core of POM:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

public class JobListingPage {
    private WebDriver driver;
    // Your existing locator
    private By jobStatusLocator = By.xpath("//span[@ng-bind='::getJobStatus(job.firewallStatus)']");

    public JobListingPage(WebDriver driver) {
        this.driver = driver;
        // Wait for elements to load fully after filter application (critical for reliability)
        new WebDriverWait(driver, Duration.ofSeconds(10))
                .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(jobStatusLocator));
    }

    // Get all visible job status texts after filtering
    private List<String> getFilteredStatusTexts() {
        List<WebElement> statusElements = driver.findElements(jobStatusLocator);
        // Filter out any empty texts to avoid false negatives
        return statusElements.stream()
                .map(WebElement::getText)
                .filter(text -> !text.trim().isEmpty())
                .collect(Collectors.toList());
    }

    // Public method to validate all statuses are ACCEPTED
    public boolean allStatusesAreAccepted() {
        List<String> statuses = getFilteredStatusTexts();
        // Check every status matches "ACCEPTED"
        return statuses.stream().allMatch(status -> status.equals("ACCEPTED"));
    }

    // Don't forget to add your filter application method here (example below)
    public void applyAcceptedFilter() {
        // Implement your logic to select the "ACCEPTED" filter here
        // e.g., driver.findElement(filterDropdown).sendKeys("ACCEPTED");
        // driver.findElement(applyFilterButton).click();
    }
}

Step 2: Use the Page Object in Your Test Case

Now call the page object methods from your test to perform the validation. Keep your test focused on the business rule, not the page details:

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JobFilterTest {
    @Test
    public void verifyFilterShowsOnlyAcceptedStatuses() {
        // Initialize driver (adjust based on your browser setup)
        WebDriver driver = new ChromeDriver();
        driver.get("your-job-listing-page-url");

        // Initialize page object and apply filter
        JobListingPage jobPage = new JobListingPage(driver);
        jobPage.applyAcceptedFilter();

        // Validate the filter worked as expected
        Assert.assertTrue(
            "After applying ACCEPTED filter, some statuses are not ACCEPTED",
            jobPage.allStatusesAreAccepted()
        );

        driver.quit();
    }
}

Key Tips for Reliability

  • Explicit Waits: The WebDriverWait ensures we don't try to validate elements before the filter has finished updating the page—this avoids flaky tests.
  • Empty Text Handling: Trimming and filtering empty texts prevents edge cases where elements might be present but not loaded yet.
  • Clear Assertions: The error message in Assert.assertTrue tells you exactly what went wrong if the test fails, making debugging faster.
  • POM Compliance: All page interactions and locators stay in the page class, so if the UI changes later, you only update one file.

内容的提问来源于stack exchange,提问作者UzIT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:31:33