您可以使用By声明(POM Way)来做到这一点,即一次声明并多次调用。 它也适用于同等级和其他等级。 您可以通过公共声明在其他类中访问它。
public class FormValidation {
protected static WebDriver driver;
By errorField = By.xpath("//*[@id=\"center_column\"]/div/ol/li");
By sendButton = By.id("submitMessage");
By email = By.id("email");
@BeforeTest()
public void beforeTest() {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
}
@Test(priority = 0)
public void blankFormTest() {
driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?controller=contact");
WebElement SendButton = driver.findElement(sendButton);
SendButton.click();
WebElement ErrorField = driver.findElement(errorField);
{
Assert.assertEquals(ErrorField.getText(), "Invalid email address.");
}
}
@Test(priority = 1)
public void correctEmailonly() {
WebElement Email = driver.findElement(email);
Email.sendKeys("kasiatrzaska@o2.pl");
WebElement SendButton = driver.findElement(sendButton);
SendButton.click();
WebElement ErrorField = driver.findElement(errorField);
{
Assert.assertEquals(ErrorField.getText(), "The message cannot be blank.");
}
}
}