๐Ÿงฉ Ultimate Selenium + Cucumber Framework Guide (Interview 2025)

๐Ÿš€ Ultimate Selenium + Cucumber Framework Guide (Interview 2025)

A one-stop technical reference for Selenium 4, Cucumber BDD, TestNG, XPath, and OOP-based automation frameworks — perfectly structured for interviews and real-world projects.


๐Ÿ” What is Selenium?

Selenium is an open-source automation testing tool for web applications. It supports multiple browsers, platforms, and languages, enabling effective automated functional testing.

⚙️ Selenium 3 vs Selenium 4 Architecture

  • Selenium 3: Based on JSON Wire Protocol for client-server communication.
  • Selenium 4: Fully compliant with the W3C WebDriver standard, includes relative locators and Chrome DevTools Protocol (CDP) support.

๐Ÿง  OOP Concepts in Selenium Framework (Interview Key)

Question: Where do you use OOPs concepts in your framework?

Answer: OOPs principles — Encapsulation, Inheritance, Abstraction, and Polymorphism — are the foundation of automation frameworks to ensure reusability, modularity, and scalability.

Encapsulation (POM Design Pattern)

// LoginPage.java
public class LoginPage {
  private WebDriver driver;

  @FindBy(id="username") private WebElement username;
  @FindBy(id="password") private WebElement password;
  @FindBy(id="loginBtn") private WebElement loginBtn;

  public LoginPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
  }

  public void login(String user, String pass) {
    username.sendKeys(user);
    password.sendKeys(pass);
    loginBtn.click();
  }
}

Inheritance (Base Class)

// BaseTest.java
public class BaseTest {
  protected WebDriver driver;

  @BeforeMethod
  public void setUp() {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
  }

  @AfterMethod
  public void tearDown() { driver.quit(); }
}

Abstraction (Interface for Waits)

// IWait.java
public interface IWait {
  WebElement waitVisible(By locator, Duration time);
}

// WaitHelper.java
public class WaitHelper implements IWait {
  private WebDriver driver;
  public WaitHelper(WebDriver driver){ this.driver = driver; }

  public WebElement waitVisible(By locator, Duration time) {
    return new WebDriverWait(driver, time)
      .until(ExpectedConditions.visibilityOfElementLocated(locator));
  }
}

Polymorphism (Overloaded Helper Methods)

// ClickHelper.java
public class ClickHelper {
  private WebDriver driver;
  public ClickHelper(WebDriver driver){ this.driver = driver; }

  public void click(By locator){ driver.findElement(locator).click(); }
  public void click(WebElement element){ element.click(); }
  public void click(By locator, Duration wait){
    new WebDriverWait(driver, wait)
      .until(ExpectedConditions.elementToBeClickable(locator)).click();
  }
}

๐ŸŒ Selenium 4 WebDriver Syntax

๐Ÿš€ Browser Setup

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://example.com");

๐Ÿงญ Locators & Element Interaction

driver.findElement(By.id("username")).sendKeys("Masum");
driver.findElement(By.name("password")).sendKeys("Raza");
driver.findElement(By.cssSelector(".btn")).click();
System.out.println(driver.findElement(By.tagName("h2")).getText());

๐Ÿ–ฑ️ Actions Class

Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.id("menu"))).perform();
act.doubleClick(driver.findElement(By.id("edit"))).perform();
act.dragAndDrop(src, trg).perform();

๐Ÿ“‹ Dropdown Handling

Select sel = new Select(driver.findElement(By.id("country")));
sel.selectByVisibleText("India");
sel.selectByValue("IND");
sel.selectByIndex(2);

⚠️ Alert Handling

Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept(); // or alert.dismiss();

๐Ÿ’ก JavaScriptExecutor

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");
js.executeScript("arguments[0].click();", driver.findElement(By.id("submit")));
js.executeScript("document.getElementById('name').value='Masum';");

๐Ÿงฉ Frames & iFrames

driver.switchTo().frame(0);
driver.switchTo().frame("frameName");
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
driver.switchTo().defaultContent();

๐Ÿ”— Navigation Commands

driver.navigate().to("https://site.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();

๐ŸชŸ Multi-window Handling

String main = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
  if (!handle.equals(main)) {
    driver.switchTo().window(handle);
    System.out.println(driver.getTitle());
    driver.close();
  }
}
driver.switchTo().window(main);

๐Ÿช Cookies Management

driver.manage().addCookie(new Cookie("user","Masum"));
for(Cookie c: driver.manage().getCookies())
  System.out.println(c.getName() + "=" + c.getValue());
driver.manage().deleteAllCookies();

⏳ Waits (Implicit / Explicit / Fluent)

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
wait.until(ExpectedConditions.titleContains("Home"));

Wait<WebDriver> fluent = new FluentWait<>(driver)
  .withTimeout(Duration.ofSeconds(30))
  .pollingEvery(Duration.ofSeconds(5))
  .ignoring(NoSuchElementException.class);

๐Ÿ“ธ Screenshot Capture

File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("./Screenshots/image.png"));

๐Ÿ“ Excel Reader (Apache POI)

FileInputStream fis = new FileInputStream("./TestData/data.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
String val = wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue();
System.out.println(val);
wb.close(); fis.close();

๐Ÿ” Broken Link Checker

List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement l : links) {
  String url = l.getAttribute("href");
  if (url != null && !url.isEmpty()) {
    HttpURLConnection c = (HttpURLConnection)new URL(url).openConnection();
    c.setRequestMethod("HEAD");
    c.connect();
    if (c.getResponseCode() >= 400)
      System.out.println(url + " is broken link");
  }
}

๐Ÿงญ XPath Syntax Reference

//tagName[@attribute='value']
//tagName[@attribute='value'][@attribute='value']
//tagName[starts-with(@attribute,'value')]
//tagName[contains(@attribute,'value')]
//tagName[text()='value']
//tagName[contains(text(),'partialvalue')]
//tagName[@attribute='value']/..
//tagName[@attribute='value']/parent::tagname
//tagName[@attribute='value']/following-sibling::tagname
//tagName[@attribute='value']/preceding-sibling::tagname[1]

๐Ÿฅ’ Cucumber BDD Integration

Feature File (Login.feature)

Feature: Login Functionality
  @smoke @login
  Scenario: Verify valid login
    Given user launches the browser
    When user opens "https://example.com/login"
    And user enters username "Masum" and password "Raza"
    And clicks on Login button
    Then user should see the Dashboard

Runner Class

@RunWith(Cucumber.class)
@CucumberOptions(
  features = "src/test/resources/features",
  glue = {"com.framework.steps","com.framework.hooks"},
  plugin = {"pretty","html:target/cucumber.html"},
  tags = "@smoke"
)
public class RunTest { }

Hooks

public class Hooks {
  public static WebDriver driver;

  @Before
  public void setup(){ WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); }

  @After
  public void tearDown(Scenario sc){
    if(sc.isFailed()) System.out.println("Scenario failed: " + sc.getName());
    driver.quit();
  }
}

๐Ÿงช TestNG Grouping & Annotations

Example

public class GroupingExample {
  @Test(groups={"sanity"})
  public void sanityTest(){ System.out.println("Sanity Test"); }

  @Test(groups={"regression"})
  public void regressionTest(){ System.out.println("Regression Test"); }
}

TestNG Annotations Overview

@BeforeSuite: Runs once before all tests in the suite.
@BeforeTest: Runs before any test method in a  tag.
@BeforeClass: Runs before the first method in the current class.
@BeforeMethod: Runs before each test method.
@Test: Marks the actual test case.
@AfterMethod: Runs after each test method.
@AfterClass: Runs after all test methods in the class.
@AfterTest: Runs after all test methods under .
@AfterSuite: Runs once after all tests in the suite.

๐Ÿ—️ Hybrid Framework Structure

project-root/
├─ src/main/java/com.framework/base/
├─ src/main/java/com.framework/pages/
├─ src/main/java/com.framework/utils/
├─ src/test/java/com.framework/steps/
├─ src/test/java/com.framework/hooks/
├─ src/test/resources/features/
└─ Reports/
  • POM: Encapsulation of page actions.
  • TestNG: Execution control, grouping, and parallel runs.
  • Cucumber: Behavior-driven test structure.
  • Data Driven: Excel-driven inputs with Apache POI.
  • Reports: Extent, Allure, and Cucumber HTML reports.

Created by Masum Raza — Automation Test Engineer

Popular posts from this blog

Explore essential Java programs commonly asked in interviews, offering valuable coding insights and practice.

Here is the content refined for clarity and professionalism suitable for someone preparing for a QA Automation interview:

Comprehensive Selenium WebDriver Syntax for Effective Test Automation