Selenium Methods and TestNG Concepts with Java
Selenium Methods
get() method
Code: driver.get("URL");
Description: Opens the specified URL in the current browser window.
findElement() method
Code: driver.findElement(By.id("elementID"));
Description: Finds a single web element based on the locator provided.
findElements() method
Code: driver.findElements(By.className("className"));
Description: Finds multiple elements based on the locator.
sendKeys() method
Code: driver.findElement(By.name("username")).sendKeys("testUser");
Description: Types text into an input field.
click() method
Code: driver.findElement(By.xpath("//button[text()='Submit']")).click();
Description: Simulates a click on a web element.
getText() method
Code: String text = driver.findElement(By.tagName("p")).getText();
Description: Gets the visible text from a web element.
getTitle() method
Code: String title = driver.getTitle();
Description: Returns the title of the current page.
getScreenshotAs() method
Code: File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Description: Captures a screenshot of the current browser window.
window handling
Code: String windowHandle = driver.getWindowHandle();
Description: Switches to different browser windows using window handles.
navigate() method
Code: driver.navigate().to("http://example.com");
Description: Navigates to a new URL, similar to get() but with additional options.
alert handling
Code: Alert alert = driver.switchTo().alert();
Description: Switches to an alert box for actions (accept, dismiss).
Data Driven Testing with Excel
To read data from an Excel sheet using Apache POI:
// Step 1: Add Apache POI dependency
// Step 2: Use FileInputStream and Workbook classes to read data.
FileInputStream fis = new FileInputStream("data.xlsx");
Workbook wb = new XSSFWorkbook(fis);
Sheet sheet = wb.getSheetAt(0);
// Reading data from cell
String cellValue = sheet.getRow(1).getCell(0).getStringCellValue();
// Iterating through all rows
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
String data = sheet.getRow(i).getCell(0).getStringCellValue();
System.out.println("Data: " + data);
Take Screenshot
To take a screenshot in Selenium:
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("path/to/save/screenshot.png"));
Extent Reports Generation
To integrate ExtentReports into your Selenium tests:
// Step 1: Add ExtentReports dependency
ExtentReports extent = new ExtentReports("path/to/report.html", true);
ExtentTest test = extent.startTest("Test Name");
// Step 2: Log test steps
test.log(LogStatus.PASS, "Test Step Passed");
test.log(LogStatus.FAIL, "Test Step Failed");
// Step 3: End the test
extent.endTest(test);
extent.flush();
TestNG Annotations and Concepts
@Test
Code: @Test
public void testMethod() { // Test logic }
Description: Defines a test method that is executed by TestNG.
@BeforeMethod
Code: @BeforeMethod
public void setUp() { // Setup code }
Description: Executed before each test method.
@AfterMethod
Code: @AfterMethod
public void tearDown() { // Cleanup code }
Description: Executed after each test method.
@BeforeClass
Code: @BeforeClass
public void beforeClass() { // Before class setup }
Description: Executed once before the first test method in the current class.
@AfterClass
Code: @AfterClass
public void afterClass() { // After class cleanup }
Description: Executed once after all test methods in the current class.
@DataProvider
Code: @DataProvider
public Object[][] data() { return new Object[][] { { data1 }, { data2 } };
Description: Provides data to a test method in the form of a 2D array.
@Parameters
Code: @Parameters({"parameter1"})
public void test(String param) { // Test logic }
Description: Injects parameters from the TestNG XML file into test methods.
@BeforeSuite
Code: @BeforeSuite
public void beforeSuite() { // Before suite setup }
Description: Executed before any test methods in the suite are run.
@AfterSuite
Code: @AfterSuite
public void afterSuite() { // After suite cleanup }
Description: Executed after all test methods in the suite are run.