Saturday, November 5, 2016

How to run multiple test suites in TestNG?

Suppose we have individual test suite for different features/modules in TestNG, we can run multiple testNG test from a master suite xml file by calling the individual tests as shown below:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MainTestSuite">
    <suite-files>
        <suite-file path="suiteFeatureA.xml" />  
        <suite-file path="suiteFeatureB.xml" /> 
        <suite-file path="suiteFeatureC.xml" />  
        <suite-file path="suiteFeatureD.xml" />      
    </suite-files>
</suite>


This way, we can run multiple test suites from a master suite file in TestNG.

Thursday, November 3, 2016

Handling authentication popup in Selenium WebDriver

In case of login into an application in selenium, an alert pop may be displayed authenticating for user credential. The pop-up can be web based or windows based popup.

Web Based Authentication pop-up can be handled using alert class as shown below:

// provide a wait condition for alert to be present
WebDriverWait wait = new WebDriverWait(driver, 30);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//Using alert.authenticateUsing, provide username and password for http alert popup     
alert.authenticateUsing(new UserAndPassword( ** username ** , ** password ** ));

Or else, can try the below code and see it works:

driver.Navigate().GoToUrl("http://UserName:Password@testingmail.com");

This was about web-based pop-up, In case a windows pop-up appears asking for username and password, An autoIT script can be created to handle the windows pop-up.
  • Create an au3 file for handling the pop-up.
  • Compile the au3 file using AutoIT script  to exe converter.
autoit script converter

  • Run the autoIT exe file using below code in selenium.

Runtime.getRuntime().exec("d:\\AuthenticateAutoIt.exe");		

Tuesday, November 1, 2016

Creating TestNG.xml to run Selenium test suites.

TestNG is a testing framework used to design automated test suites for functional/unit tests in Java. Below are few reasons which makes TestNG, a strong framework for automation. 

  • TestNG provides annotations to distinguish tests from methods to be called before the test and after the tests.
  • TestNG provides assertions to validate test conditions.
  • TestNG provides data Provider annotation to create data driven automated tests.
  • TestNG test information is added in an xml file. We can easily create a suite of testNG tests in a master suite xml file and run the tests from eclipse or from Jenkins using ant files. 
  • XML file denotes a test or suite of tests to be executed.


Pre-condition: Below article are useful to setup and create first TestNG test in selenium.



Focus of this article:

  • Creating TestNG xml file to run test suite.
  • How to run TestNG xml file in eclipse.

Let us take an example, suppose there are two classes in a Java Project.  we can create a test suite using testng xml file and run the test suite from eclipse as shown in the below image.


TestNG parallel test

Creating TestNG.xml file for different scenarios:

A. Run all the tests in both classes NewTest and NewTest1. This will execute all the methods with beforetest annotation in both the classes, followed by methods with test annotation, and then methods with afterTest annotation.

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
  <test name="Test">  
   <classes>  
    <class name="testNG.NewTest"/>  
       <class name="testNG.NewTest1"/>  
   </classes>  
  </test>  
 </suite>  


B. Run test in class NewTest and then the methods in NewTest1 class:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
  <test name="Test1">  
   <classes>  
    <class name="testNG.NewTest"/>  
         </classes>  
  </test>  
  <test name="Test2">  
   <classes>  
    <class name="testNG.NewTest1"/>  
   </classes>  
  </test>  
 </suite>  

C. Executing all tests in a package with name as TestNG(Note: TestNG is the name of package in our example, it can be any name other than TestNG also)

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
   <packages>  
    <package name="testNG" />  
   </packages>  
  </test>  
 </suite>  

D. Executing all tests based on the group Name including and excluding specific tests

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
  <groups>  
   <run>  
    <include name="testemail"/>  
    <exclude name="testnoemail"/>  
   </run>  
  </groups>  
   <classes>  
    <class name="testNG.NewTest"/>  
    <class name="testNG.NewTest1"/>  
   </classes>  
 </test>  
 </suite>  

E. Executing specific methods by including methods in the class:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
 <classes>  
   <class name="testNG.NewTest">  
    <methods>  
     <include name="emailval" />  
    </methods>  
   </class>  
   <class name="testNG.NewTest1"/>  
 </classes>  
 </test>  
 </suite>