我对自动化测试还是个新手。出于练习的目的,我想使用TestNG.This is the page在Selenium中为contact表单创建测试。我创建了几个测试用例,但我不确定如何声明稍后将调用的变量(在同一个类中)。代码如下,我想声明“Email”、“ErrorField”和“SendButton”-非常感谢所有建议,因为我尝试了几种方法,但都出现了错误。
public class FormValidation {
protected static WebDriver driver;
@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(By.id("submitMessage"));
SendButton.click();
WebElement ErrorField = driver.findElement(By.xpath("//*[@id=\"center_column\"]/div/ol/li"));
{
Assert.assertEquals(ErrorField.getText(), "Invalid email address.");
}
}
@Test(priority = 1)
public void correctEmailonly() {
WebElement Email = driver.findElement(By.id("email"));
Email.sendKeys("kasiatrzaska@o2.pl");
WebElement SendButton = driver.findElement(By.id("submitMessage"));
SendButton.click();
WebElement ErrorField = driver.findElement(By.xpath("//*[@id=\"center_column\"]/div/ol/li"));
{
Assert.assertEquals(ErrorField.getText(), "The message cannot be blank.");
}
}
}
1条答案
按热度按时间7gcisfzg1#