How to create Test Case using TestNG?
Creating TestCase is simple using TestNG. The main motive to create a Test Case using TestNG is because of it powerful framework and to avoid main() method.
Steps to create a TestNG Test case
Creating TestCase is simple using TestNG. The main motive to create a Test Case using TestNG is because of it powerful framework and to avoid main() method.
Steps to create a TestNG Test case
- Ensure TestNG is correctly configured in Eclipse(Install TestNG)
- Import the Package import org.testng.annotations.* in the respective Class
- Use Annotation @ Test to run a particular Test Case
- Run the Program as TestNG
Running the Program as a TestNG The below diagram depicts the flow of Running of Test Cases in accordance to Annotations used |
Flow of Running the Test NG Cases |
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
public class TestNGProgram {
public WebDriver driver;
@BeforeClass
public void setup()
{
this.driver=new FirefoxDriver();
}
@AfterClass
public void teardown()
{
this.driver.quit();
}
@Test
public void loadpage()
{
driver.get("https://www.facebook.com/");
String s =driver.getTitle();
System.out.println(s);
}
}
Notes
- this is a keyword in Java that can be used to refer current class instance variable hence it used to access the driver variable.
- Order of execution of above tests annotation is @BeforeClass>@test>@AfterClass
- There is no need to write main() method
- Title of facebook page is fetched using getTitle() method
Output
0 comments:
Post a Comment