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);}
}



Selenium : Reflection API

 Reflection API


Scenario: In Keyword Driven Framework we have Test Case in below format. We need to implement framework in such a way that underlying framework code works like ReflectionAPITestCase.java


============================================================================
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionAPITestCase {
   public static void main(String[] args)  {
               a.CallMethod("OpenBrowser","","http://www.google.com");
               a.CallMethod("EnterText", "//input[@id='gs_htif0']", "Selenium");
               a.CallMethod("ClickButton", "//button[@id='gbqfba']", "");

   }
}  

=============================================================================

Solution :
Reflection API can be helpful to implement above. It will help you to call methods of a class as shown below

package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class a {
 public static void CallMethod(String Action,String Object,String Value) {
               Method method= a.class.getMethod(Action, String.class,String.class);
                method.invoke(Action,Object,Value
);
        }

public static void OpenBrowser(String Object,String Value){
           System.out.println("Opening browser URL:" + Value );
 }
 
 public static void EnterText(String Object,String Value){
        System.out.println("Entering text in : " + Object  + " Value:" + Value) ;
 }

   public static void ClickButton(String Object,String Value){
      System.out.println("Clicking Object : " + Object ) ;
       }
}

Note : Above implementation allows you to keep on adding “a “ to your “a” class without worrying about call strategy.
-----------------------------------------------------------------------------------------------------------------------------
Invoking Methods using Method Object
http://tutorials.jenkov.com/java-reflection/methods.html

Example 2 :
public class MyClass{
    public static void doSomething(String s){
    }
}

MyClass MyObject = new MyClass();
Method method = MyObject.class.getMethod("doSomething", String.class);    

//get method that takes a String as argument
//Method m=oTestcase.getClass().getMethod("TestCase"+iTestCase_ID,java.util.HashMap.class);


Object returnValue = method.invoke(null, "parameter-value1");


  1. If the method is static you supply null instead of an object instance.
  2. If the method is none static you supply something like :
 Object returnValue = method.invoke(new MyClass(), "parameter-value1");
  1. The Method.invoke(Object target, Object ... parameters) method takes an optional amount of parameters, but you must supply exactly one parameter per argument in the method you are invoking. In this case it was a method taking a String, so one String must be supplied.