Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用TestNG/Selenium/Java声明全局变量?_Java_Selenium_Testng - Fatal编程技术网

如何使用TestNG/Selenium/Java声明全局变量?

如何使用TestNG/Selenium/Java声明全局变量?,java,selenium,testng,Java,Selenium,Testng,我对自动化测试完全陌生。 出于实践目的,我想使用TestNG为Selenium中的contact表单创建测试。我正在练习。我创建了几个测试用例,但我不确定如何声明稍后将调用的变量(在同一个类中)。下面是代码,我想声明“Email”、“ErrorField”和“SendButton”-所有建议都非常感谢,因为我尝试了几种方法,但都出现了错误 public class FormValidation { protected static WebDriver driver; @BeforeTe

我对自动化测试完全陌生。 出于实践目的,我想使用TestNG为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.");
    }

  }
}
您可以使用按声明(POM方式)来实现,即一次声明,多次调用。它也适用于同一等级和其他等级。您可以通过公共声明在其他类中访问它


实际上您已经创建了一个类变量:
protectedstaticwebdriver-您可以对任何其他变量执行相同的操作,例如
私有静态字符串submitMessage=“submitMessage”在开始处,然后在类中的任何位置使用该变量。请谷歌页面对象模型。
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.");
    }    
  }

}