Android apps to use in appium android test examples

Breaking

Wednesday, July 11, 2018

TestNG Framework Quick Tutorial for Selenium

TestNG Framework Quick Tutorial for Selenium

i) Introduction to TestNG Framework

ii) Install TestNG together with write commencement TestNG Program

iii) Create multiple Test cases together with prioritize Test Cases.

iv) Execute multiple Classes / Programs using XML

v) Grouping Test Cases
--------------------------------------------
i) Introduction to TestNG Framework

> In Selenium using Java in that place are 2 Frameworks available,

1) JUnit

2) TestNG

> TestNG is a Testing Framework designed to simplify a wide hit of Testing activities from Unit Testing to System Testing.

> Initially developed for Unit Testing, forthwith used for all kinds of Testing

> TestNG is an opened upwardly root Framework where NG Stands for Next Generation.

> TestNG inspired from JUnit (Java platform together with NUnit (.NET platform), simply introduced unopen to novel functionality.
--------------------------------------------
Advantages of TestNG

> TestNG Annotations are slow to exercise Test Cases

> Test Cases tin last grouped together with prioritized to a greater extent than easily.

> Supports Parameterization

> Execute Test Batches

> Parallel Test Execution

> Generate HTML Test Reports.
--------------------------------------------
ii) Install TestNG together with write commencement TestNG Program

In Eclipse IDE,

> Help Menu
> Install New Software
> Click Add
> Enter parent equally "TestNG"
> Enter URL equally : "http://beust.com/eclipse/"
> Select "TestNG"
> Next > Next > conduct maintain License Agreement > finish
--------------------------------------------
Write commencement TestNG Test Case

Manual Test Case: Verify the Title of Gmail Home Page

Test Steps:
i) Launch the Browser
ii) Navigate to "https://www.gmail.com"

Verification Point:
Capture the Page Title together with Compare amongst expected.

Expected: Gmail

Input Data: None
--------------------------------------------
public class VerifyTitle {
@Test
public void verifyTitle(){
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.gmail.com");

String pageTitle = driver.getTitle();
Assert.assertEquals (pageTitle, "Gmail");
driver.close();
}
}
--------------------------------------------
Note:

1) nous method is non used for TestNG programs.
2) TestNG Program contains alone methods that comprise @Test Annotations
3) If nosotros don't write @Test Annotation together with thus the method is non going to last executed.
--------------------------------------------
iii) Create multiple Test cases together with prioritize Test Cases.

public class VerifyTitle {
@Test
public void logout(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test
public void abcd(){
Assert.assertEquals("abcd", "abcd");
}
@Test
public void xyz(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test
public void login(){
Assert.assertEquals("Google", "Google");
}
}
---------------------------------------
Test Execution Flow equally per the Program:

logout
abcd
xyz
login
--------------------------------
abcd
login
logout
xyz

Note: TestNG Test Cases are executed inwards Alphabetical Order.

If y'all desire command the Test Execution together with thus usage priority attribute or dependsOnMethods attribute
---------------------------------------
Required Flow:
login
abcd
xyz
logout

Using priority attribute

public class VerifyTitle {
@Test(priority = 4)
public void logout(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test(priority = 2)
public void abcd(){
Assert.assertEquals("abcd", "abcd");
}
@Test(priority = 3)
public void xyz(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test(priority = 1)
public void login(){
Assert.assertEquals("Google", "Google");
}
}
--------------------------------------------
Using dependsOnMethods attribute

import org.testng.Assert;
import org.testng.annotations.Test;

public class VerifyTitle {
@Test(dependsOnMethods = {"xyz"})
public void logout(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test(dependsOnMethods = {"login"})
public void abcd(){
Assert.assertEquals("abcd", "abcd");
}
@Test(dependsOnMethods = {"abcd"})
public void xyz(){
Assert.assertEquals("Yahoo", "Yahoo");
}
@Test
public void login(){
Assert.assertEquals("Google", "Google");
}
}
--------------------------------------------
login -Pre status for every Test case
logout -Post Condition for Evey Test Case

Required Flow:

login
abcd
logout

login
xyz
logout
--------------------------------------------
public class VerifyTitle {
@AfterMethod
public void logout(){
System.out.println("Logout Successful");
}
@Test
public void abcd(){
System.out.println("abcd Successful");
}
@Test
public void xyz(){
    System.out.println("xyz Successful");
}
@BeforeMethod
public void login(){
System.out.println("Login Successful");
}
}
-------------------------------------------
login -Pre status for all Test cases inwards the Program
logout -Post Condition for all Test cases inwards the Program

Required Flow:

login
xyz
abcd
logout
-----------------------------------------
public class VerifyTitle {
@AfterClass
public void logout(){
System.out.println("Logout Successful");
}
@Test(priority = 2)
public void abcd(){
System.out.println("abcd Successful");
}
@Test(priority = 1)
public void xyz(){
    System.out.println("xyz Successful");
}
@BeforeClass
public void login(){
System.out.println("Login Successful");
}
}
--------------------------------------------
iv) Execute multiple Classes / Programs using XML
XML file for executing multiple Classes/Programs.

<suite parent ="SuiteName">
<test parent = "TestName" >
<classes>
<class parent = "Package.Class1Name" />
<class parent = "Package.Class2Name" />
<class parent = "Package.Class3Name" />
</classes>
</test parent = "TestName" >
</suite parent ="Suite Name">

Tags

suite
test
classes
class
-------------------------------
Navigation to exercise XML file

Select Java Package > Right Click > New > Others...
> Enter "TestNG" together with Select TestNg Class
> Enter root together with Package names
> Enter XML file name
> Finish
--------------------------------------------
Test Cases:

login
addVendor
addCurrency
addProduct
logout
-------------------------------------
XML File

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Ecommerce" parallel="false">
  <test name="Smoke Tests">
    <classes>
      <class name="abcd.Class1"/>
      <class name="abcd.Class2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
---------------------------------------
Class 1

public class Class1 {
@BeforeTest
public void login() {
System.out.println("Login Successful");
}
@AfterTest
public void logout() {
System.out.println("Logout Successful");
}
@Test(priority = 1)
public void addVendor() {
System.out.println("Add Vendor Successful");
}
@Test(priority = 3)
public void addProduct() {
System.out.println("Add production Successful");
}
@Test(priority = 2)
public void addCurrency() {
System.out.println("Add Currency Successful");
}
}
------------------------------------------------
Class 2:

public class Class2 {
/*@BeforeClass
public void login() {
System.out.println("Login Successful");
}
@AfterClass
public void logout() {
System.out.println("Logout Successful");
}*/
@Test(priority = 1)
public void deleteVendor() {
System.out.println("Delete Vendor Successful");
}
@Test(priority = 3)
public void deleteProduct() {
System.out.println("Delete Product Successful");
}
@Test(priority = 2)
public void deleteCurrency() {
System.out.println("Delete Currency Successful");
}
}
--------------------------------------------
v) Grouping Test Cases

XML file for grouping Test Cases

<suite parent ="Suite Name">
<test parent ="Test Name">

<groups>
<run>
<include parent ="GroupName"/>
</run>
</groups>

<classes>
<class parent = "Package.Class1"/>
<class parent = "Package.Class1"/>
</classes>

</test parent ="Test Name">
</suite parent ="SuiteName">
--------------------------------------------
Tags

suite
test
groups
run
include
classes
class
--------------------------------------------
Sanity grouping

login
search
prepaidRecharge
logout

Regression group

login
advancedSearch
prepaidRecharge
billPayments
logout
-------------------------------------
XML File

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
 
  <groups>
  <run>
  <include parent ="Regression"/>
  </run>
 
  </groups>
    <classes>
      <class name="abcd.Class3"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
-------------------------------------------
Class File

public class Class3 {
@Test(groups = {"Sanity", "Regression"}, priority=1)
public void login() {
System.out.println("Login Successful"); 
}
@Test(groups = {"Sanity", "Regression"}, priority=10)
public void logout() {
System.out.println("Logout Successful"); 
}
@Test(groups = {"Sanity"}, priority=2)
public void search() {
System.out.println("Search Successful"); 
}
@Test(groups = {"Regression"}, priority=2)
public void advancedSearch() {
System.out.println("Advanced Search Successful"); 
}

@Test (groups = {"Sanity", "Regression"}, priority=3)
public void prepaidRecharge() {
System.out.println("Prepaid Recharge Successful"); 
}
@Test (groups = {"Regression"}, priority=4)
public void billPayments() {
System.out.println("Bill Payments Successful"); 
}
}
--------------------------------------------
Selenium Webdriver Test Cases
1) Launch Browser
2) Verify Gmail Home Page
3) Verify Yahoo Home Page
4) Close Browser
-----------------------------------
TestNG Program:

public class Sample {
public WebDriver driver;

@BeforeClass
public void launchBrowser(){
driver = novel FirefoxDriver();   
}
@AfterClass
public void closeBrowser(){
driver.close();
}
@Test(priority=1)
public void verifyGmailpage(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}
@Test(priority=2)
public void verifyYahoopage(){
driver.get("https://in.yahoo.com");
Assert.assertEquals("Yahoo", driver.getTitle());
}
}
--------------------------------------------

Sumber http://www.gcreddy.com/

No comments:

Post a Comment