0% found this document useful (0 votes)
39 views9 pages

Test NG

The Selenium Framework is a structured approach to automate testing, enhancing code maintenance, reusability, and readability. It includes three main types of frameworks: Data Driven, Keyword Driven, and Hybrid, with the Page Object Model (POM) and PageFactory being key design patterns for organizing test scripts. TestNG is a Java framework that supports test execution, reporting, and annotations to streamline the testing process.

Uploaded by

patilrohini0224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views9 pages

Test NG

The Selenium Framework is a structured approach to automate testing, enhancing code maintenance, reusability, and readability. It includes three main types of frameworks: Data Driven, Keyword Driven, and Hybrid, with the Page Object Model (POM) and PageFactory being key design patterns for organizing test scripts. TestNG is a Java framework that supports test execution, reporting, and annotations to streamline the testing process.

Uploaded by

patilrohini0224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What is Selenium Framework? Types of Framework?

The Selenium Framework is a code structure that makes code maintenance


easy and efficient.

Without frameworks, users may place the “code” and “data” at the same
location which is neither reusable nor readable.

Frameworks produce beneficial outcomes like increased code reusability,


higher portability, reduced cost of script maintenance, better code readability, etc.

There are mainly three type of frameworks created by Selenium WebDriver to


automate manual test cases

1. Data Driven

2. Keyword Driven

3. Hybrid

1. Data Driven

Data Driven Framework in Selenium is a method of separating Test data


from the test case.

Once the Test data are separated from the test case, it can be easily modified
for a specific functionality without changing the code.

It is used to fetch Test Data from external files like Excel, .csv, .xml or some
database tables.
POM with pagefactory

POM:

It is a java design pattern used for designing of classes in Test script.

Page Object model is an object design pattern in Selenium, where

1. Web pages are represented as classes &

2. The various elements on the page are defined as variables on the


class.

In this case we will use Page Factory to initialize web elements that are
defined in web page classes or Page Objects.

POM strictly follows encapsulation concept where

1. Data member should be declared globally with access level private.

2. Initialize within a constructor with access level public using


pagefactory.

3. Utilize within a method with access level public

Note:

1. No of D.M. that need to be created under a pom class will depends


on no of element that need to be handle in a webpage.

2. pom class will not contain a main method, to run a pom class we
require another class with main() ie. Test class

3. Test class will contain all the navigation steps to test an application
Pagefactory:

It is a class which contains static method like initElements.

To initialize D.M./variables in PageFactory we need to use initElements


method within the constructor.

Syntax:

[Link](driver, this);

initElements will initialize D.M by identifying each component present in a


webpage by using @findBy annotation, which takes locator type as an input.

Syntax:

@FindBy(locator Type ="locator value/exression")

private WebElement D.M. ;

Working of PageFactory:

1. While executing Test Script initElement method will convert all the the
data members @findBy annotation to findElement(),

this process is known as basic/early initialization.--after creating object


of pom class

@FindBy(xpath="//span[text()='KV']") private WebElement PN; ----


> private WebElement PN = [Link]([Link]("//span[text()='KV']"));

2. To perform action on component we need to call methods.

3. Before performing each action initElement method will identifies


component present or not, then it will do complete initialization

this process is known as late/lazy initialization.


Disadvantage of POM:

POM will initialize all the component before performing actions, but
sometimes application may contains few components which will be hidden &
displayed once we perform action on components, that hidden component will not
be displayed while pom initializing, so it throws "No such element" exception.

To overcome drawback of pom, we need to use "PageFactory" which is an


extension of pom.

Difference between POM & PageFactory

POM

1. It will initialize all the D.M. present in class completely before


performing action on components.
2. It will use if webpage is not containing hidden elements.

PageFactory
3. It will initialize the D.M. present in a class before

Performing each action.

4. It will be used if webpage is containing hidden elements.

Advantages of POM:

1) Makes the code reusable

2) It is useful in reducing code duplication

3) It makes ease in maintaining the code (flow in the UI is separated from


verification)

4) Makes code readable (Methods get more realistic names)

5) The Code becomes less and optimized.


TestNG

TestNG is a java unit framework used for writing/designing of Test classes.

->Example of normal Test Class

->Example of TestNG Test class

1. Emailable Report

Report generation is very important when you are doing the Automation Testing as
well as for Manual Testing.

By looking at the result, you can easily identify how many test cases are passed,
failed and skipped.

By looking at the report, you will come to know what the status of the project is.

Selenium web driver is used for automating the web-application, but it won't
generate any reports.

The TestNG will generate the default report.

---Steps to generate Emailable report----

1. Execute Test class and refresh the project.

2. You will get test-output folder.

3. In That folder Right click on the "[Link]" and select the option
Open with the web browser or double click on it.

Note:

1. if we use sop() to display text as a output then result will be displayed in


console not in emailable report.

2. To display text in emailable report we need to use static method log


present in Reporter class.
eg. [Link]("String msg", true)

2. Annotation

1. @Test

2. @BeforeMethod

3. @AfterMethod

4. @BeforeClass

5. @AfterClass

1. @Test:- Used for execution of test method/TC.

2. @BeforeMethod:- It is used for execution of test method before execution


of every test method with an annotation @Test.

3. @AfterMethod:- It is used for execution of test method after execution of


every test method with an annotation @Test.

4. @BeforeClass:- It is used for execution of test method before execution of


test class.

5. @AfterClass:- It is used for execution of test method after execution of


test class.
3. TestNG Keywords:

1. invocationCount

2. priority

3. enabled

4. TimeOut

5. dependsOnMethods

1. invocationCount:

Sometimes same test method/TC need to be executed multiple which can be


possible by using TestNG keyword "invocationCount"

eg. invocationCount=5;

2. priority:

To change test method/TC execution order we need to use TestNG keyword


"priority".

eg. priority=1

Note: priority can be 1. bydefault=0

2. +ve integer

3. -ve integer

4. Duplicate

Priority can't be 1. Decimals


2. Variables

3. enabled:

Disabling a test method/TC in TestNG can be achieved by setting the


enabled attribute of the @Test annotation to false.

eg. enabled=false
[Link]

While executing the automation scripts, test cases may fail for several reasons. To
optimize our next runs we need to re-run only failed test cases

Steps to execute [Link] file

1. Create testng. xml file under project folder.

2. Execute [Link] file

3. In the test-output folder >> testng-failed. xml file will be created.

4. execute "testng-failed. xml"

In this way we can execute fail testcases in TestNG class.

Reasons for fail TC

1. Environment issue

2. Script error

3. Bug

Disable TC Execution

1. From Test class using 'enabled=false' keyword

2. From suite using include/exclude keyword

You might also like