⚙️ Java + Selenium Programming Concepts
⚙️ Java + Selenium Programming Concepts
🔹 1. Java Basics for Selenium
Before diving into Selenium, it's important to understand core Java concepts:
OOP Principles: Class, Object, Inheritance, Polymorphism, Abstraction, Encapsulation
Control Structures: if-else, for, while, switch
Collections: List, Set, Map
Exception Handling: try, catch, throw, throws, finally
Methods & Constructors
File Handling: For reading test data
🔹 2. Selenium WebDriver Basics
Selenium WebDriver is the main component used for browser automation.
Common WebDriver commands:
java
Copy
Edit
WebDriver driver = new ChromeDriver(); // Launch browser
driver.get("https://example.com"); // Open URL
driver.findElement(By.id("username")).sendKeys("admin"); // Enter text
driver.quit(); // Close browser
🔹 3. Locators in Selenium
Used to find elements on a webpage:
By.id("id")
By.name("name")
By.className("class")
By.tagName("tag")
By.linkText("link text")
By.partialLinkText("partial text")
By.xpath("//tag[@attribute='value']")
By.cssSelector("tag[attribute='value']")
🔹 4. Handling Web Elements
java
Copy
Edit
WebElement element = driver.findElement(By.id("submit"));
element.click(); // Click button
element.getText(); // Get visible text
element.isDisplayed(); // Check visibility
🔹 5. Waits in Selenium
To handle synchronization issues:
Implicit Wait: Wait for a default time for all elements
java
Copy
Edit
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Wait: Wait for a specific condition
java
Copy
Edit
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
🔹 6. Handling Alerts, Frames, and Windows
Alerts:
java
Copy
Edit
Alert alert = driver.switchTo().alert();
alert.accept();
Frames:
java
Copy
Edit
driver.switchTo().frame("frameName");
driver.switchTo().defaultContent();
Windows:
java
Copy
Edit
String mainWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
🔹 7. Page Object Model (POM)
Design pattern to enhance test maintenance and readability.
java
Copy
Edit
public class LoginPage {
WebDriver driver;
By username = By.id("user");
By password = By.id("pass");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user, String pass) {
driver.findElement(username).sendKeys(user);
driver.findElement(password).sendKeys(pass);
}
}
🔹 8. TestNG Framework
Used for test execution and reporting.
java
Copy
Edit
@Test
public void testLogin() {
LoginPage login = new LoginPage(driver);
login.login("admin", "admin123");
}
Features:
Annotations: @BeforeMethod, @AfterMethod, @Test
Assertions: Assert.assertEquals(actual, expected)
Parallel test execution
TestNG XML for grouping and running tests
🔹 9. Handling Excel Files
For data-driven testing using Apache POI:
java
Copy
Edit
FileInputStream file = new FileInputStream("data.xlsx");
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheet("Sheet1");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
🔹 10. Taking Screenshots
java
Copy
Edit
TakesScreenshot ts = (TakesScreenshot) driver;
File src = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("screenshot.png"));
Learn Selenium JAVA Course in Hyderabad
Read More
Running Tests with TestNG XML Suite
Introduction to TestNG Annotations
Visit Our IHUB Talent Training in Hyderabad
Comments
Post a Comment