Friday, 15 August 2014

Configure testng.xml In Eclipse For Create single or multiuple class run under single or multiple test.



We can create any number of class may run under in any number of test in same execution by using testng.xml file.


For Example:


1. BaseClassOne.java
package TestNGOnePack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseClassOne {

    public static WebDriver driver = new FirefoxDriver();

    //@BeforeSuite annotation describes this method has to run before all suites
    @BeforeSuite
    public void setup() throws Exception {
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
    }

    //@AfterSuite annotation describes this method has to run after execution of all suites
    @AfterSuite
         public void tearDown() throws Exception {
         driver.quit();
    }
}
Above class will be used as base class to initialize and close webdriver instance.


2. ClassOne.java
package TestNGOnePack;

import org.testng.annotations.Test;

public class ClassOne extends TestNGOnePack.BaseClassOne{

 //@Test annotation describes this method as a test method
 @Test
  public void testmethodone() {
    String title = driver.getTitle();
    System.out.print("\nCurrent page title is : "+title);
    String Workdir = System.getProperty("user.dir");
    String Classpackname = this.getClass().getName();
    System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully");
  }
}

Above ClassOne is inherited from BaseClassOne.



3. ClassTwo.java
package TestNGOnePack;

import org.testng.annotations.Test;

public class ClassTwo extends TestNGOnePack.BaseClassOne{

 //@Test annotation describes this method as a test method
 @Test
  public void testmethodone() {
  driver.navigate().to("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
  String title = driver.getTitle();
  System.out.print("\nCurrent page title is : "+title);
  String Workdir = System.getProperty("user.dir");
  String Classpackname = this.getClass().getName();
  System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully");
  }
}




We can create above two class as Classone.java and ClassTwo.java. Then we create testng.xml file as below for run under same test or multiple test.


Configure testng.xml to run two classes in one test

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" />
   <class name="TestNGOnePack.ClassTwo" />
  </classes>
 </test>
</suite>


When test execuion get complete we can see the both class are run under same test.



Configure testng.xml to run two classes in two tests

.
Now if you wants to run both classes as separate test then you have to configure your testng.xml file as bellow.



<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" /> 
  </classes>
 </test>
 <test name="Test Two" >
  <classes>
   <class name="TestNGOnePack.ClassTwo" />
  </classes>
 </test>
</suite>

Configure testng.xml In Eclipse For Creating And Running WebDriver Test Suit Using testng.xml File



Usage Of testng.xml File

In TestNG framework, We need to create testng.xml file for below purpose

-> To create and handle multiple test classes.

-> we can configure our web test as per our needs in testng.xml file.

-> we can set dependency

-> include or exclude any method or any classes or any package.

-> Set Priority



Creating TestNG.xml file



1) For creating testNG.xml file, we need to right click on project -> New-> file -> open new file wizard

2) In New File wizard select project and give file name as "TestNG.xml" and then click Finish button.

3) It will add TestNG.xml file under our current project.

4) Then add below lines in TestNG.xml file (Select Source tab for type below code in Eclipse)


<suite name="TestNG_Suite">
<test name="TestNG_Test">
<classes>
<class name="TestNG_Package.TestNG_Test1"></class>
</classes>
</test>
</suite>



Here,

-> <suite> :

 suite tag defines the TestNG suite. You can give any name to your suite using 'name' attribute. In above given example, We have given "Suite One" to our test suite.


-> <test> :

test tag defines the TestNG test. You can give any name to your test using 'name' attribute. In above given example, We have given "Test One" to our test.


-> <classes> :

classes tag defines multiple class. We can multiple test class under classes tag. In our example we have only one class.


-> <class> :

class tag defines the class name which you wants to consider in test execution.


Example :

In above given example, we have defined name="TestNG_Package.TestNG_Test1"

Here 'TestNG_Package' describes package name
and 'TestNG_Test1' describes class name.

So in above example we are going to run only one class.



5) Running TestNG.xml file


Right click on testng.xml file -> Run As -> Select TestNG Suite

It will start execution of Test_Test1 class.



6)  View test execution HTML report generated by TestNG


- Right click on Project named as "TestNG_Project and select Refresh.

- then Folder with name = "test-output" will be created under your project folder.

- Explorer that "test-output" folder. then we can see the "index.html" file.

- Right click on "index.html" file then select open with -> Web browser - To see the HTML report in Eclipse.

Thursday, 14 August 2014

Steps for Create and Run TestNG testcase in Eclipse



Let me describe you steps of writing your first test case with TestNG.


1) Create New Project and New package for TestNG

- File -> New -> Java Project -> give new project name as "TestNG_Project"

- Richt click on "src" inside project name as "TestNG_Project" -> New -> Package -> give name as TestNG_Package



2) Add TestNG library


- Goto your project's Properties -> Java Build Path -> Libraries Tab.

- Click on Add Library button -> Select TestNG from Add Library popup and then click on Next and Finish buttons.

- And then verify it will add TestNg library in your project under library tab. Now click on OK button to close that window.



3) Create TestNG Class

- TO create TestNG class is different steps compare to create normal class in java project. Follow below steps to create TestNG class

- Right click on package as TestNG_package and click -> new -> other - it will open new wizard window

- Select TestNG from new wizard window and click on next button.

- on next screen add class name as "TestNG_Test1" and click finish.

- It will as new class as "TestNG_class1" under TestNG_Project with below coding.


package TestNG_Package;


public class TestNG_Test1 {

@Test
public void f(){
}
}




4) Add/write sample webdriver test script.

Write your own test script or add bellow given script in TestNG class file.




package TestNG_Package;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNG_Test1 {
WebDriver driver1=new FirefoxDriver();
@BeforeMethod
public void OpenBrowser(){
driver1.get("http://google.com");
driver1.manage().window().maximize();
}
@AfterMethod
public void CloseBrowser(){
driver1.quit();
}
@Test
public void f() {
String title = driver1.getTitle();
System.out.println("title"+title);   
}
}





5)  Run Webdriver test script with TestNG


- Go to Run -> Run As -> And Click on TestNg Test

- It will run your webdriver test with TestNG.

- we can the result after complete the execution.



6)  View test execution HTML report generated by TestNG


- Right click on Project named as "TestNG_Project and select Refresh.

- then Folder with name = "test-output" will be created under your project folder.

- Explorer that "test-output" folder. then we can see the "index.html" file.

- Right click on "index.html" file then select open with -> Web browser - To see the HTML report in Eclipse.


(Also the testng-customsuite.xml file will create automatically and configure the web driver test. Path = C:\Users\arunraj\AppData\Local\Temp\testng-eclipse--360075510\testng-customsuite.xml)

Steps for Downloading And Installing Testng In Eclipse For WebDriver



-> TestNG is very powerful and easy to use framework and it is supported by tools like Eclipse, Maven, IDEA, etc..

-> Here we are going to use TestNG Framework with Eclipse so obviously you must have installed latest Java development kit (JDK) in your system and you must have Eclipse.




TestNG Installation Steps In Eclipse


Refer this link -> http://www.softwaretestingmentor.com/selenium-tutorials/how-to-setup-testng-with-selenium-webdriver-in-eclipse-ide/


1) Open Eclipse and go to Menu Help -> Install New Software. It will open new software installation window.

2) In new software installation window, Type http://beust.com/eclipse exactly in Work with field and click on Add button.

For Eclipse 3.4 and above, enter http://beust.com/eclipse.
For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

3) Type name as "TestNG" and select "ok"

4) TestNG should get displayed under the name section, select the TestNG check box, click next and finish installation. ( Note: If TestNG already installed, then Next button is disabled.).

5) Restart eclipse to finalize installation. After restarting eclipse TestNG will get configured automatically in eclipse IDE.






Error facing while install TestNG with eclipse.


We may face after step(3) - Type name as "TestNG" and select "ok".  like below


" Problem occurred

"installing Software" has encountered a problem.

An error occurred while collecting items to be installed "


So we need to restart Eclipse and then follow the steps in above TestNG Installation Steps In Eclipse.





How to verify TestNG installed successfully in Eclipse


-> Once you have installed the plug-in, restart Eclipse

-> select the menu Window -> Show View -> Other

-> Select Java category, then we can see the TestNG is listed.

-> And Click the Testng and observe the below scenario.

1) If TestNG tab will open in console without any error then TestNG installed Successfully.
2) If TestNG tab will open in console with any error then TestNG not installed Successfully.
3) If TestNG is not listed under Java category then the TestNG is not installed in our eclipse.

Similarities and Difference Between Junit and TestNG Framework For WebDriver




Similarities Between JUnit and TestNG


1) We can create test suite in JUnit and TestNG both frameworks.

2) Timeout Test Is possible very easily in both the frameworks.

3) We can ignore specific test case execution from suite in both the frameworks.

4) It is possible to create expected exception test in both the frameworks.

5) Annotations - Few annotations are similar in both frameworks suite like @Test, @BeforeClass, @AfterClass. JUnit's Annotations @Before and @After are similar to TestNG's @BeforeMethod and @AfterMethod annotations.




Difference Between JUnit and TestNG


1) In TestNG, Parameterized test configuration is very easy while It is very hard to configure Parameterized test in JUnit.

2) TestNG support group test but it is not supported in JUnit.

3) TestNG has a feature to configure dependency test. Dependency test configuration is not possible in JUnit.

4) TestNG support @BeforeTest, @AfterTest, @BeforeSuite, @AfterSuite, @BeforeGroups, @AfterGroups which are not supported in JUnit.

5) Test prioritization, Parallel testing is possible in TestNG. It is not supported by JUnit.

Introduction Of TestNG




-> TestNG is unit testing framework. and its most popular among all java developers and selenium webdriver automation test engineers.

-> TestNG's development is inspired from combination of NUnit and JUnit. Its have all features from JUnit and also its have more features comapare to JUnit.



TestNG Annotations

-> The main reason behind TestNG's popularity is we can create and configure test case and test suite very easily using many different annotations of TestNG.

-> Few annotations are similiar with JUnit. Also its have more annotations.

-> Annotations in TestNG are which guides you what to do next or which method should be exeute next.

-> TestNG has also facility to pass parameters with annotations.


1) @Test
2) @BeforeMethod
3) @AfterMethod
4) @BeforeClass
5) @AfterClass
6) @BeforeTest
7) @AfterTest
8) @BeforeSuite
9) @AfterSuite
10) @DataProvider
11) @BeforeGroups
12) @AfterGroups
13) @Parameters
14) @Factory
15) @Listeners



1) @Test

-> Generally method under @Test used to write all testing related activity.
-> We can have any number of @Test method in single class of our test.



2) @BeforeMethod

-> Any Method which its marked as BeforeMethod will execute before each and every @Test Method Annotations.
-> Generally method under @BeforeMethod used to initializing browser or environment related setup.
-> The method under @BeforeMethod annotation will executed before each @Test annotations. That means if we have four @Test annotaions in your class, then the @BeforeMethod will execute four times.
-> Its same as @Before annoation in JUnit.



3) @AfterMethod

-> Any Method which its marked as BeforeMethod will execute after each and every @Test Method Annotations.
-> Generally method under @BeforeMethod used to close the instances.
-> The method under @AfterMethod annotation will executed after each @Test annotations. That means if we have four @Test annotaions in your class, then the @AfterMethod will execute four times.
-> Its same as @After annoation in JUnit.



4) @BeforeClass

-> Methods under @BeforeClass will execute before any one of the first test method starts execution.
-> It will execute only once even if there are multiple @Test method in our class.



5) @AfterClass

-> The method under @AfterClass will execute all the completion of @Test methods exection in class.
-> It will execute only once even if there are multiple @Test method in our class.



6) @BeforeTest

-> @BeforeTest annotated method will be executed before the any @Test annotated method of those classes which are inside <test> tag in testng.xml file.



7) @AfterTest

-> @AfterTest annotated method will be executed when all @Test annotated methods completes its execution of those classes which are inside <test> tag in testng.xml file.



8) @BeforeSuite

-> Method marked with @BeforeSuite annotation will run before the all suites from test.



9) @AfterSuite

-> @AfterSuite annotated method will start running when execution of all tests executed from current test suite.



10) @DataProvider

When you use @DataProvider annotation for any method that means you are using that method as a data supplier. Configuration of @DataProvider annotated method must be like it always return Object[][] which we can use in @Test annotated method.
VIEW PRACTICAL EXAMPLE OF @DataProvider ANNOTATION


11) @BeforeGroups

@BeforeGroups annotated method will run before the first test run of that specific group.


12) @AfterGroups

@AfterGroups annotated method will run after all test methods of that group completes its execution.


13) @Parameters

When you wants to pass parameters in your test methods, you need to use @Parameters annotation.
VIEW PRACTICAL EXAMPLE OF @Parameters ANNOTATION


14) @Factory

When you wants to execute specific group of test cases with different values, you need to use @Factory annotation. An array of class objects is returned by @Factory annotated method and those TestNG will those objects as test classes.

 
15) @Listeners

@Listeners are used to with test class. It is helpful for logging purpose.



Major Features


1) TestNG has a built in multiple Before and After and other annotations like @BeforeSuite, @AfterSuite, @BeforeTest, @BeforeGroups, @DataProvider, @Parameters, @Test etc.. All the TestNG annotations are very easy to understand and implement.

2) we can configure dependent test method. That means Test1() is dependent to Test2(). or else if test we cn configure if test1() is fail means Test1() has to be execute or not.

3) There is not required to extend any class in TestNG

4) We can configure our test suite using test methods, classes, packages, groups, etc.. in single testng.xml file.

5) Support of configuring test groups like backendtest-group, frontendtest-group etc..

6) we can also tell TestNG to execute only specific group/groups.

7) TestNG is supported by many tools and plug-ins like Eclipse, Maven, IDEA, etc..

8) TestNG has built in HTML report and XML report generation facility. It has also built in  logging facility.

9) Support for data-driven testing (with @DataProvider).

10) Flexible test configuration.

11) Ability to re-execute failed test cases.

12) TestNG also supports
-> parallel testing
-> Parameterization (Passing parameters values through XML configuration)
-> Data Driven Testing(executing same test method multiple times using different data)



How to use For Each loop in Java



-> It was introduced in JDK 1.5

-> Its mainly used to traverse the elements from array or collections.

-> The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.


Syntex:

for(data_type variable : array | collection)
{

}


Example :

class ForEachExample1{ 
  public static void main(String args[]){ 
   int arr[]={12,13,14,44}; 
 
   for(int i:arr){ 
     System.out.println(i); 
   } 
 
 }  

     

Output:12
       13
       14
       44