Sunday, October 16, 2016

  • JAVA_HOME : C:\Program Files\Java\jdk1.8.0_102
  • JDK_HOME : %JAVA_HOME%
  • JRE_HOME : %JAVA_HOME%\jre
  • CLASSPATH :.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
  • PATH : your-unique-entries;%JAVA_HOME%\bin (make sure that the longish your-unique-entries does not contain any other references to another Java installation folder.
Notice that all these environment variables are derived from the "root" environment variable JAVA_HOME. This makes it easy to update your environment variables when updating the JDK. Just point JAVA_HOME to the fresh installation.

Click here for more Selenium topics
In this I will show the code for creating TrayIcon for your Selenium Automation Suite User Interface


public class javaframe extends JFrame {

    private static JPanel contentPane;
    public static JTextField textField;
    public javaframe frame;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                } catch (UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
                try {
                    javaframe frame = new javaframe();
                    frame.setVisible(true);                   
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public javaframe() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
       
        JLabel lblNewLabel = new JLabel("URL");
       
        textField = new JTextField();
        textField.setColumns(10);
        textField.setText("http://www.");
       
        JButton btnNewButton = new JButton("Run");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Multi3 m1=new Multi3(); 
                Thread t1 =new Thread(m1); 
                t1.start(); 
            }
        });


                //Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon =
                new TrayIcon(createImage("/images/selenium.gif", "tray icon"));
        final SystemTray tray = SystemTray.getSystemTray();
      
        // Create a pop-up menu components
        MenuItem aboutItem = new MenuItem("About");
        CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
        CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
        Menu displayMenu = new Menu("Display");
        MenuItem errorItem = new MenuItem("Error");
        MenuItem warningItem = new MenuItem("Warning");
        MenuItem infoItem = new MenuItem("Info");
        MenuItem noneItem = new MenuItem("None");
        MenuItem exitItem = new MenuItem("Exit");
      
        //Add components to pop-up menu
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(cb1);
        popup.add(cb2);
        popup.addSeparator();
        popup.add(displayMenu);
        displayMenu.add(errorItem);
        displayMenu.add(warningItem);
        displayMenu.add(infoItem);
        displayMenu.add(noneItem);
        popup.add(exitItem);
      
        trayIcon.setPopupMenu(popup);
      
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
        }
       
    }
   
   

    protected static Image createImage(String path, String description) {
        URL imageURL = javaframe.class.getResource(path);
        
        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }
   
   
}
class Multi3 implements Runnable{ 

public void run(){ 
    Url url= new Url();
    url.openurl(javaframe.textField.getText());
}
}
This error usually happens if there is a conflict in the Java installations. It means there may be more than one installation of Java in your system. Mostly presence of Oracle Jinitiator in C:\Program Files (x86)\Oracle creates this problem.


Perhaps your file associations got messed up. At the command prompt, try running
ftype | find "jarfile"
On my 64-bit Windows 7 computer, that shows
jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*
You can also change it with ftype:
ftype jarfile="C:\Program Files (x86)\Java\jre6\bin\javaw.exe" -jar "%1" %*
 
and reboot the system. 
 
You may also uninstall the Jinitiator which is no longer supported by Oracle as per our knowledge. 
UI elements in a User Interface will not respond to the user while execution if a thread is not implemented. Here I will explain how to create a thread so that User Interface will keep responding to the user even when the execution is happening.

Create a class outside the Jframe class
class Multi3 implements Runnable{ 

public void run(){ 
    Url url= new Url();
    url.openurl(javaframe.textField.getText());
}
}

Call the Thread from any actionlistener like below

Multi3 m1=new Multi3(); 
                Thread t1 =new Thread(m1);
                t1.start();  
Once you are able to work independently on Selenium scripting, another important task is how to ease your automation test execution. There are various methods for Selenium  execution. In continuous integration, depending on Jenkins integration is a necessity. Automation execution using Jenkins will enable you to perform mass execution, scheduling etc.

                           I have derived another method which will ease your execution to a certain extent. It is by creating a user interface for your automation framework. It will also give a grant appeal to the framework you have created.

For this you need not have much of UI designing skills. With the help of WindowBuilder addon in Eclipse you can create a UI. I have explained this in a previous post. Please follow the link: Click here

How to export the Project to a runnable JAR

1) You can export the whole project to a runnable JAR with UI class as the main class


a) Export the Project




b) Select Runnable JAR under Java



c)

Against the Launch Configuration dropdown-> Select the UI class as main class 
Enter the destination with .jar extension against Export destination





















d)  Click Finish. Your runnable JAR will be created in the given folder


Benefits of having a user Interface for an Selenium Automation Suite/Regression Suite are:

1) You may select dynamic values like Dev/QA Url from the UI and execute, browser: Chrome, Firefox etc
2) This jar can be executed in any system provided JRE is installed. So developers can do unit testing(Eclipse not required)







Saturday, April 2, 2016

I have researched lot, how to execute the TestNG xml from a Java class because I wanted to make my whole project in to a Runnable JAR file and execute it anywhere outside eclipse. This was already in the TestNG docs, but I couldn't understand this first time.

Advantages of this method:


  • You can execute your tests outside eclipse, any machine provided JRE is installed on the machine. You don't need to place the libraries too, The JAR will have all these.
  • Without a Selenium grid, this enables you to  run the tests on any previous versions of browsers running on another machine.
  • This can be executed by anyone without any knowledge of Eclipse, scripting etc.


How to do this:

Assuming you are running your test cases by executing testNG.xml as TestNG.

Steps:

Create a Java class with main method with below code.


public class MainTest {
    public static void main(String[] args){
    List<String> file = new ArrayList<String>();
    file.add("D:/Selenium_workspace/Framework/src/Framework.xml");
    TestNG testNG = new TestNG();
    testNG.setTestSuites(file);
    testNG.run();}
}



Now Export your whole project to runnable JAR. Double click the JAR, will execute the xml as testNG. You may create a user interface to select XML so that it adds flexibility to select which test you need to run.


In this post I will give you a code how to automate selection of date from a date widget neither has the date input and also direct navigation to the months/years. This widget only has the 'Prev', 'Next' buttons to navigate to next months/previous months.













Please find the code below:


public class DateWidgetSample {
public static void main(String args[]){
 String date1="12/04/2016";
 String date2="12/12/2015";

 SimpleDateFormat fmt= new SimpleDateFormat("dd/MM/yyyy");
 Date datevar1 = null;
 Date datevar2 = null;
 try {
  datevar1 = fmt.parse(date1);
  datevar2 = fmt.parse(date2);
 } catch (ParseException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 Calendar cal1 = Calendar.getInstance();
 Calendar cal2 = Calendar.getInstance();
 cal1.setTime(datevar1);
 cal2.setTime(datevar2);

 cal1.set(Calendar.DAY_OF_MONTH, 1);
 cal2.set(Calendar.DAY_OF_MONTH, 1);

 int diffYear = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
 int diffMonth = diffYear * 12 + cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);

 System.out.println(diffMonth);
}
}



Result output is difference of months.


Next you have to do is have a for loop to click the previous/next button based on the number of months obtained in the output. If the number is a positive value, ask to click the next button. If it's a negative value, click the Previous button. That's all. Check code below.

if(diffMonth>0){
for(int i=0; i<diffMonth; i++){
driver.findElement(By.id("nextbtn").click();
}
}

if(diffMonth<0){
for(int j=0; j<diffMonth; j++){
driver.findElement(By.id("previousbtn").click();
}
}





>

Selenium useful links

Powered by Blogger.

Featured Post

Benefits of having a user Interface for a Selenium Automation Suite/Regression Suite

Once you are able to work independently on Selenium scripting, another important task is how to ease your automation test execution. There a...

Video

Popular Posts

Our Facebook Page