TA Lecture-4
Introduction to Selenium
and
TestNG Framework
Why Automated Testing?
• Automated Software Testing saves time and money.
• Vastly increase your test coverage.
• Automated Testing improves accuracy.
• Automation does what manual testing cannot.
• Automated QA Testing helps developers and testers.
• QA and Dev team morale improves.
Pros & Cons of Automated Testing
Advantages: Disadvantages:
• Reliable: eliminating human • High investment is needed in
error the tools and training
• Reusability • Pesticide paradox
• Better software quality • Requires high proficiency
• Fewer labour resources are • Test script debugging can be
required challenging
• Cost reduction • Continuous script maintenance
Selecting an Automation Tool
• Support for various OS, Applications and Platforms.
• Support for Mobile devices.
• Support for various software testing types.
• Creating automated tests without programming.
• Recording automated software tests.
• Creating cross-browser web tests.
• Creating cross-platform mobile web tests.
Automation Testing Tools in the Market
Commercial Testing Tools: Open Source Testing Tools:
• HP QuickTest Professional • Selenium
• HP Mercury WinRunner • Watir
• IBM Rational Functional Tester • Sikuli
• Micro Focus Silk Test • FitNesse
Selenium
What is Selenium?
• Chemical symbol: Se
• Atomic Number: 34
• Atomic mass: 78.96
• Melting point: 428.9F
• Element classification: Non-Meta
• Crystal structure: Hexagonal
What is Selenium?
• Browser-based Web Application Testing Tool
• Open Source License
• Extensible Framework
• Support Mobile testing Android / iOS
• Write test cases using languages like Java, C#, Python, Ruby & HTML
• Supports native browser testing
History of Selenium
• Developed by Jason Huggins in 2004 while working on in in-house
project in ThoughtWorks. He started working on Selenium Core.
• Dan Fabulich and Nelson Sproul created Selenium RC.
• Shinya Kasatani in Japan become interested in Selenium, and
realised that he could wrap the core into an IDE module into the
Firefox browser, and be able to record tests as well as play them
back in the same plugin.
Advantages of Selenium
• Open source, free of cost for companies.
• Supports native testing for all major browsers.
• Customize according to the requirements.
• AJAX-based testing.
• Only tool to support Test Case language of choice Java, C#, Python
& Ruby.
• Selenium 2.., Web Driver is developed by Google and all the Google
products are tested with Selenium
• Web companies such as Facebook, Twitter, LinkedIn, Groupon uses
Selenium.
Selenium Components
Selenium IDE
• Selenium IDE allows testers and
developers to create and run automated
tests for web applications without
needing to write any code.
• Firefox add-on for easy recording and
replaying tests.
• Intelligent field selection will use IDs,
names, or XPath as needed.
• Autocomplete for all common Selenium
commands.
• Debug and set breakpoints.
• Save tests as HTML, Ruby scripts, or any
other format.
Using Selenium IDE
Selenium RC
• Selenium Remote Control (RC) is a server,
written in Java, that accepts commands
for the browser via HTTP.
• RC makes it possible to write automated
tests for a web application in any
programming language, which allows for
better integration of Selenium in existing
unit test frameworks.
• To make writing tests easier, Selenium
project currently provides client drivers
for PHP, Python, Ruby, .NET, Perl and Java.
The Java driver can also be used with
JavaScript (via the Rhino engine).
Selenium RC Architecture
• The client libraries can communicate with the Selenium RC Server passing each
Selenium command for execution.
• Then the server passes the Selenium command to the browser using Selenium-Core
JavaScript commands.
• A new instance of selenium RC server is needed to launch html test case - which means
that the port should be different for each parallel run.
• However, for Java/PHP test case only one Selenium RC instance needs to be running
continuously.
Selenium Grid
• Selenium-Grid allows you run your tests
on different machines against different
browsers in parallel.
• That is, running multiple tests at the same
time against different machines running
different browsers and the operating
systems.
• Essentially, Selenium-Grid support
distributed test execution.
• It allows for running your tests in
a distributed test execution environment.
Selenium Grid
Selenium Web Driver
• The most recent and popular Selenium
tool is WebDriver.
• It offers a simple and effective API for
automating web application testing.
• Does not require a core engine like RC and
interacts natively with the browser
applications using browser-specific drivers
such as ChromeDriver, GeckoDriver,
or EdgeDriver.
• Numerous programming languages,
including Java, Python, C#, and Ruby, are
supported.
Selenium Web Driver: Architecture
1. Selenium Client Libraries / Selenium Language Bindings
2. JSON Wire Protocol
3. Browser Drivers
4. Browsers
Selenium Web Driver: Architecture
1. Selenium Language Bindings / Selenium Client Libraries
– Selenium developers have built language bindings/Selenium Client Libraries
in order to support multiple languages. All the supported language bindings
can be downloaded from the official website:
(https://www.seleniumhq.org/download/#client-drivers)
2. JSON Wire Protocol
– JSON (JavaScript Object Notation) is an open standard for exchanging data on
web. It supports data structures like object and array.
3. Browser Drivers
– Selenium uses drivers, specific to each browser in order to establish a secure
connection with the browser without revealing the internal logic of browser's
functionality.
4. Browsers
– Internet Explorer, Mozilla Firefox, Google Chrome, Safari
Selenium Suite
Selenium
suite
Selenium
Selenium RC Web Driver Selenium Grid
IDE
Merged
Selenium 2
Setup Selenium Development Environment
1. Install Java and configure Java Path
2. Install IDE (Eclipse, IntelliJ IDEA)
3. Create new Java project
4. Download and configure Selenium jars
5. Download and configure Selenium driver
6. Create first test script and execute
1. Install Java and Configure Java Path
• Download Java :
https://www.oracle.com/technetwork/java/javase/downloads/index.html
• Install Java
• Set Java Environment Path
• Verify Java Installation
2. Install Eclipse
• Download Eclipse: https://www.eclipse.org/downloads/
• Install Eclipse
3. Create New Java Project
4. Download and Configure Selenium Jars
• Download the WebDriver java client:
https://www.selenium.dev/downloads/
• Extract the files
• Right-click on the project > Build Path > Configure Build Path
• Select the "Java Build Path“
• Click on the "Libraries" tab
• Select “Classpath”
• Click on the "Add External JARs"
5. Download and Configure Selenium Driver
• Download the browser-specific WebDriver :
https://www.selenium.dev/downloads/
• For Chrome browser:
https://chromedriver.chromium.org/downloads/version-selection
6. Create First Test Script and Execute
package com.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTestCase
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver",
"D:\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
String title = driver.getTitle();
System.out.print(title);
}
}
TestNG
What is TestNG?
• TestNG is an open source automated testing framework; where NG
means Next Generation.
• TestNG is similar to JUnit, but it is not a JUnit extension.
• It is designed to be better than JUnit, especially when testing
integrated classes.
• TestNG allows organizing, prioritizing and reporting the test
automation scripts (Selenium cannot do this).
• TestNG allow us to execute multiple test cases simultaneously.
• It also helps us separate the test case into suites.
Features of TestNG
• TestNG provides several annotations to organize the test cases.
• TestNG uses more Java and OO features.
• TestNG can be used to group and prioritize test cases.
• Supports parameterization (multi-browser testing).
• Supports testing integrated classes (e.g., by default, no need to
create a new test class instance for every test method).
• Supports data-driven testing using Data Providers.
• It can generate HTML test reports.
• It supports parallel execution.
• Logs can be generated.
Installation of TestNG
• Install Eclipse IDE
• Method 1
– Go to Help > Eclipse Marketplace
– Search for “TestNG for Eclipse”
– Install the plugin
• Method 2
– Go to Help > Install new software
– Search for “TestNG”
– Install the plugin
• Note: Do not forget to uncheck the checkbox ‘TestNG M2E (Maven)
Integration (Optional)’.
• Restart Eclipse
Installation of TestNG: Method-1
Installation of TestNG: Method-1
Steps to Create TestNG – JAVA Project
1. In Eclipse, go to File > New > Java Project
2. Right-click on src and choose New > Other
3. Click on TestNG and select TestNG class
4. On the next screen, browse and choose the Source folder
5. Provide the Source folder and Package information and choose
annotations. eg: @BeforeTest and @AfterTest and click finish.
6. Define the business logic in the Test method
7. Run the class file as TestNG Test
Steps to Create TestNG – MAVEN Project
Maven is a powerful project management tool that is based on POM (project
object model). It is used for projects build, dependency and documentation.
1. In Eclipse, go to File > New > Maven Project
2. On the next screen, enter Group Id, Artifact Id, Name, Description and
click Finish
3. Open pom.xml and add TestNG dependency from
https://mvnrepository.com/artifact/org.testng/testng
4. Provide the Source folder and Package information and choose
annotations. eg: @BeforeTest and @AfterTest and click finish.
5. Define the business logic in the Test method
6. Run the class file as TestNG Test
TestNG Annotations
TestNG Annotations
TestNG Annotation is a piece of code which is inserted inside a program or
business logic used to control the flow of execution of test methods.
• @Test : To mark a method as a test method
• @BeforeMethod: Executes before each test method
• @AfterMethod: Executes after each test method
• @BeforeClass: Executes before the first test method in the current class
• @AfterClass: Executes after all the test methods in a current class
• @BeforeTest: Executes before any test methods of available classes
• @AfterTest: Executes after all the test methods of available classes
TestNG Annotations (cont.)
• @BeforeSuite: Executes before all the tests in this suite
• @AfterSuite: Executes after all tests executed in this current suite
• @BeforeGroup: Executes before the first test method run in that specified
group
• @AfterGroup: Executes after the end of all the test methods executed in
that specified group
• @Parameters: pass parameters to tests as arguments
• @Listeners: listens to every event that occurs in a selenium code
• @Factory: used to specify a method as a factory for providing objects to
be used by TestNG for test classes. The method marked with @Factory
annotation should return Object array
Hierarchy of the TestNG Annotations
1. @BeforeSuite
2. @BeforeTest
3. @BeforeClass
4. @BeforeMethod
5. @Test
6. @AfterMethod
7. @AfterClass
8. @AfterTest
9. @AfterSuite
Example
public class testngAnnotations { @AfterClass
@Test public void afterClass() {
public void test1() { System.out.println("After Class");
System.out.println("Test Case 1"); }
} @BeforeTest
@Test public void beforeTest() {
public void test2() { System.out.println("Before Test");
System.out.println("Test Case 2"); }
} @AfterTest
@BeforeMethod public void afterTest() {
public void beforeMethod() { System.out.println("After Test");
System.out.println("Before Method"); }
} @BeforeSuite
@AfterMethod public void beforeSuite() {
public void afterMethod() { System.out.println("Before Suite");
System.out.println("After Method"); }
} @AfterSuite
@BeforeClass public void afterSuite() {
public void beforeClass() { System.out.println("After Suite");
}
System.out.println("Before Class");
}
}
Output
Before Suite
Before Test
Before Class
Before Method
Test Case 1
After Method
Before Method
Test Case 2
After Method
After Class
After Test
PASSED: test1
PASSED: test2
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
After Suite
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
Attributes Supported by TestNG
Annotations
Attributes Supported by TestNG Annotations
1. enabled
• This attribute is used to enable (when set to true) or disable (when
set to false) the execution of a particular test.
@Test(enabled = true)
• Syntax:
@Test(enabled = false)
Example of enabled
public class DisableTestClass
{
@Test(enabled = true)
public void m1()
{
Output:
System.out.println("Hello");
} Hello
Exit
@Test(enabled = false)
public void m2() PASSED: m3
{ PASSED: m1
System.out.println(“World");
}
@Test
public void m3()
{
System.out.println(“Exit");
}
}
2. dataProvider
• This attribute is used to provide data to the test method directly in
data-driven testing.
• Syntax: @Test(dataProvider = "getData")
3. dataProviderClass
• This attribute is used to call the DataProvider method from another
class.
• Syntax: @ Test(dataProvider = "getData", dataProviderClass = Hello.class)
4. groups
• This attribute specifies the list of groups this method or class
belongs to.
• Syntax: @ Test(groups = {“GroupName1”, “GroupName2”,…})
Example of groups
public class GroupingTestMethods
{
@Test(groups = {“4 wheeler”})
public void Car()
{
Output:
S.o.p(“Car: 4 wheeler”);
} Bike: 2 wheeler
@Test(groups = {“2 wheeler”}) Car: 4 wheeler
public void Bike()
{ Light motor vehicle
S.o.p(“Bike: 2 wheeler");
}
@Test(groups = {“2 wheeler”,
“4 wheeler”})
public void LMV()
{
S.o.p(“Light motor vehicle");
}
}
5. dependsOnGroups
• This attribute is used to make test methods depend on a particular
group.
• We can also specify a list of groups this method depends on.
• Syntax: @Test(dependsOnGroups = {"GroupA", "GroupB", . . .})
Example of dependsOnGroups
public class dependsOnGroupsEx
{
@Test(groups = "GroupA")
public void m1()
{ Output:
S.o.p("m1-GroupA");
} m1-GroupA
@Test(dependsOnGroups = "GroupB") m3-GroupB
public void m2() m2 is dependent on GroupB
{
S.o.p("m2 is dependent on GroupB");
}
@Test(groups = "GroupB")
public void m3()
{
S.o.p("m3-GroupB");
}
}
6. dependsOnMethods
• The attribute is used to make the test method depend on a
particular method.
• Syntax: @Test(dependsOnMethods = {"Method1","Method2",…})
Example of dependsOnMethods
@Test(dependsOnMethods = "m2")
public void m1()
{
S.o.p("Test method one");
}
Output:
@Test
public void m2() Test method two
{ Test method one
S.o.p("Test method two");
FAILED: m3
}
@Test SKIPPED: m4
public void m3()
{
Assert.fail();
S.o.p("Test method three");
}
@Test(dependsOnMethods = {"m3", "m1"})
public void m4()
{
S.o.p("Test method four");
}
7. alwaysRun
• This attribute takes a value as true or false.
• If we set true, this method will always execute even its depending
method fails.
• Syntax: @Test(alwaysRun = true)
Example of alwaysRun
public class AlwaysRunEx
{
@Test
public void m1()
{
Output:
System.out.println("m1 method");
} m1 method
@Test(alwaysRun = false) m2 method
public void m2()
{ m3 method
System.out.println("m2 method");
}
@Test(alwaysRun = true)
public void m3()
{
System.out.println("m3 method");
}
}
Another Example of alwaysRun
@Test
public void m1()
{
S.o.p("Test method one");
Assert.fail();
Output:
}
Test method one
@Test(dependsOnMethods = {"m1"}) Test method three
public void m2() PASSED: m3
{
S.o.p("Test method two"); FAILED: m1
} SKIPPED: m2
@Test(dependsOnMethods = {"m1"},
alwaysRun = true)
public void m3()
{
S.o.p("Test method three");
}
8. description
• This attribute is used to define the description of the method.
• Syntax: @Test(description = "test method")
9. invocationCount
• This attribute is used to execute a method the number of times. It
acts as a loop.
• Syntax: @Test(invocationCount = count)
Example of invocationCount
public class InvocationCountEx
{
@Test(invocationCount = 3)
public void m1()
{
Output:
System.out.println("m1 method");
} m1 method
@Test(invocationCount = 0) m2 method
public void m2()
{ m3 method
System.out.println("m2 method");
}
@Test(invocationCount = -1)
public void m3()
{
System.out.println("m3 method");
}
}
10. invocationTimeOut
• The attribute timeOut is used for a time out test.
• It specifies a time period (in milliseconds) to wait for a test for
complete execution.
• Syntax: @Test(invocationTimeOut = time_in_ms)
11. priority
• This attribute sets the priority at the test method level.
• The default value is zero for priority.
• Lower priorities will be scheduled first for execution.
• Syntax: @Test(priority = value)
Example of priority
@Test(priority = 1)
public void firstTest()
{
S.o.p("First test method");
}
Output:
@Test(priority = -1) Fourth test method
public void secondTest()
{ Second test method
S.o.p("Second test method"); Third test method
}
First test method
@Test(priority = 0)
public void thirdTest()
{
S.o.p("Third test method");
}
@Test(priority = -2)
public void fourthTest()
{
S.o.p("Fourth test method");
}
12. timeOut
• This attribute is used to specify the time out of a test.
• It specifies a time period (in milliseconds) to wait for a test for
complete execution.
• Syntax: @timeOut = time_in_ms)
Example of timeOut
@Test(timeOut=200)
public void m1() throws InterruptedException
{
Thread.sleep(500);
S.o.p("This is testcase1");
Output:
}
This is testcase2
@Test This is testcase3
public void m2()
{ PASSED: testcase3
S.o.p("This is testcase2"); PASSED: testcase2
}
FAILED: testcase1
@Test(timeOut = 200)
public void m3() throws InterruptedException
{
Thread.sleep(100);
S.o.p("This is testcase3");
}
13. threadPoolSize
• This attribute is used to specify the size of the thread pool for this
method.
• The method will be invoked from multiple threads as specified by
invocationCount.
• Note: This attribute is ignored if invocationCount is not specified.
• Syntax: @Test(invocationCount = X, threadPoolSize = Y)
Example of threadPoolSize
@Test(invocationCount = 3, threadPoolSize = 3)
public void testThreadPools()
{
S.o.p("Thread Id : %s%n", Thread.currentThread().getId());
}
Output:
Thread Id : 18
Thread Id : 17
Thread Id : 16
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
Another Example of threadPoolSize
@Test(invocationCount = 5, threadPoolSize = 3)
public void testThreadPools()
{
S.o.p("Thread Id : %s%n", Thread.currentThread().getId());
}
Output: Output:
Thread Id : 18 PASSED: testThreadPools
Thread Id : 16 PASSED: testThreadPools
Thread Id : 17 PASSED: testThreadPools
Thread Id : 17 PASSED: testThreadPools
Thread Id : 16 PASSED: testThreadPools
Assertions in TestNG
What is Assertion?
• Assertions in TestNG are a way to verify that the expected result
and the actual result match in the test case or not.
• A test is considered successful ONLY if it is completed without
throwing any exceptions.
• There are two types of Asserts provided in the TestNG:
1. Hard Assert
2. Soft Assert
Hard Assert
• Hard Asserts are the type of assets that stop the execution of the
program when the condition becomes fail and mark the Test as
failed.
• It is used when the validation of one element is necessary to
perform any other upcoming actions.
• It is the default type of Assert provided by TestNG.
• Different Asserts statements:
1. AssertEquals(actual, expected)
2. AssertNotEquals(actual, expected)
3. AssertTrue(condition)
4. AssertFalse(condition)
Soft Assert
• Soft asserts are the asserts which continue the execution even after
the Assert condition fails.
• It is used in a case when the passing of one test condition is not
necessary to execute the upcoming tests.
• Soft assert is not included by default in TestNG. We need to create
the instance for the soft assert, and then import the package:
import org.testng.asserts.SoftAssert;
SoftAssert sobj = new SoftAssert();
Example
import org.testng.Assert;
import org.testng.asserts.SoftAssert;
@Test
public void HardAssertTest()
{
int a, b, sum, diff;
a = 3; b = 2; Output:
sum = a+b;
Assert.assertEquals(sum, 4); FAILED: HardAssertTest
diff = a-b; PASSED: SoftAssertTest
Assert.assertEquals(diff, 1);
}
@Test
public void SoftAssertTest()
{
int a, b, sum, diff;
a = 3; b = 2;
SoftAssert sa = new SoftAssert();
sum = a+b;
sa.assertEquals(sum, 4);
diff = a-b;
sa.assertEquals(diff, 1);
}