Sunday, January 25, 2015

Selenium-IDE template for TestNG

As mentioned in the previous post we can get a customized test class template for Selenium-IDE using existing templates. As we use TestNG for our testing we faced an issue when generating UI tests through Selenium-IDE. Selenium-IDE currently has support for JUnit but not for TestNG. As a solution we customized the JUnit 4 template for TestNG.

We use the  'Java / JUnit 4 / WebDriver' as the base template in this post as well.
The changes needed to be done
  • Change the imports to TestNG
  • Modify assertion methods according to TestNG specification
    • TestNG and JUnit has different order of parameters in 'assertEquals' method [1] [2]
JUNIT  ---> assertEquals(expected, actual);
TESTNG ---> assertEquals(actual, expected);
Create a new template as described in the previous post using Java / JUnit 4 / WebDriver' template and  give a new name. Then do the following changes to the source file in a text editor.

Replace following imports with the imports given next.
import org.junit.*;
import static org.junit.Assert.*;
New Imports
import org.testng.annotations.*;
import static org.testng.Assert.*;
Next we need to change the order of the arguments. Replace the Equals.prototype.assert section with the following.
Equals.prototype.assert = function() {
  return "assertEquals(" + this.e2.toString() + ", " + this.e1.toString() + ");";
};
  • Click Add button in the formats section mentioned in previous post. 
  • Copy and paste the customized source on it. 
  • Save and Close.
  • Restart the Selenium-IDE.
Now you are ready to generate UI tests on TestNG!!!

No comments:

Post a Comment