Module driven framework with Testng and Jenkins

Step1. Configure Testng with eclipse ide

             In eclipse ide go to Help menu ---------- go to Eclipse market place ------------  enter testng for eclipse juno ------------ click go  ------------ click  install for testng for eclipse ------ Accept licese             agreement ------- click next until finish --------------- restart eclipse ide.

Step2. Create project associate required jars, set compiler and JRE versions and associate Testng to project.

            Right click on project ------- Go to Properties ---------------- java build path ------------- Libraries  --------- add library --------- TestNg ---------- Next -------- Finish.

 

step3. Developing classes with annotated methods.

          Right click on project -------- go to New ------ Package ---------- enter package name ----------- click finish.

           Right click on package  ------ Testng ------ create Testng class ----- Enter a name to class. --- finish

Provide automation code in corresponding class methods like shown bellow :

public class Test1    

    {

          public WebDriver driver;

        @Test(priority=0)

         public void launch() throws InterruptedException

          {

            // Launch site

              System.setProperty("webdriver.chrome.Driver", ""path of chrome driver exe");

               driver = new ChromeDriver();

               driver.get("http://www.gmail.com");   

              Thread.sleep(5000);

           }

      @Test(priority=1)

            public void close()

             {

                driver.close();

           } 

     }. 

Step4. Create suite file(.xml)

            Right click on package ------ Testng -------  Create testng class ------ Enter a name to suite file (.xml) -------- click finish ------ open xml file source code add class names like shown bellow

 

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

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

<classes>

   <class name = "mypack.Test1" />           

     <class name = "mypack.Test2" />    

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

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

Note1. Every class is having methods wwith @Test annotation.

@Test(priority=0) ---- corresponding method can run  first

@Test(enabled=false) ---- corresponding method cannot be run.

Note2.

Testng can provide different annotations along with @Test sucha as @BeforeTest, @AfterTest, @BeforeSuite, @AfterSuite, @BeforeMethod, @AfterMethod.

Note3: 

In general most of testers can use @Test annotation for methods in class instead of other annotations. But to schedule those methods in a class for execution, testers can use @Test annnotation with properties.

Example: 

                @Test(priority=0) ---- corresponding method can run  first

            @Test(enabled=false) ---- corresponding method cannot be run.

                 @Test(dependsOnMethods) = ("method1"} 

               Current method can run when "method1" was passed.

             @Test(dependsOnGroups) = ("method1"} 

               Current method can run when all methods in group "A' are passed.

 

Note4.

  Testng can provide built in assertions like in SOAPUI tool . Assertion can compare expected value and actual value instead of If -- else statement.

example:

@Test(priority=0) 

public void method1() 

{

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

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

Assert.assertEquals("xxxxxx", "xxxxxx"); // Here first xxxx is expected value, 2nd one is actual value.

}

 

Note 5. 

In Testng module driven framework, xml suite file can allow us to run corresponding Test classes methods parllelly.

example:

<?xml version = "1.0" encoding = "UTF-8" ?>

<suit name ="suite" parllel = "classes" thread-count = "2">

        <test name  = "Test">

           <classes>

            <class name = "mypack.Test1">

             <class name = "mypack.Test2">

             </classes>

    </test>

</suite>

Sep5:  Extend classes methods for data driven

To implement test classes for data driven we need to change suite file like shown bellow

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

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

<test name = "Login test with valid data"> 

<parameter name= "uid" value = "some name">

<parameter name= "pwd" value = "some value">

<classes>

       <class name = "mypack.Test1">

</classes>

</test>

<test name = "Login test with invalid data"> 

<parameter name= "uid" value = "some name">

<parameter name= "pwd" value = "some value">

<classes>

       <class name = "mypack.Test1">

</classes>

</test>

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

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

To usse xml data for data driven we need to update corresponding class methods like shown bellow.

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

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

@Test(priority=0) 

@parameters({"uid", "pwd'})

public void login(String x, String y) throws InterruptedException

{

 driver.findElement(By.name("Email")).sendKeys(x);

 driver.findElement(By.id("next")).click();

Thred.sleep(5000);

 driver.findElement(By.name("passwd")).sendKeys(y);

 driver.findElement(By.id("SignIn")).click();

Thred.sleep(5000);

Sep6.  Run test classes run from command prompt.

we can use bellow command to run test classes from command prompt

java -cp E:\projectfolder\jars\*; E:\projectfolder\gmailmd\bin

1                            5                                            4

org.testng.TESTNG  E:\projectfolder\gmailmd\bin\suite1.xml

2                                           3

 

Step7. Run from Jenkins

Go to google site ------------- Jenkins download ---------- jenkins.io site --------- click doownload jenkins ----------- click on stable version ------- paste that download in your personal folder ------ jenkins.war -------- Go to command prompt  ----------- run bellow cmd

java -jar E:\projectfolder\jenkins.war

Open localhost:8080 using browser.

New item ---- enter a name to project ----- select free style project ---------- click on OK  -------------- select build periodically ---------- build - ------------ Add step ------------- windows batch command ---------- path of batch file --------save