Original URL: https://www.guru99.com/xslt-report-selenium.html
XSLT Report in Selenium
The test report is the most important feature of the Selenium framework.
In Selenium, Testng provides its default reporting system. To enhance the reporting feature further XSLT Report is helpful. It also has more user-friendly UI and detail description for the test suite result.
In this tutorial, you will learn –
What is XSLT
XSLT stands for Extensible Stylesheet Language Transformations. XSLT is a language for transforming XML documents into other XML documents (XHTML) that are used by a browser.
With XSLT, we can customize the output file. This can be done by adding/removing attributes and elements in the XML file. This help to interpret result quickly. All browser support XSLT. It uses XPath to navigate through elements and attributes in XML documents.
Below are the most popularly used XSL element in programming:
<xsl:stylesheet> It defines that this document is an XSLT style sheet document.
<xsl:if> is used to put a conditional test against the content of the XML file.
<xsl:template> is used to build templates.
<xsl:apply-templates>is used to apply templates to elements.
<xsl:choose> is used in conjunction with <xsl: otherwise > and <xsl:when > to express multiple conditions.
<xsl:for-each>is used to select every XML element of a specified node.
<xsl:value-of>is used to extract the value of a selected node.
<xsl:sort> is used to sort the output.
Pre-requisite to generate XSLT report
Following is the pre-requisite to generate XSLT report.
1) ANT build tool should be install (Its necessary to install ANT for XSLT reporting feature). ANT is used to compile the source code and creating the build. It is also very much extensible. Refer this link for steps to download and install ANT.
2) XSLT package downloaded.
3) Selenium script that should be executed by TestNG.
We will discuss XSLT report in Selenium Webdriver during this tutorial.
Generate XSLT Report in Selenium
In this scenario, we will use Guru99 demo site to illustrate Generate XSLT report.
Scenario: You will automate and generate XSLT report for the following scenario
- Launch the web browser
- Launch the Firefox and open the site "http://demo.guru99.com/V4/ "
- Login to the application.
Now we will generate XSLT report in selenium as given in below steps.
Step 1): For the above-mentioned scenario. Create and execute the Selenium script for Guru99 demo site.
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class Testing { WebDriver driver= new FirefoxDriver(); @Test(priority=1) public void Login() { //Launching the Site. driver.get("http://demo.guru99.com/V4/"); //Login to Guru99 driver.findElement(By.name("uid")).sendKeys("mngr34926"); driver.findElement(By.name("password")).sendKeys("amUpenu"); driver.findElement(By.name("btnLogin")).click(); //Verifying the manager home page Assert.assertEquals(driver.getTitle(),"Guru99 Bank Manager HomePage" ); } @Test(priority=2) public void verifytitle() { //Verifying the title of the home page Assert.assertEquals(driver.getTitle(),"Guru99 Bank Manager HomePage" ); } @Test(priority=3) public void Logout() { driver.findElement(By.linkText("Log out")).click(); Alert alert=driver.switchTo().alert(); alert.accept(); //Verifying the title of the logout page Assert.assertEquals(driver.getTitle(),"Guru99 Bank Home Page" ); } }
Step 2): Download the XSLT report package from this link:
Unzip the above folder you will get below items:
- build.xml
- testng-results.xsl
Step 3): Unzip the folder and copy all files and paste at the project home directory as shown in below screen.
Step 4): In this step run the build.xml file from eclipse as shown below:
Right click on the build.xml then click on run as Ant build.
Then a new window opens. Now select option 'generateReport'.
Click on Run button. It should generate the report.
Verifying XSLT Report
Once build is successful and moved to project home directory. You will find the testng-xslt folder.
Inside this folder you will find index.html file as shown below:
Now open this HTML file in any browser like Firefox or Chrome, which support javascript. You will find the report as shown in below screen. The pie chart report represents test status more clearly. The filter feature allows the user to filter the result as per the set criteria.
You will find the pie chart showing the percentage of passed, failed and skipped test.
To display the result in regular format click on the Default suite from the left-hand side of the pane. It should show the details of each test as shown in below screen:
Now we forcefully make a test pass, fail and skip.
To view a report of each type for the test result, we need to do some changes in below methods.
- verifytitle() : In the Assert, we pass the wrong expected page title. When the code is executed, it does not match the expected title. Hence making the test fail.
- Logout() : In this method, we forcefully skip the test by using skipexception. So that when the code is executed, this method will get skip.
By doing so, we are trying to show XSLT report with the help of pie chart. It will show the test result for a pass, fail and skip test.
@Test(priority=2) public void verifytitle() { //Verifying the title of the home page Assert.assertEquals(driver.getTitle(),"Guru99 Bank Manager" ); }
@Test(priority=3) public void Logout() { throw new SkipException("Skip this"); }
Now we have one test for each type of result status, i.e., pass, fail and skip.
After execution of script and build.xml. Verify the XSLT report as shown in the below screen:
The test report is more user-friendly report and easy to understand. You can also filter the result by selecting the check box in the below screen.
Note: In the screenshot the 'config' option display the test for which the configuration is done. In big project, there are lots of configuration code. So usually it is used in big projects.
Summary:
XSLT report is required to enhance the TestNG reporting feature in a very user-friendly way.
- XSLT stands for Extensible Stylesheet Language Transformations.
- Download and installation of ANT build refer to given link.
- Generated the XSLT report in selenium and executed the build.xml from eclipse.
- Verify the XSLT report from project folder.
- Verify the XSLT report of each type of result status.