How to integrate testng in selenium
,
TestNG is a popular testing framework for Java, which provides various annotations, assertions, and configurations to make testing easier and more effective. Here are the general steps to integrate TestNG in Selenium:
Set up your Selenium project by downloading and installing the necessary drivers, such as the ChromeDriver, GeckoDriver, or EdgeDriver.
Add the TestNG dependency to your project by adding the following lines to your project's pom.xml file (if using Maven):
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>
Create a new TestNG class by right-clicking on your project folder, selecting New -> Other -> TestNG -> TestNG Class.
Write your test methods in the new TestNG class using TestNG annotations such as @Test, @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest, and so on. These annotations help TestNG to identify which methods are test methods, which methods are setup methods, and which methods are teardown methods.
In your test methods, use the Selenium WebDriver to perform the necessary actions and assertions on the web page.
Run your TestNG tests by right-clicking on your TestNG class and selecting Run As -> TestNG Test.
Here is an example TestNG test class that uses Selenium WebDriver to search for a keyword on Google:
import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.annotations.*;public class GoogleSearchTest {WebDriver driver;@BeforeMethodpublic void setUp() {// Set up the Chrome driverSystem.setProperty("webdriver.chrome.driver", "path/to/chromedriver");driver = new ChromeDriver();}@Testpublic void testGoogleSearch() {// Navigate to Googledriver.get("https://www.google.com/");// Enter the search keyworddriver.findElement(By.name("q")).sendKeys("TestNG");// Click the search buttondriver.findElement(By.name("btnK")).click();// Verify that the search results page contains the expected textString expectedText = "TestNG - Welcome to TestNG";Assert.assertTrue(driver.getPageSource().contains(expectedText));}@AfterMethodpublic void tearDown() {// Close the browserdriver.quit();}}
This example TestNG class sets up the ChromeDriver, navigates to Google, enters a search keyword, clicks the search button, and verifies that the search results page contains the expected text. The @BeforeMethod annotation sets up the Chrome driver before each test, the @Test annotation specifies the test method, and the @AfterMethod annotation closes the browser after each test.
Related Posts:
education
Subscribe to:
Post Comments (Atom)











0 comments: