Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97977

How to declare global variables using TestNG/Selenium/Java?

$
0
0

I am totally new in automated testing.For practice purposes, I want to create tests for the contact form in Selenium with TestNG. This is the page I am using for practice. I created several test cases but I am not sure how to declare variables that I will be calling later on (in the same class). The code is below, I would like to declare 'Email', 'ErrorField' and 'SendButton' - all suggestions are much appreciated since I tried several ways and I am getting errors.

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.");    }  }}

Viewing all articles
Browse latest Browse all 97977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>