Honestly, I wouldn't worry about it. What this form does and what you are trying to verify doesn't happen on real life forms. In actual forms, you either get a success message or it transitions to another page, both of which can be verified easily. This section of the page is intended to validate the failure cases like leave the City field blank, submit the form, and then verify that the, "Please provide a valid city." error message appears.
I investigated the page and this is what I found:
You can't validate the green checkbox or red X for success/failure. It's some native formatting to the INPUT in the form.
You also can't validate the green/red border around the INPUT. It's also some native formatting to the INPUT in the form.
The only thing I could find to validate success was what I said above... whether the error message is displayed. The downside is that there is no formatting change on the DIV that displays the error message, e.g. "display: none" vs "display: block", etc. The only thing we can rely on is to use
element.isDisplayed()on each message element.The easiest way to determine if the page has reloaded after submitting the form is to check one of the elements for stale. I chose the Submit Form button.
Once you put all of these together, you get the code below.
WebDriver driver = new ChromeDriver();
driver.get("https://play1.automationcamp.ir/forms.html");
driver.findElement(By.id("validationCustom03")).sendKeys("City");
driver.findElement(By.id("validationCustom04")).sendKeys("State");
driver.findElement(By.id("validationCustom05")).sendKeys("Zip");
driver.findElement(By.id("invalidCheck")).click();
WebElement submitFormButton = driver.findElement(By.xpath("//button[text()='Submit Form']"));
submitFormButton.click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.stalenessOf(submitFormButton));
boolean cityErrorIsDisplayed = driver.findElement(By.id("invalid_city")).isDisplayed();
boolean stateErrorIsDisplayed = driver.findElement(By.id("invalid_state")).isDisplayed();
boolean zipErrorIsDisplayed = driver.findElement(By.id("invalid_zip")).isDisplayed();
boolean termsErrorIsDisplayed = driver.findElement(By.id("invalid_terms")).isDisplayed();
Assert.assertFalse(cityErrorIsDisplayed, "Verify City error is not displayed");
Assert.assertFalse(stateErrorIsDisplayed, "Verify State error is not displayed");
Assert.assertFalse(zipErrorIsDisplayed, "Verify Zip error is not displayed");
Assert.assertFalse(termsErrorIsDisplayed, "Verify Terms error is not displayed");
driver.quit();