Tuesday, August 26, 2014

Java :Simple GUI (Button activating an Edit Field)

Java :Simple GUI (Button activating an Edit Field) 

Steps for creating new GUI :
  1. Make sure you have added Windows Builder Pro addin to your Eclipse
  2. Select the immediate parent location where you want to create a new GUI file.
  3. Rt click>new>other>Window Builder > Swing Designer >Select Application Window.
  4. Click on Next
  5. In Create application window give a Name in "Name"box.
  6. Click on Finish
Refer : http://catchbug.blogspot.in/2014/02/java-add-browse-button-to-gui.html

 // Use JDialog instead of JFrame for thread to pause until an event


Code for gui


import java.awt.EventQueue;
public class gui {

 JFrame frame;
 JButton btnNewButton ;
 JTextArea textArea;
 SpringLayout springLayout;
 JTextPane textPane;

// gui(){ initialize();}

public void initialize() {
 
 frame = new JFrame();
 frame.setBounds(100, 100, 450, 300);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 springLayout = new SpringLayout();
 frame.getContentPane().setLayout(springLayout);
 
 btnNewButton = new JButton("New button");
 springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 113, SpringLayout.NORTH, frame.getContentPane());
 springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 132, SpringLayout.WEST, frame.getContentPane());
 frame.getContentPane().add(btnNewButton);
 
 textPane = new JTextPane();
 textPane.setText("Hi");

 btnNewButton.addActionListener(new ActionListener(){ 
  public void actionPerformed(ActionEvent e){ 
   
   if(textPane.isDisplayable()==false){
    frame.getContentPane().add(textPane); 
    springLayout.putConstraint(SpringLayout.NORTH, textPane, 31, SpringLayout.SOUTH, btnNewButton);
    springLayout.putConstraint(SpringLayout.WEST, textPane, 148, SpringLayout.WEST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, textPane, 248, SpringLayout.WEST, frame.getContentPane());
   }
   else    
    frame.getContentPane().remove(textPane);
 
    frame.validate();
    frame.repaint();
 
   }});

}
}
Code calling GUI
 class Test {

  public static void main(String[] args) {

   gui obj=new gui();
   obj.initialize();
   obj.frame.setVisible(true);
     }

Saturday, August 23, 2014

Selenium : Selenium WebDriver And Interface

 WebDriver And Interface






Q. What Is An Interface And Why To Use It?

Interface is set of rules for behavior of application. Interface looks like class but it is not. When you implements that Interface In any class then all those Interface rules must be applied on that class.
 In short, if you implement an Interface on class then you must have to override all the methods of Interface In your class. Interface will create Rules to follow structure for class where it Is Implemented. This way, if you look at the code of Interface, You can get Idea about your program business logic. When you are designing big architecture applications like selenium web driver, you can use Interface to define business logic at Initial level.
 Interface can be implemented with any class using implements keyword. There are set of rules to be followed for creating an Interface. Let me tell you all these rules first and then give you an example of an Interface.

1.       Interface can not hold constructor.
2.       Interface can not hold instance fields/variables.
3.       Interface can not hold static methods.
4.       You can not instantiate/create object of an Interface.
5.       Variables Inside Interface must be static and mandatory to initialize the variable.
6.       Any class can Implement Interface but can not extend Interface.
7.       Can write body less methods Inside Interface.
8.       By default all the methods and variables of Interface are public so no need to provide access modifiers.

public interface College {        //Interface file
     static String Collegename = "XYZ";  //static by default
     void StudentDetails();    //Created non static methods without body.
     void StudentResult();
}

public class Computer implements College {
                                        //@Override annotation describes that methods are overridden on interface method.
                                         //Methods name, return type are same as methods of an Interface.
     @Override
     public void StudentDetails() {  System.out.println("hi"); }

     @Override
     public void StudentResult() { System.out.println("bye");  }
}

public class TestCollege
{                  //Class file. No need to implement Interface.

                    public static void main(String[] args)
{
                                         //Can access Interface variable directly using Interface name.
                                        System.out.println(College.Collegename+" Collage student details.");
 
                                                            //Created Computer class object with reference of interface College.
                                        College compdept = new Computer();
                                                            //Methods will be called from Computer class.
                                        compdept.StudentDetails();
                                        compdept.StudentResult();
 }
}
-------------------------------------------------------------------------------------------------------------------------
Selenium WebDriver And Interface

Simple example of Interface In selenium WebDriver Is WebDriver Interface. When you are Initializing any browser using selenium WebDriver, You are writing statements like bellow.

WebDriver driver = new FirefoxDriver();
Or
WebDriver driver = new ChromeDriver();

You can view more examples of selenium WebDriver on THIS PAGE.Here, WebDriver Is Interface and FirefoxDriver and ChromeDriver are the class files where WebDriver Interface Is Implemented.

http://software-testing-tutorials-automation.blogspot.in/2014/04/interface-in-java-tutorials-for.html

Wednesday, August 20, 2014

BASIC HTML FOR TESTING SELENIUM - SOURCE CODE

 BASIC HTML FOR TESTING SELENIUM - SOURCE CODE

app link -
http://catchbug.blogspot.in/2014/08/basic-html.html




<!—This is a comment  -   ref http://www.ironspider.ca/basic_html/structure.htm-->
<html>
<head>
               <title> My Home Page </title>
</head>
<body>
               HELLO WORLD!
<br>
<br>
               EDIT FIELD
               <input type="text" name="firstname">
<br>
<br>
               CHECKBOX
               My favourite colors are:<br><br>
               <input type="checkbox" name="color" value="red">Red<br>
               <input type="checkbox" name="color" value="yellow">Yellow<br>
<br>
<br>
               RADIO BUTTON
               Your current web browser is:<br><br>
               <input type="radio" name="browser" value="IE" checked>Internet Explorer<br>
               <input type="radio" name="browser" value="Mozilla">Mozilla<br>
<br>
<br>
               DROP DOWN
               I like my coffee:<br><br>
               <select name="coffee">
               <option value="black">Black</option>
               <option value="cream" selected>With cream</option>
<br>
<br>
               BUTTON
               <input type="submit" value="Submit Information">
<br>
<br>
               TABLE
<table border="5">
   <tr>     <td>Row 1, Cell 1</td>                    <td>Row 1, Cell 2</td>     </tr>
   <tr>     <td>Row 2, Cell 1</td>                    <td>Row 2, Cell 2</td>     </tr>
</table>
</body>
</html>


Basic html

My Home Page 1. Text Data :

2. EDIT FIELD :

3. CHECKBOX : My favourite colors are:

Red
Yellow


4. RADIO BUTTON : Your current web browser is:

Internet Explorer
Mozilla


5. DROP DOWN : I like my coffee:



7. TABLE :
Row 1, Cell 1 Row 1, Cell 2
Row 2, Cell 1 Row 2, Cell 2

Saturday, August 16, 2014

Selenium : Error Logging using Listeners



TestNG Error Reporting using Listeners



Testng.xml

<suite name ="test">
               <listeners>
                              <listener class-name="test.custom_listener" />
               </listeners>
              
               <test name="test name">
                              <classes>
                                             <class name="test.test_listener"/>
                              </classes>                          
               </test>
</suite>


custom_listener.java  (Listener File)

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;

 public class custom_listener extends TestListenerAdapter
{                                                          
                     @Override
                        public void onTestFailure(ITestResult tr) {
                            Reporter.log(tr.getName()+ "--Test method failed\n");
                        }
                                         
                        @Override
                        public void onTestSkipped(ITestResult tr) {
                                        Reporter.log(tr.getName()+ "--Test method skipped\n");
                        }
                                         
                        @Override
                        public void onTestSuccess(ITestResult tr) {
                                        Reporter.log(tr.getName()+ "--Test method success\n");
                        }
}

Test_listener.java
(Base Class - BaseClass is the class where my setup(@BeforeClass) and teardown(@AfterClass) method is declared.)

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

public class test_listener {

                    @Test //PASS
                       public void testMethodOne(){     Assert.assertTrue(true); }
                     
                    @Test //FAIL
                    public void testMethodTwo(){        Assert.assertTrue(false); }
                     
                     @Test(dependsOnMethods={"testMethodTwo"})            //DEPENDS ON "testMethodTwo" which will be skipped
                    public void testMethodThree(){     Assert.assertTrue(true);}
}