TestNG Examples

Example 1:

package com.testng.examples;

import org.testng.annotations.Test;

public class TestNGTest {
 @Test(priority = 1,groups="smoke")//(enabled=false)
 public void test1(){
  System.out.println("Test 1");
 }

 @Test(priority=2, groups="regression")
 public void test2(){
  System.out.println("Test 2");
 }
 
 @Test(priority=3,groups="regression")
 public void Test3(){
  System.out.println("Test 3");
 }
}

Example 2:

package com.testng.examples;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

public class TestNGBeforeAfterMethod {
  @Test(alwaysRun = true, priority=1, groups="smoke")
  public void facebookLogin() {
   System.out.println("Facebook Login Test is executing");
  }
  @BeforeMethod
  public void beforeMethod() {
   System.out.println("Before test method this method will execute");
  }

  @AfterMethod
  public void afterMethod() {
   System.out.println("After test method this method will execute");
  }
  
  @Test(priority=0, groups="regression")
  public void googleSearch(){
   System.out.println("Google Search Test is executing");
  }
}

Example 3:

package com.testng.examples;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class TestNGAfterBeforeTest {
 @Test(groups="smoke")
   public void facebookLogin() {
    System.out.println("Facebook Login Test is executing");
   }
   @BeforeTest
   public void beforeMethod() {
    System.out.println("Before all test method this method will execute");
   }

   @AfterTest
   public void afterMethod() {
    System.out.println("After all test method this method will execute");
   }
   
   @Test(groups="regression")
   public void googleSearch(){
    System.out.println("Google Search Test is executing");
   }
}

Example 4:

package com.facebook.register;

import org.testng.annotations.Test;
import utility.Helper;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;

public class FacebookReg {
   WebDriver driver;
   @Test
   public void facebookRegistration() throws InterruptedException {
     driver.get("http://facebook.com");
      String password = "9756325625";
   String emailID="abcdefgh@gmail.com";
      driver.findElement(By.id("u_0_1")).sendKeys("abcd");
   
   //Enter last Name using name locator
   driver.findElement(By.name("lastname")).sendKeys("XYZ");
   
   //Enter Email ID or Mobile number using xapath locator
   driver.findElement(By.xpath("//input[@id='u_0_5']")).sendKeys(emailID);
   
   //Re-Enter Email ID or Mobile Number using cssSelector
   driver.findElement(By.cssSelector("#u_0_8")).sendKeys(emailID);
   
   //Enter Password using Xpath locator
   driver.findElement(By.xpath(".//input[@id='u_0_a']")).sendKeys(password);
   
   //selectByValue using Select Class
   Select day = new Select(driver.findElement(By.id("day")));
   day.selectByValue("10");
   
   //selectByVisibleText using Select Class
   Select month = new Select(driver.findElement(By.name("birthday_month")));
   month.selectByVisibleText("Apr");
   
   //selectByIndext using Select Class
   Select year = new Select(driver.findElement(By.cssSelector("select[id='year']")));
   year.selectByIndex(4);
   
   Thread.sleep(2000);
   //Click a radio buttom
   driver.findElement(By.id("u_0_d")).click();
   
   Thread.sleep(5000);
   
   driver.findElement(By.id("u_0_e")).click();
   
   Thread.sleep(6000);
   }
   @BeforeTest
   public void beforeTest() {
    driver = Helper.launchBrowser("chrome");
   }

   @AfterTest
   public void afterTest() {
    driver.quit();
   }
}

Testng1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My First Suite">
  <test name="Test NG Example">
    <classes>
      <class name="com.testng.examples.TestNGBeforeAfterMethod"/>
      <class name="com.testng.examples.TestNGAfterBeforeTest"/>
      <class name="com.testng.examples.TestNGTest"/>
    </classes>
  </test> <!-- Test NG Example -->
</suite> <!-- My First Suite Example -->


Testng2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Second Suite">
  <test name="Test">
    <packages>
      <package name="com.testng.examples"/>
    </packages>
  </test> <!-- Test -->
</suite> <!-- Suite -->


Testng3.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Suite Suite 3">
  <test name="Test Methods">
    <classes>
      <class name="com.testng.examples.TestNGTest">
        <methods>
         <include name=".+est.*"></include>
        </methods>
      </class>
    </classes>
  </test> <!-- Test Methods -->
</suite> <!-- My Suite Suite 3 -->

Testng4.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Suite 3">
  <test name="Test Using Groups">
  <groups>
    <run>
     <include name="regression"></include>
     <include name="smoke"></include>
    </run>
  </groups>
    <classes>
      <class name="com.testng.examples.TestNGBeforeAfterMethod"/>
      <class name="com.testng.examples.TestNGAfterBeforeTest"/>
      <class name="com.testng.examples.TestNGTest"/>
    </classes>
  </test> <!-- Test Using Groups -->
</suite> <!-- My Suite 3 -->

No comments:

Post a Comment