0% found this document useful (0 votes)
3 views13 pages

Framework

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

src/

├── main/

│ ├── java/

│ │ └── com/

│ │ └── project/

│ │ ├── base/ # Base classes

│ │ ├── pages/ # Page Object Model classes

│ │ └── utils/ # Utility classes (e.g., ConfigReader)

│ └── resources/

│ └── config.properties # Configuration file

├── test/

│ └── java/

│ └── com/

│ └── project/

│ └── tests/ # Test cases

config/

├── testdata/ # Test data files (e.g., Excel, JSON)

├── reports/ # Generated reports

Explain selenium framework :

- In our automation framework, we integrate TestNG, Cucumber, Maven, and Jenkins to


create a robust CI/CD pipeline.
- We write BDD scenarios in Cucumber and manage test execution using TestNG.
- Our framework is built with Maven, which handles dependencies and builds the project.
- Jenkins acts as our CI/CD tool, automatically triggering test execution on a schedule. (or on
every code commit)
- After test execution, reports are generated using Extent Reports, and Jenkins archives these
reports for review.
- If tests fail, we have a retry mechanism in place to attempt the failed tests again.

Real-Time Scenario: Executing Test Cases in a Continuous Integration Setup


Here’s how a real-time setup might l
ook with an integrated framework:
- Step 1: Writing and Organizing Test Cases
Cucumber Feature Files: Test scenarios are written in Cucumber’s Gherkin syntax (feature
files). These files describe the behavior of the application in simple, plain English steps.
Step Definitions in TestNG: The actual Selenium code to perform each step in the feature
file is written in Step Definition files. TestNG annotations (@BeforeClass, @AfterClass, etc.)
can be used to manage the setup and teardown for test execution.
Page Object Model (POM): Page classes represent different pages of the application. Each
page has web element locators and methods to interact with the page, which keeps your
test code modular and maintainable.
- Step 2: Setting Up Maven for Dependency and Execution
Maven dependencies for Selenium, Cucumber, TestNG, Extent Reports, etc., are configured
in the pom.xml file.
Test execution is configured in Maven using plugins such as the Surefire Plugin or Failsafe
Plugin. These plugins allow you to run TestNG or Cucumber tests by simply executing mvn
test from the command line.
- Step 3: Configuring Jenkins for CI/CD Execution
Jenkins Job Setup: In Jenkins, you create a new job (often called a "pipeline job") for your
test automation project.
Code Integration: Jenkins pulls the latest code from the version control system (e.g., GitHub,
GitLab, or Bitbucket).
Build and Test Execution:
Build Phase: Jenkins uses Maven to build the project (mvn clean install) and resolve
dependencies.
Test Execution Phase:
If Cucumber is used as the primary framework, you may configure Maven to run Cucumber
tests using the Surefire plugin.
For TestNG, specify the testng.xml file in the Jenkins configuration to ensure that TestNG
tests are executed.
Parallel and Cross-Browser Execution: TestNG can be configured for parallel test execution.
This is useful for running tests across different browsers or environments using Selenium
Grid if cross-browser testing is required.
- Step 4: Post-Execution Reporting
Extent Reports or Allure Reports: These are generated to provide detailed insights into the
test execution. You’ll get pass/fail statuses, logs, screenshots (for failures), and more.
Report Archiving: Jenkins can be configured to archive these reports so they’re available to
the team for review.

TestNG and Cucumber integration:


Yes, in real-time projects, it’s possible to have **both TestNG and Cucumber integrated
within a single project**. This setup is common in organizations where certain tests are
more effectively handled with **TestNG** (like unit tests or integration tests), while
**Cucumber** is used for **Behavior-Driven Development (BDD)** to support better
collaboration between QA, developers, and non-technical stakeholders. Here’s how they
work together and how the integration is typically set up for streamlined test execution.

#### Why Combine TestNG and Cucumber?


1. **Cucumber for BDD**: Cucumber is preferred for end-to-end tests in a BDD style, where
test cases are written in Gherkin syntax. This is ideal for feature tests that require human-
readable scenarios.
2. **TestNG for Flexibility**: TestNG can be used for additional testing requirements like
unit tests, integration tests, or scenarios where specific TestNG features (e.g., parallel
execution, priority ordering, or specific annotations like `@DataProvider`) are beneficial.

### How Integration is Set Up


1. **Separate Test Management**:
- **TestNG** is generally used to manage and execute non-BDD tests in a traditional
format.
- **Cucumber** is used for BDD-style feature tests.

2. **Common Setup with Maven**:


- **Maven** is used to manage dependencies and the project structure, integrating
libraries for both TestNG and Cucumber.
- The `pom.xml` file includes dependencies for both **TestNG** and **Cucumber**, along
with any reporting tools (e.g., Extent Reports or Allure Reports).

3. **Unified Test Execution Using TestNG’s Runner with Cucumber**:


- You set up a **TestNG runner** class specifically to execute Cucumber feature files.
- In a typical setup, Cucumber tests can be executed as TestNG tests, allowing TestNG to
handle Cucumber features.

### Step-by-Step Example of Setting Up Test Execution with TestNG and Cucumber

#### 1. Configure Dependencies in `pom.xml`

Include dependencies for both TestNG and Cucumber. Here’s an example:

```xml
<dependencies>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>

<!-- Cucumber -->


<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.2.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.2.3</version>
</dependency>

<!-- Add other dependencies like Selenium, Extent Reports, etc. -->
</dependencies>
```

#### 2. Set Up a TestNG Runner for Cucumber

Create a **TestNG runner** class to run Cucumber feature files. This runner allows
Cucumber tests to be executed as TestNG tests, making use of TestNG’s parallel execution,
reporting, and suite management.

```java
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;

@CucumberOptions(
features = "src/test/resources/features",
glue = "stepdefinitions",
plugin = {"pretty", "html:target/cucumber-reports"}
)
public class CucumberTestRunner extends AbstractTestNGCucumberTests {

@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
```
- The `@CucumberOptions` annotation specifies the **feature files** and **step
definition** locations.
- The `DataProvider` allows parallel execution for each scenario if desired.

#### 3. Using TestNG’s `testng.xml` for Suite Management

TestNG’s `testng.xml` can be used to manage and control the execution order of both
**Cucumber tests** and **traditional TestNG tests**. You can include the
`CucumberTestRunner` in the `testng.xml` file alongside other TestNG test classes.

Example `testng.xml`:

```xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Automation Suite" parallel="tests" thread-count="2">

<!-- Run Cucumber tests via TestNG runner -->


<test name="Cucumber Tests">
<classes>
<class name="runner.CucumberTestRunner"/>
</classes>
</test>

<!-- Run regular TestNG tests -->


<test name="TestNG Tests">
<classes>
<class name="tests.SampleTest1"/>
<class name="tests.SampleTest2"/>
</classes>
</test>
</suite>
```

- The `CucumberTestRunner` is run as a TestNG test.


- Additional TestNG test classes (e.g., unit tests, integration tests) can be included in the
same suite, enabling the entire suite to run together.

#### 4. Reporting and CI/CD Integration

1. **Extent Reports/Allure Reports**: Both TestNG and Cucumber results can be integrated
into unified reports for a single view of test results. Extent Reports or Allure Reports can
capture results from both TestNG and Cucumber test runs.
2. **Jenkins CI/CD Integration**:
- Configure **Jenkins** to trigger the `testng.xml` suite for continuous integration.
- Jenkins can handle notifications, archive reports, and provide feedback to the team on
test outcomes.
Gist
### Real-Time Execution Scenario Example

1. **Developer Pushes Code**: A developer commits code, which triggers Jenkins to build
the project using Maven (`mvn clean install`).
2. **Running Tests**:
- Jenkins runs `testng.xml`, which:
- Executes Cucumber tests via the `CucumberTestRunner`.
- Runs additional TestNG tests defined in the suite.
3. **Parallel and Sequential Execution**: Using `testng.xml`, tests can be configured to run
in parallel (for Cucumber scenarios) and in sequence (for TestNG tests).
4. **Report Generation and Notifications**:
- After execution, reports are generated and stored in Jenkins, and notifications are sent to
relevant team members with links to the report.

### Summary Answer for an Interview

In real-time projects, we may integrate **TestNG and Cucumber in a single framework** to


get the best of both.

Cucumber handles BDD scenarios, while TestNG is used for other types of tests. We typically
set up a **TestNG runner** to execute Cucumber feature files, and both Cucumber and
TestNG tests are managed through **testng.xml** for suite execution. This integration
allows us to use TestNG’s capabilities (like parallel execution and dependency management)
with Cucumber’s BDD features, providing comprehensive test coverage across different types
of tests. **Jenkins** manages the CI/CD pipeline, executing our tests on each code commit,
and **Maven** handles dependencies and builds.
Parallel Execution
Running both TestNG and Cucumber tests in parallel can be challenging due to
differences in how parallel execution is managed.

Parallel Execution Limitations

● Cucumber Parallelism: While Cucumber supports parallel execution, it is often


limited to running scenarios in parallel rather than feature files. Using
@DataProvider in the Cucumber runner with TestNG helps achieve scenario-level
parallelism, but achieving true parallelism across feature files can be complex.
● TestNG Parallelism: TestNG provides robust support for parallel execution at
multiple levels (tests, classes, methods), but it doesn’t easily handle Cucumber
features directly without extra configuration.

Realistic Parallel Execution Setup

In real projects, it’s common to run either TestNG or Cucumber tests in parallel
independently, rather than trying to run both simultaneously in parallel. For example:

1. Separate Test Execution:


o Run Cucumber tests in parallel by setting up the TestNG runner specifically
for Cucumber scenarios.
o Run TestNG tests in parallel within the same suite or job but avoid mixing
parallel execution of both TestNG and Cucumber directly.
2. Using Multiple Jenkins Jobs:
o Set up one Jenkins job to handle Cucumber tests and configure it to run
scenarios in parallel using the @DataProvider in the TestNG-Cucumber
runner.
o Set up a second Jenkins job for TestNG-specific tests that also runs in
parallel where possible.
o This separation allows each framework to execute in parallel as designed,
without the complexity of trying to combine them.

Real-World Scenario Answer for Interviews

In real-time projects, we usually separate TestNG and Cucumber tests in parallel execution
due to their differences. If parallelism is required, we handle it at the job level in Jenkins,
where one job may run Cucumber scenarios in parallel, and another job runs TestNG tests in
parallel. This approach maintains test efficiency while avoiding the complexities of managing
both frameworks simultaneously in parallel within a single suite.
● Programming language

● Framework type: Data driven framework or keyword driver by using page object model

● POM: separate classes for each web page.

● Packages:

We have separate packages for pages and tests.

⮚ All the web page related classes come under the pages package

⮚ And all the tests related classes come under Tests package.

https://chatgpt.com/share/f9f28d22-ecf8-4d11-bd5d-eb4f185649a2
● https://chatgpt.com/share/f9f28d22-ecf8-4d11-bd5d-eb4f185649a2

● https://chatgpt.com/share/f9f28d22-ecf8-4d11-bd5d-eb4f185649a2


Here are some common automation framework interview questions with answers based on your
experience:

1. Where did you implement OOPS concepts in your framework? Give examples
for each one.
Answer:

1. Encapsulation:
○ Encapsulation is achieved by keeping WebElement locators private in the
webelements package classes and accessing them through public getter methods
in the functions package.
● Example:
java
Copy code
public class LoginPage {

● private By username = By.id("username");

● private By password = By.id("password");

● public By getUsername() {

● return username;

● }

● public By getPassword() {

● return password;

● }

● }


2. Inheritance:
○ The BaseClass serves as the parent class, providing common methods such as
browser setup, initialization of WebDriver, and handling timeouts. Test classes in the
testcases package extend BaseClass.
● Example:
java
Copy code
public class BaseClass {

● protected WebDriver driver;

● public void setup() {

● // Code to initialize WebDriver

● }

● }

● public class LoginTest extends BaseClass {

● @Test

● public void testLogin() {

● setup();

● // Test steps

● }

● }


3. Polymorphism:
○ Method overloading is used for reusable methods like taking screenshots, where we
provide different parameters.
● Example:
java
Copy code
public void takeScreenshot(String testName) {

● // Logic to save screenshot with testName

● }

● public void takeScreenshot(WebDriver driver, String testName)


{

● // Logic to save screenshot with driver instance

● }


4. Abstraction:
○ Interfaces are used for reporting functionality like ILog to separate the logging
mechanism (Log4j, Extent Reports) from implementation.
● Example:
java
Copy code
public interface ILog {

● void logInfo(String message);

● void logError(String message);

● }

● public class Log4jLogger implements ILog {

● public void logInfo(String message) {

● // Log using Log4j


● }

● public void logError(String message) {

● // Log errors using Log4j

● }

● }

2. What is the linkage between config.properties file, BaseClass, screenshots,


and timeouts?
Answer:

1. config.properties:
○ Contains key-value pairs for environment-specific data like browser, URL,
timeout, and screenshotPath.
● Example:
makefile
Copy code
browser=chrome

● timeout=30

● screenshotPath=./screenshots


2. BaseClass:
○ Reads the properties file using Properties class and sets the configurations.
● Example:
java
Copy code
Properties prop = new Properties();

● FileInputStream fis = new


FileInputStream("config.properties");
● prop.load(fis);

● driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(I
nteger.parseInt(prop.getProperty("timeout"))));


3. Screenshots:
○ Screenshot path is fetched from config.properties in the BaseClass and
passed to the screenshot method.
● Example:
java
Copy code
String screenshotPath = prop.getProperty("screenshotPath");

● public void takeScreenshot(String testName) {

● File src = ((TakesScreenshot)


driver).getScreenshotAs(OutputType.FILE);

● File dest = new File(screenshotPath + testName + ".png");

● FileUtils.copyFile(src, dest);

● }


4. Timeouts:
○ Timeout values like implicit, explicit, and Fluent Wait are managed centrally through
config.properties.
● Example:
java
Copy code
int timeout = Integer.parseInt(prop.getProperty("timeout"));

○ WebDriverWait wait = new WebDriverWait(driver,


Duration.ofSeconds(timeout));

You might also like