URL访问失败(错误码16)时如何终止测试用例执行?
Hey there! Let's fix this frustrating issue where all your 30 Gmail test cases keep running and failing even when the URL is blocked by security rules. The key here is to add an early check for the error page right after accessing the URL, and terminate all tests immediately if the error matches your specific scenario.
Step 1: Add Error Page Validation Logic
First, right after navigating to the Gmail URL, you need to verify if the page shows the blocked error. You can either check the page source for the exact error details, or look for a unique element that appears only on the error page (the latter is more reliable).
Here's how to modify your code with page source checks (matching the exact error info you provided):
String url = getUrl(prop.getProperty("url")); System.out.println(url); driver.get(url); // Make sure you're navigating to the URL first (I noticed this was missing in your snippet) // Check if the error page is displayed String pageSource = driver.getPageSource(); // Define the key error identifiers from your scenario boolean isBlocked = pageSource.contains("错误码16") && pageSource.contains("此请求被安全规则阻止") && pageSource.contains("您的IP117.98.192.121") && pageSource.contains("代理IP45.60.125.185(ID 10747)"); if (isBlocked) { // Terminate all test execution immediately // For TestNG, use Assert.fail to stop the suite Assert.fail("Gmail access blocked by security rules. Error details matched. Terminating all tests."); // For JUnit, you can use: fail("Error message here"); or throw a RuntimeException // throw new RuntimeException("Detected blocked access, stopping all tests."); } // Proceed with your original test steps only if no error was found WelcomePage.ClickonLoginbutton(); loginPage = new LoginPage(driver); loginPage.login(prop.getProperty("AdminUser"), prop.getProperty("Adminpass")); // Replace Thread.sleep with explicit waits for better reliability! // util.waitForElementToAppear(policypage.SelectpolicyText); WebDriverWait wait = new WebDriverWait(driver, 40); wait.until(ExpectedConditions.visibilityOf(policypage.SelectpolicyText)); adminpage= new AdminUserpage(driver); adminpage.Controlpanelclick(); util.waitForElement(driver, 8000, adminpage.TextUserandOrg);
Step 2: Optimize with Explicit Waits (Better Than Thread.sleep)
Instead of using Thread.sleep(40000) which is flaky and inefficient, use explicit waits to wait for elements to appear. This makes your tests more stable and faster.
For example, to wait for the error element (if you can identify it via XPath/CSS):
// Wait up to 10 seconds for the error element to appear By errorLocator = By.xpath("//*[contains(text(), '错误码16') and contains(text(), '此请求被安全规则阻止')]"); try { WebElement errorElement = new WebDriverWait(driver, 10).until( ExpectedConditions.presenceOfElementLocated(errorLocator) ); // If we reach here, the error page is present Assert.fail("Blocked access detected: " + errorElement.getText()); } catch (TimeoutException e) { // No error element found, proceed with tests System.out.println("Gmail URL accessible, continuing test execution."); }
Why This Works
- The check runs immediately after accessing the URL, so you catch the blocked state early.
- Throwing an assertion error (or runtime exception) will stop the current test and, depending on your test suite configuration, halt all subsequent tests (most frameworks like TestNG/JUnit will stop the suite if a critical failure occurs).
- You avoid wasting time running 30 tests that are guaranteed to fail because the initial URL access was blocked.
内容的提问来源于stack exchange,提问作者lokesh sharma

