Selenium professional test automation framework
✅ 1. Selenium with Cucumber
Selenium is a browser automation tool, and Cucumber is a testing framework that supports Behavior Driven Development (BDD) using Gherkin syntax. Together, they allow writing human-readable test cases (feature files) that map to Selenium code. Cucumber helps define user behavior, and Selenium automates those behaviors in browsers. This combination improves collaboration between testers, developers, and business stakeholders.
✅ 2. Selenium Web Elements
Selenium uses WebElement
interface to interact with HTML elements like buttons, inputs, dropdowns, etc. Common methods include click()
, sendKeys()
, getText()
, isDisplayed()
. These elements are located using locators like id
, name
, xpath
, cssSelector
, etc. Proper element handling ensures robust and maintainable automation scripts.
✅ 3. Page Object Model (POM)
Page Object Model is a design pattern in Selenium where each webpage is represented by a Java class containing WebElements and corresponding actions/methods. It improves code reusability, readability, and maintainability. Changes in UI require updates only in the page class, not in test logic. It keeps test steps separate from locators and actions.
✅ 4. Step Definitions
Step Definitions link Cucumber feature file steps (written in Gherkin) to actual Java methods containing Selenium logic. Each step in the .feature
file is implemented in a method annotated with @Given
, @When
, or @Then
. These methods execute the actions like navigating, clicking, entering text, or asserting results.
✅ 5. Hooks in Cucumber
Hooks are special methods in Cucumber (annotated with @Before
, @After
) that run before or after every scenario. They are used for setup (like browser launch) and teardown (like closing browser, logging). You can also use tagged hooks to run certain hooks only for specific scenarios.
✅ 6. JUnit (with Cucumber)
JUnit is a unit testing framework for Java and is used to execute Cucumber tests. In Cucumber, the @RunWith(Cucumber.class)
and @CucumberOptions
annotations in a test runner class configure and run feature files. JUnit manages test execution, reports, and lifecycle management of Cucumber test cases.
✅ Example on Gmail login
✅ 1. Feature File: gmail_login.feature
Located at: src/test/resources/features/gmail_login.feature
✅ 2. Page Object: GmailLoginPage.java
✅ 3. Step Definitions: GmailLoginSteps.java
✅ 4. Hooks: Hooks.java
(Dynamic Browser + Screenshot + Timestamp)
✅ 5. Test Runner: TestRunner.java
✅ 6. Maven pom.xml
(Key Dependencies)
✅ Screenshots Folder (Auto-Created)
Failed scenario screenshots with timestamp will be stored in:
✅ Optional: Profiles via Maven (for browser/env)
Use Maven profile in pom.xml
or read from config.properties
for real projects.
✅ Run the Test
-
Run
TestRunner.java
as a JUnit test. -
Or from terminal:
No comments:
Post a Comment