Building an automation framework
Learn to build a Java automation framework. Set up project structure. Create reusable components. Implement test cases. Integrate with CI/CD pipelines.
Examples
// Example 1: Setting up project structure
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestBase {
protected WebDriver driver;
public void setup() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}
public void teardown() {
if (driver != null) {
driver.quit();
}
}
}
// Example 2: Creating reusable components
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
private WebDriver driver;
private By usernameField = By.id("username");
private By passwordField = By.id("password");
private By loginButton = By.id("login");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String username) {
driver.findElement(usernameField).sendKeys(username);
}
public void enterPassword(String password) {
driver.findElement(passwordField).sendKeys(password);
}
public void clickLogin() {
driver.findElement(loginButton).click();
}
}
// Example 3: Implementing test cases
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class LoginTest extends TestBase {
private LoginPage loginPage;
@Before
public void setUp() {
setup();
loginPage = new LoginPage(driver);
driver.get("https://example.com/login");
}
@Test
public void testLoginSuccess() {
loginPage.enterUsername("testuser");
loginPage.enterPassword("testpass");
loginPage.clickLogin();
assertTrue(driver.getTitle().contains("Dashboard"));
}
@After
public void tearDown() {
teardown();
}
}
FAQ (interview questions and answers)
-
What is the first step in building an automation framework?
Create test cases
Set up project structure
Write reusable components
-
Why is it important to create reusable components?
To avoid code duplication
To increase complexity
To slow down tests
-
How do you validate successful login in a test case?
Check the URL
Check the console log
Check the page title
-
Which library is used for assertions in the examples above?
TestNG
JUnit
Mockito
-
What is a common tool for WebDriver implementation?
Selenium
Puppeteer
Cypress
Your Total Score: 0 out of 5
Remember to just comment if you have any doubts or queries.

No comments:
Post a Comment
Note: Only a member of this blog may post a comment.