Selenium TestNG Examples

Selenium TestNG Examples

I) Executing multiple classes using XML file 

@BeforeMethod - executes before every test case in a class
@AfterMethod -executes after every test case in class
---------------
@BeforeClass -executes before all test cases in a class
@AfterClass - executes after all test cases in a class
--------------------------------
@BeforeTest - executes before all test cases from all classes in XML file
@AfterTest -executes after all test cases from all classes in XML file
------------------------------------------------
XML file for executing multiple classes:
<suite name="Suite" >
  <test name="Test">
    <classes>
      <class name="seleniumTests.Abcd1"/>
      <class name="seleniumTests.Abcd2"/>
     
    </classes>
  </test>
</suite>
-----------------------
Class File 1
public class Abcd1 {
  @BeforeTest
  public void Login(){
  System.out.println("Login Successful");
  }
  @AfterTest
  public void Logout(){
  System.out.println("Logout Successful");
  }
  @Test (priority = 1)
  public void FundTransfer (){
    System.out.println("Fund Transfer Successful");
  }
  @Test (priority=3)
  public void PrepaidRecharge(){
  System.out.println("Prepaid Recharge Successful");
  }
  @Test (priority=2)
  public void BillPayments(){
  System.out.println("Bill Payments Successful");
  }
}
-----------------------------------------------
Class File 2
public class Abcd2 {
    /*@BeforeMethod
    public void Login() {
        System.out.println("Login Successful");
    }
    @AfterMethod
    public void Logout() {
        System.out.println("Logout Successful");
    }*/
    @Test(priority=1)
    public void Search() {
        System.out.println("Search Successful");
    }
    @Test(priority=2)
    public void AdvancedSearch() {
        System.out.println("Advanced Search Successful");
    }
}
---------------------------------------------------------------
II) Grouping Test Cases
 

XML file

<suite name="Suite">
 <test name="Test">
 <groups>
 <run>
 <include name ="regression"/>
 </run>
 </groups>
    <classes>
      <class name="seleniumTests.GroupingTests"/>
    </classes>
  </test>
</suite>
----------------------------
Class File
public class GroupingTests {
    @BeforeGroups (groups ="regression")
      public void Login(){
      System.out.println("Login Successful");
      }
      @AfterGroups (groups ="regression")
      public void Logout(){
      System.out.println("Logout Successful");
      }
      @Test (groups ={"sanity"})
      public void FundTransfer (){
        System.out.println("Fund Transfer Successful");
      }
      @Test (groups ={"sanity", "regression"})
      public void Search (){
            System.out.println("Search Successful");
          }
      @Test (groups ={"regression"})
      public void PrepaidRecharge(){
      System.out.println("Prepaid Recharge Successful");
      }
      @Test (groups ={"regression"})
      public void BillPayments(){
      System.out.println("Bill Payments Successful");
      }
    }
-----------------------------------------------
III) Data driven Testing using DataProvider Annotation

 public class DataDriven {
    @Test (dataProvider ="testdata")
    public void Addition(String val1, String val2, String val3){
        int a = Integer.parseInt(val1);
        int b = Integer.parseInt(val2);
        int c = Integer.parseInt(val3);
        int result = a + b + c;
        System.out.println(result);
    }
       
@DataProvider(name="testdata")   
public Object [] [] readExcel() throws BiffException, IOException{
    File f = new File("C:/Users/gcreddy/Desktop/Input.xls");
    Workbook w = Workbook.getWorkbook(f);
    Sheet s = w.getSheet(0);
    int rows = s.getRows();
    int columns = s.getColumns();
    //System.out.println(rows);
    // System.out.println(columns);
    String InputData [] [] = new String [rows] [columns];
    for (int i = 0; i<rows; i++){
        for (int j=0; j<columns; j++){
            Cell c = s.getCell(j, i);
            InputData [i] [j] = c.getContents();
            //System.out.println(InputData [i][j]);
        }
    }
return InputData;
}
}
-----------------------------------------------------
IV) Parallel Test execution
Parallel test execution using TestNG (using single machine/computer)

Parallel test execution Using Selenium Grid (using multiple computers)
----------------------------------------
Take tests and execute using different browsers (IE, Firefox, and Chrome)
---------------------------------------------
Steps:

Before Class - Launch Browsers (Firefox, Internet Explorer and Chrome)

Execute Tests on all Browsers

After Class - Close all browsers
--------------------------------
Class File
 

public class ParallelTests {
    public WebDriver driver;
   
    @Parameters("browser")
    @BeforeClass
       
    public void LunchBrowser(String browser) {

    if(browser.equalsIgnoreCase("FF")) {
    driver = new FirefoxDriver();
    }

    else if (browser.equalsIgnoreCase("IE")) {
    System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
    driver = new InternetExplorerDriver();
    }

    else if (browser.equalsIgnoreCase("CH")) {
    System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
    driver = new ChromeDriver();
    }
    driver.get("http://www.google.com");
    }
    @Test
    public void VerifyPageTitle() throws InterruptedException {
    Assert.assertEquals(driver.getTitle(), "Google");
    }
    @AfterClass
    public void CloseBrowsers() {
    driver.quit();
    }
    }
-------------------------------
XML file
<suite name="Suite" parallel="tests" thread-count ="3">
  <test name="FireFoxTests">
  <parameter name = "browser" value = "FF"/>
     <classes>
      <class name="seleniumTests.ParallelTests"/>
    </classes>
  </test>
 
  <test name="IETests">
  <parameter name = "browser" value = "CH"/>
      <classes>
      <class name="seleniumTests.ParallelTests"/>
          </classes>
  </test>
 
  <test name="ChromeTests">
  <parameter name = "browser" value = "IE"/>
     <classes>
     <class name="seleniumTests.ParallelTests"/>
    </classes>
  </test>
</suite>
----------------------------------------------------

0 comments:

Post a Comment