Showing posts with label TestNg. Show all posts
Showing posts with label TestNg. Show all posts

Sunday, February 14, 2016

Selenium : Simple Error Reporting in TestNG without Halt (Soft assertion)

 Simple Error Reporting in TestNG without Halt (Soft assertion)

 Following Class in TestNG offers reporting :
1. Soft Assertion : Execution will not skip the rest of the steps in  the method.
 SoftAssert soft_assert=new SoftAssert();  
 soft_assert.assertEquals("abc", "ABC");
 soft_assert.assertAll(); //should be added in all @Test Methods
2. Hard Assertion :Execution will skip rest of the steps in the method and move to the next method
Assert.assertEquals("abc", "ABC");

3. To log the information from the script to the HTML report we must use the class org.testng.Reporter
Now to print the data to the report we must use :
Reporter.log("PASS/FAIL");
-------------------------------------------------------------------------------------

 Project Tree



// Assert1.java

import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assert1 {
    SoftAssert soft_assert=new SoftAssert();
       
    @Test
    public void test1(){               
        Assert.assertEquals("abc", "ABC");
        System.out.println("hi");       
    }
   
    @Test
    public void test2(){               
        soft_assert.assertEquals("abc", "ABC");
        System.out.println("bye");
        soft_assert.assertAll();       
    }
}

//testng.xml

<suite name="TestNg_test">       
       <test name="assert1">>        
              <classes>
                     <class name="Assert1" />                     
              </classes>
       </test>       
</suite>

Wednesday, February 27, 2013

Selenium : Batch Execution in TestNG

Batch Execution in TestNG

I will be discussing on a Simple partial framework in using Eclipse Helios.

Pre-Requistes:
  1. You should have Eclipse Helios.
  2. TestNg plug-in should be installed to Eclipse.

Step 1. Framework skeleton

  1. Create a project with any name inside eclipse.
  2. Inside that project create a package by name "package_name" .
  3. Inside "package_name" create 3 Java files (Class Files) of the following names :- a) Initial  b) Sample  c) Sample2

Step 2 .Create "testng.xml"

Here is a code that you use :- 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Can Be any name" verbose="1" parallel="false" >

<test name="Test name is Some Name">
<classes>
<class name="package_name.Initial" />
</classes>
</test>


<test name="Test name is Some Other Name">
<classes>
<class name="package_name.Sample" />
</classes>
</test>


<test name="Test name is Another Name">
<classes>
<class name="package_name.Sample2" />
</classes>
</test>

</suite>

  1.  Paste this code in text file.
  2. Save as "testng.xml".
  3. Drag and Drop this file in the project you created in Eclipse.

 

Step 3   Code in a) Initial  b) Sample  c) Sample2

Copy paste the following codes in each of the files .

----------------------------Initial.Jar ----------------------------------------
package package_name;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class Initial
{

@BeforeSuite//Can only be used 1 Time in the Suite ,executes the very first
public void Initialization()
{
System.out.println("-----------@BEFORESUITE---------------\n");
}

@AfterSuite//Can only be used 1 Time in the Suite ,executes the very end
public void Destroy()
{
}
}


---------------------------------------Sample.jar---------------------------------------------------
package package_name;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Sample
{
@BeforeTest //BEFORE TEST
public void SampleBeforeTest()//Executed only 1 at the beginning of the file
{
System.out.println(" BeforeTest--Sample ");
}
@BeforeMethod //BEFORE METHOD
public void SampleBeforeMethod()//, Executed each time before test
{
System.out.println("BeforeMethod--Sample ");
}

@Test (dataProvider = "registerData")//Parameterized data
public void SampleTest1A(String Some_Data)
{
System.out.println(Some_Data);

}
@DataProvider //Parameterization
public Object[][] registerData()
{
Object[][] Data=new Object[2][1];// execute 2 times , 1 data each time
//Rows = No of times test should be repeated
//Columns = No of data
Data[0][0]="SampleTest1A--Sample , Parameterized !!";
Data[1][0]="SampleTest1B--Sample , Parameterized !!";
return Data;
}

@AfterTest //AFTER TEST
public void SampleAfterTest()//Executed only once in this file after this file is completed
{
System.out.println(" AfterTest--Sample \n");

}
@AfterMethod //AFTER METHOD
public void SampleAfterMethod()//, Executed each time after every test is executed
{
System.out.println("AfterMethod--Sample \n");
}
}

---------------------------------------------Sample2.jar--------------------------------------------

package package_name;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Sample2
{
@BeforeTest //@BeforeTest runs only 1 time before test
public void SampleBeforeTest2()
{
System.out.println(" BeforeTest--Sample2\n");
//throw new SkipExecution("Skipping test");
}
@BeforeMethod //@BeforeMethod runs each time before @test
public void SampleBeforeMethod2()
{
System.out.println("BeforeMethod--Sample2");
}
@Test // @Test runs only once
public void SampleTest2A()
{
System.out.println("SampleTest2A--Sample2 ");
}
@Test //@Test runs only once
public void SampleTest2B()
{
System.out.println("SampleTest2B--Sample2");
throw new SkipException("Skipping test -- SampleTest2B");
}
@AfterTest //@AfterTest runs only 1 time at the end .
public void SampleAfterTest2()
{
System.out.println(" AfterTest--Sample2\n");
System.out.println(" ---Over of Sample2--- \n");
}
@AfterMethod //@AfterMethod runs each time After @test
public void SampleAfterMethod2()
{
System.out.println("AfterMethod--Sample2 \n");
}
}


Step 4 Run the Skeleton.

  1. Right click on the testng.xml file and  Runas>TestNG test.
  2. Press f5 (Refresh) or Right click on Package Explorer>Refresh
  3. You should be able to see "test-output" folder
  4. " test-output>index.html " gives result in html format.

Integrating TestNG with Eclipse

Integrating TestNG with Eclipse


  1. You should have installed Eclipse .
  2. Goto help>Install New Software
  3. Open Browser
  4. Goto Google >type install testNG  or Goto http://testng.org/doc/download.html
  5. Copy the link corresponding to Eclipse Version .This should look something like below .
Example
For Eclipse 3.4 and above, enter http://beust.com/eclipse.
For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

6.  Paste the same in "Work With" edit field on opened "Avaliable Software"window .Press Add button.
7. Check the Check box TestNg
8. Click next....next blah!blah! ...
9.  Restart Eclipse and TestNg will be installed

---------------------------------------------------

Running TestNG


1.      Eclipse > New>Project>Java Project >Enter name ...  blah,blah ..(how you create your normal Java projects)
2.      Project created
3.      Go to project > src > Delete your default package
4.      Create a new package in src (Ex : tester)
5.      Create a new class (Ex:"starter") inside "tester" package
6.      Add below code inside tester.java
     package starter;
public class starter {



@Test

public void testing()

{

System.out.print("hi");

}



}
//Note: All Classes and Methods has to be of "Public" access identifier.

7.      Add all dependent libraries
8.      RtClick on src folder
9.      New>Other>XML>XML File
10.  Create file “Testng.xml”
11.  Open Testng.xml, Below screen click on “Source”
12.  Add following Code:

<suite name="some name">

<test name = "other name">



<parameter name ="var" value="hi"/>



<classes>

<class name = "tester.starter" />

</classes>



</test>

</suite>  

Now Right Click on TestNg.xml file and Runas "TestNG Suite"

Class File





XML File





Examples :
If your class is just inside a default pacjake you can just write following in Testng.xml:
<suite name="TestNg_test">       
       <test name="assert1">>        
              <classes>
                     <class name="your_class" />                     
              </classes>
       </test>       
</suite>





Following are the links which give you know how on integration of Selenium with TestNG:-


http://qtpselenium.com/selenium-tutorial
http://selftechy.com/2012/01/09/setting-up-testng-with-eclipse