Wednesday, 21 January 2015

Java-Fetch InternetExplorer Version

Java Program to fetch internet Explorer version.

import java.io.IOException;
public class Test {

    public static void main (String[] args) throws IOException {
        String IEVersioncmd = "reg query \"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Internet Explorer\" /v svcVersion";
        System.out.println(execCmd(IEVersioncmd).split("REG_SZ")[1].trim());
     }
   
    @SuppressWarnings("resource")
    public static String execCmd(String cmd) throws java.io.IOException {
        java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }   
}

Executing the above java program:

Output:
Note:
Verified the InternetExplorer version using above program on Windows OS for IE 10 and IE 11.

Internet Explorer Version in IE Browser.

Tuesday, 20 January 2015

Java-Print all the file names starting with "Test-"

Java program to print all the file names starting with "Test-".

package fileio;
import java.io.File;
import java.io.FilenameFilter;

public class FileNames {
   
    public static void main(String[] args) {
        try {

            final String filePrefix = "Test-";
            String filesDir = System.getProperty("user.dir")+"\\src\\fileio";
           
            File dir = new File(filesDir);
            File[] foundFiles = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(filePrefix);
                }
            });

            System.out.println("Number of files found : "+foundFiles.length);
            System.out.println("");
            for (File file : foundFiles) {
              System.out.println(file.getName());
            }             
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   

}


Executing the above Java Program:

Output:

Java-Delete files for the file names starting with "Test-"

Java Program to delete files for the file names starting with "Test-"

package fileio;
import java.io.File;
import java.io.FilenameFilter;

public class DeleteFileName {

    public static void main(String[] args) {
        try {

            final String filePrefix = "Test-";
            String filesDir = System.getProperty("user.dir") + "\\src\\fileio";

            File dir = new File(filesDir);
            File[] foundFiles = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(filePrefix);
                }
            });

            System.out.println("Number of files found : " + foundFiles.length);
            System.out.println("");
            for (File file : foundFiles) {

                try {
                    if (file.delete()) {
                        System.out.println(file.getName() + " is deleted!");
                    } else {
                        System.out.println("Delete operation is failed, unable to delete the file.");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Executing the above java program:

Output:



Java-Print the file content for the file names starting with "Test-"

Java Program to print the file content for the file names starting with "Test-".

package fileio;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;

public class PrintFileContent {
   
    public static void main(String[] args) {
        try {

            final String filePrefix = "Test-";
            String filesDir = System.getProperty("user.dir")+"\\src\\fileio";
           
            File dir = new File(filesDir);
            File[] foundFiles = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(filePrefix);
                }
            });

            System.out.println("Number of files found : "+foundFiles.length);
            System.out.println("");
            for (File file : foundFiles) {
              System.out.println("----------------------------------------------");
              System.out.println("               "+file.getName());
           
              //Prints the file content.
              BufferedReader br = null;
                try {
                    String sCurrentLine;
                    br = new BufferedReader(new FileReader(file));
       
                    while ((sCurrentLine = br.readLine()) != null) {
                        System.out.println(sCurrentLine);
                    }
       
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (br != null)br.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }               
            }             
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

Note:
File "Test-001.txt","Test-002.txt" and "Test-003.txt" should have some content
    Ex: "This content of test file for Test001"









Executing the above java program:


Output:

Monday, 22 December 2014

Adding firebug add-on to the FirefoxDriver instance

When automation scripts are executed, firefox browser is launched with out add-ons in it.
Adding the firebug add-on to the the FirefoxDriver instance is possible with the below approach.

1. Download the "firebug-<<version>>.xpi" file in Internet Explorer browser.
https://addons.mozilla.org/en-US/firefox/addon/firebug/


Note: if the url "https://addons.mozilla.org/en-US/firefox/addon/firebug/" is accessed in firefox browser, add-on will be directly installed in to firefox browser.

2. Click on "Download" button.

3. Click on "download anyway" link.

4. Click on "Save"



Program:
import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class FF {
    public static void main(String[] args) throws IOException {
        //Path of the firebug addon(i.e. firebug-<<version>>.xpi file)       
        File addonpath = new File(System.getProperty("user.dir")+"\\other\\firebug-2.0.7-fx.xpi");
       
        FirefoxProfile firefoxprofile = new FirefoxProfile();
       
        //This will add Firebug add-on to the new profile created.
        firefoxprofile.addExtension(addonpath);
       
        //Pass the Firefox profile to the FirefoxDriver instance.
        WebDriver driver = new FirefoxDriver(firefoxprofile);
        driver.get("http://www.google.com");
       
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Webdriver");
    }
}
Note:  Above program is not integrated with TestNG/Junit, execution is done from the main method.

Executing the program in Debug mode:

Script execution is paused at break point.

Firebug add-on is added to the firefox browser launched by webdriver api and is visible on the top right corner of the browser.
By default there will be no add-ons present in the firefox browser launched by using webdriver api.
This add-on(i.e. firebug addon) is added only if the firebug extension is added to the FirefoxDriver instance. ex: WebDriver driver = new FirefoxDriver(firefoxprofile);

Press keyboard 'F6'
 


 Press keyboard 'F8'

Wednesday, 17 December 2014

Executing simple python programs in Eclipse IDE

IDE helps in preventing the simple syntax errors while writing the python program's,  we can write, compile and execute the code in IDE. Productivity is more using IDE(i.e. scripts can be developed very fastly)

Below procedure demonstrates executing the python programs in Eclipse IDE.

Prerequisites:
1. Python is installed, click here for python installation.
2. PyDev extension is installed in Eclipse IDE, click here for PyDev extension installation. 

Executing simple python program:
Create new python project.
Select "File" >> "Open File" >> "Other"

Expand "PyDev", select "PyDev project" and click on "Next >" button.

Enter the project name and click on "Finish"button.



Create "PyDev Module":
mouse right click on the project created, select "New" >> "PyDev Module".

Enter the module name and click on "Finish" button




Simple python program:
'''
Created on 15-Dec-2014

@author:
'''
print("My First Program");


Executing the python program:



Tuesday, 16 December 2014

Installing Pydev Extenstions for Eclipse IDE

Once Pydev Extenstions is installed  in Eclipse IDE, developer can write and execute the python program in Eclipse IDE.

Prerequisite:
1. Python is already installed in the system, click here for python installation.

2. PyDev requires java 7 in order to run.

3. Installation folder should have the permissions to write(i.e. install)

4. Eclipse version should be 3.6.1 or greater, since there are some issues while installing in shared folder with Eclipse 3.6.0
Refernece: https://bugs.eclipse.org/bugs/show_bug.cgi?id=322929

Installing Pydev Extensions:
Open the Eclipse IDE, Select the menu "Help" >> "Install New Software"

Click "Add" button

Enter the name & location and click on "OK" button.
location url: http://pydev.org/updates

Select the checkboxes and click on "Next" button.


Click on "Next" button in 'Install Details'

Accept the license agreements and click on "finish" button.


Restart the Eclipse IDE and verify PyDev is installed.

Verify PyDev is installed, Select "Window" >> "Preferences"


Set the Python Interpreter:
Click 'Browse' button, select "python.exe" file and click on "OK" button.