Road to generate html report for Python Webdriver test suite using HTMLTestRunner

In my previous post ""Road to create test suite for python Webdriver scripts, I posted that how to create Suite file for webdriver python test script, but In this post I will show you how to integrate test suite with HTMLTestRunner to generate report in html format.

Before going through this post click here ( “Road to create test suite for python Webdriver scripts” ) to see both webdrive python unit test scripts.  I am taking these as example.

Follow below steps to setup:
1. Click on link "HTMLTestRunner" and download “HTMLTestRunner.py”, and “test_HTMLTestRunner.py” file put in your webdriver python project root folder.
2. Import all your test script in “test_HTMLTestRunner.py” as below:
import WebdriverTest2
import WebdriverTest1

Road to create test suite for python Webdriver scripts

In this post I will show you how to create suite file for Webdriver python UnitTest.
Following are two webdriver python unit test script.
1. WebdriverTest1.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import unittest, time, re

class WebdriverTest1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://en.wikipedia.org"
       
    def test_Wikipedia(self):
        driver = self.driver
        driver.get(self.base_url + "/wiki/Main_Page")
        driver.find_element_by_id("searchInput").clear()
        driver.find_element_by_id("searchInput").send_keys("India")
        driver.find_element_by_id("searchButton").click()    
   
    def tearDown(self):
        self.driver.quit()       

if __name__ == "__main__":
    unittest.main()

TestNG factory class with example

TestNG factory class is used to create instance of testNg class at runtime (dynamically). When we apply Factory annotation on method, it always return object array of Object class ( “Object[]”). This is useful annotation when you run test multiple time.

TestNG pick @Factory method from class and consider each object mentioned in factory test as a TestNg class and invoke in separate.

Example:  FactoryClassImp.java
package com.tests;

import org.testng.annotations.Factory;

public class FactoryClassImp {

 @Factory
 public Object[] createTest() {
  Object[] res = new Object[3];
  res[0] = new FactoryTestClass(2, 2);
  res[1] = new FactoryTestClass(2, 3);
  res[2] = new FactoryTestClass(2, 4);

  return res;
 }
}


TestNG - Exception Test

In this post I will show you how to handle exception in Testng. TestNg provides expectedExceptions parameter  which is used along with @Test annotation, exp @Test(expectedExceptions)

package com.test;

import org.testng.annotations.Test;

public class TestNgException {

        @Test(expectedExceptions = ArithmeticException.class)
        public void testException() {
               int a = 10;
               int b = 0;
               a = a / b;
        }

        @Test
        public void testException1() {
               int a = 10;
               int b = 0;
               a = a / b;
        }
}

In test method testException I used “@Test(expectedExceptions = ArithmeticException.class)” so airthmetic exception handle but in  test testException1 exception generated.

TestNG - Ignore Test

In this post I will show you how to ignore testng test case to execute,  TestNg provide attribute enabled which make the test to be execute or not, if test mark @Test(enabled = false) then test will not execute. By default enabled value is true.

Here is example for ignore test case:

package com.test;

import org.testng.Assert;
import org.testng.annotations.Test;

public class IgnoreTestClass {

        @Test
        public void testMethod1() {
               Assert.assertTrue(true);
        }

        @Test(enabled = false)
        public void testMethod2() {
               Assert.assertTrue(true);
        }

        @Test(enabled = true)
        public void testMethod3() {
               Assert.assertTrue(true);
        }
}

Run above test you will see that testMethod2 will ignored and not executed:

PASSED: testMethod1
PASSED: testMethod3

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
====================================   

How to read write data in properties file using Java

For reading and writing data into properties file I am using Properties class in this post.
Create a file name with extension .properties like I have created “File.properties” below file:

Reading Data: Below code read data from above file using key and file name as an arguments.
package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class FileReader {
   
    public String readData(String key, String fileName) {
           String value = "";
           try {

                         Properties properties = new Properties();
                         File file  = new File("File.properties");
                         if (file.exists()) {
                                             properties.load(new FileInputStream(file));
                                             value = properties.getProperty(key);
                        }
            } catch (Exception e) {
                        System.out.println(e);
            }
           return value;
    }
   
    public static void main(String []str){
                        
             FileReader fileReader = new FileReader();                           
                        
                         String name = fileReader.readData("Name", "fileName");
                         System.out.println("Name :"+name);
                         String url = fileReader.readData("URL", "fileName");
                         System.out.println("URL :"+url);
        }
}
If you run above file you will get Name and Url values and print on console

Road to publish Webdriver TestNg report in Jenkins

In this post I will show you how to publish TestNg report on Jenkins server.

Installation TestNG plug-in.
1. After launching Jenkins server go to "Manage Jenkins>>Manage Plugins"
2. Enter TestNg in filter field, you will see “TestNg Results Plugin” like below screen.


3. Click on check box and then click on installation button.

How to read write data in properties file using php

For reading and writing data into properties file I am using parse_ini_file() function in this post.
Create a file name with extension .properties like I have created below:


Reading Data: below code read data from above file using key:
function getValue($key)
{
    $ini_array = parse_ini_file("File.properties");
    $value =  $ini_array[$key];
    return $value;
}
getValue(“Name”);
getValue(“URL”);

Road to capture screen shot of failed Webdriver test script part2

In my previous post ( "Road to capture screen shot of failed webdriver test script part1") I have posted how to capture screen shot using exception handling, but in this post I will show you how to capture screen shot by overriding testng listeners.

Here is code (MyListner.java) where I override onTestFailure,  onTestSuccess and onTestSkipped methods of class TestListenerAdapter.  Created function CaptureScreenShot and called it into onTestFailure function.

package com.webdriver.test;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class MyListner extends TestListenerAdapter{   
           
     @Override
     public void onTestFailure(ITestResult result){ 
            CaptureScreenShot(result);
            System.out.println(result.getName()+" Test Failed \n");
     }
           
     @Override
     public void onTestSuccess(ITestResult result){
           System.out.println(result.getName()+" Test Passed \n");
     }
           
     @Override
     public void onTestSkipped(ITestResult result){
           System.out.println(result.getName()+" Test Skipped \n");
     }
            
     public void CaptureScreenShot(ITestResult result){
           Object obj  = result.getInstance();
           WebDriver driver = ((FailedTestScreenCapture) obj).getDriver();
                        
           File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                                         
           try {
                  FileUtils.copyFile(scrFile, new File(“screenshot/” result.getName()+".png"));
           }
           catch (IOException e) {
                e.printStackTrace();
           }
      } 
}

I called above created listeners java file in below “FailedTestScreenCapture.java” TestNg file.

Road to create Virtual device (Emulator ) for beginner

In this post I will show you how to install android SDK , setup Android SDK path in system environments variable, How to update android API and how to create Virtual Devices.

Installation:
  1. Download Androd sdk exe or Zip file from link: download
  2. Install exe file in your machine or extract the zip file.
Setup Path:
1. Right click on the "My Computer" icon.
2. Click Properties
3. Click Advanced tab
4. Click Environment Variables button, You should see below screen: