Friday, April 22, 2011

Open Script : Create New Java Class and Access the Methods in it

Go to Developer Perspective in Open Script.
Go to Window > Show View >Package Explorer
under "default package". You will find the Open Script test script name as "script.java"
Right Click and Create a New Class with a name "sayHello"

The Content in the class file should look like this
class sayHello {
   
    public void sayHi(Object str)
    {
        System.out.println("Saying Hi to : "+str);
    }

}


Now go to the open script file and enter the following code in the run() method

public void run() throws Exception {

         // Creating and Using external Java Class
       
        sayHello s=new sayHello();
        // Access the say Hi Method in it
        s.sayHi("Ajith");
       
    }
Output:
Saying Hi to : Ajith

Open Script : Display Open Script Settings at Runtime

 public void run() throws Exception {
        // Displays the Details in the the result file under Comments Section
        info(getSettings().toString());
       // Prints to the console
        System.out.println(getSettings().toString());
        System.out.println(getSettings().getProperties().size());
       
}
_________________________________________________________________________
// To Display the Same Using Custom java Code :


import java.util.Enumeration;
import java.util.Hashtable;

import oracle.oats.scripting.modules.basic.api.*;
import oracle.oats.scripting.modules.browser.api.*;
import oracle.oats.scripting.modules.functionalTest.api.*;
import oracle.oats.scripting.modules.utilities.api.*;
import oracle.oats.scripting.modules.utilities.api.sql.*;
import oracle.oats.scripting.modules.utilities.api.xml.*;
import oracle.oats.scripting.modules.utilities.api.file.*;
import oracle.oats.scripting.modules.webdom.api.*;
import oracle.oats.scripting.modules.formsFT.api.*;
import oracle.oats.utilities.CaseInsensitiveProperties;

public class script extends IteratingVUserScript {
    @ScriptService oracle.oats.scripting.modules.utilities.api.UtilitiesService utilities;
    @ScriptService oracle.oats.scripting.modules.browser.api.BrowserService browser;
    @ScriptService oracle.oats.scripting.modules.functionalTest.api.FunctionalTestService ft;
    @ScriptService oracle.oats.scripting.modules.webdom.api.WebDomService web;
    @ScriptService oracle.oats.scripting.modules.formsFT.api.FormsService forms;

    public void initialize() throws Exception {
    }

    /**
     * Add code to be executed each iteration for this virtual user.
     */
    public void run() throws Exception {
        /*
         * Step 1 : get the Settings object
         * 2: get the keys
         * 3: get the value of key
         */
        //Step 1 : Get the Settings Object
        CaseInsensitiveProperties cip=getSettings().getProperties();
        // CaseInsensitiveProperties is a subclass of Properties Class
        // HashTable > Properties > CaseInsensitiveProperties
        // 2 - Get the keys
        // Returns Enumeration Object
        Enumeration enum_keys=cip.keys();
        //
        // Displays the type of Enumeration - java.util.HashTable$Enumerator@1202a06
        // Enumeration is of type Hashtable
        System.out.println("Object Type is : "+enum_keys.toString());
       
       
       
        //Iteration through the Enumeration Set
        // Use the hasMoreElements method to verify the last element
        // User nextElement - to retrieve the Object
        Object v_key;
        Object v_val;
       
        while(enum_keys.hasMoreElements())
        {
            v_key=enum_keys.nextElement();           
           
            // cip.get(Object key) - operates on a Hashtable and Returns an Object
            v_val=cip.get(v_key);           
            System.out.println("Key : "+v_key.toString()+"\tValue : "+v_val);
        }
       
// Displays 230 - Values on my system.
        System.out.println("Size is : "+cip.size());       
       
    }

    public void finish() throws Exception {
    }
}

Sample Output:

Object Type is : java.util.Hashtable$Enumerator@135b92e
Key : file.encoding.pkg    Value : sun.io
Key : http.usedeflate    Value : false
Key : org.osgi.framework.executionenvironment    Value : OSGi/Minimum-1.0,OSGi/Minimum-1.1,JRE-1.1,J2SE-1.2,J2SE-1.3,J2SE-1.4,J2SE-1.5,JavaSE-1.6
Key : ft.smartmatch    Value : true
Key : osgi.framework    Value : file:/D:/OracleATS9.2/openScript/plugins/org.eclipse.osgi_3.4.2.R34x_v20080826-1230.jar
Key : vmargs    Value :
Key : http.usegzip    Value : true
...( Total 230 Key Value Pairs were displayed on my system )
...
...
...
...
...
Key : java.specification.version    Value : 1.6
Key : err.http.node_not_found_exception    Value : Warn
Key : encryption_properties_file    Value : C:\Documents and Settings\amoni.APPLICATIONS\osworkspace\.metadata\.plugins\oracle.oats.scripting.utilities\encryption.properties
Key : sun.arch.data.model    Value : 32
Size is : 230
Size is : true

Open Script : Set Session Name & Print the Current Session Name

import oracle.oats.scripting.modules.basic.api.*;
import oracle.oats.scripting.modules.basic.api.exceptions.AbstractScriptException;
import oracle.oats.scripting.modules.browser.api.*;
import oracle.oats.scripting.modules.functionalTest.api.*;
import oracle.oats.scripting.modules.utilities.api.*;
import oracle.oats.scripting.modules.utilities.api.sql.*;
import oracle.oats.scripting.modules.utilities.api.xml.*;
import oracle.oats.scripting.modules.utilities.api.file.*;
import oracle.oats.scripting.modules.webdom.api.*;
import oracle.oats.scripting.modules.formsFT.api.*;

public class script extends IteratingVUserScript {
    @ScriptService oracle.oats.scripting.modules.utilities.api.UtilitiesService utilities;
    @ScriptService oracle.oats.scripting.modules.browser.api.BrowserService browser;
    @ScriptService oracle.oats.scripting.modules.functionalTest.api.FunctionalTestService ft;
    @ScriptService oracle.oats.scripting.modules.webdom.api.WebDomService web;
    @ScriptService oracle.oats.scripting.modules.formsFT.api.FormsService forms;

    public void initialize() throws Exception {
        browser.launch();
    }

    /**
     * Add code to be executed each iteration for this virtual user.
     */
    public void run() throws Exception {
       // Use getSession instance to set the Name of the Session
       // Set the Name of the session to "Test1"
       getSession().setName("Test1");
      
        // Print the Current Session Name
        sop(getSession().getName());
       
    }
    public void sop(Object str)
    {
        System.out.println(str);
    }

       public void finish() throws Exception {
    }
}

______________________________________________________________
Output :
Test1

Open Script:TIP:waitForPage(null) depends on Object Timeout Value in Open Script Preferences

waitForPage(null) depends on Object Timeout Value in Open Script Preferences

Open Script Exercise : Navigate to a site and wait for the page to load completely

import oracle.oats.scripting.modules.basic.api.*;
import oracle.oats.scripting.modules.basic.api.exceptions.AbstractScriptException;
import oracle.oats.scripting.modules.browser.api.*;
import oracle.oats.scripting.modules.functionalTest.api.*;
import oracle.oats.scripting.modules.utilities.api.*;
import oracle.oats.scripting.modules.utilities.api.sql.*;
import oracle.oats.scripting.modules.utilities.api.xml.*;
import oracle.oats.scripting.modules.utilities.api.file.*;
import oracle.oats.scripting.modules.webdom.api.*;
import oracle.oats.scripting.modules.formsFT.api.*;

public class script extends IteratingVUserScript {
    @ScriptService oracle.oats.scripting.modules.utilities.api.UtilitiesService utilities;
    @ScriptService oracle.oats.scripting.modules.browser.api.BrowserService browser;
    @ScriptService oracle.oats.scripting.modules.functionalTest.api.FunctionalTestService ft;
    @ScriptService oracle.oats.scripting.modules.webdom.api.WebDomService web;
    @ScriptService oracle.oats.scripting.modules.formsFT.api.FormsService forms;

    public void initialize() throws Exception {
        browser.launch();
    }

    /**
     * Add code to be executed each iteration for this virtual user.
     */
    public void run() throws Exception {
        // Navigate to Three Sites and Wait for the Sites to Completely load the page
        this.naviagate_site("http://www.yahoo.com");   
        this.naviagate_site("http://www.google.com");       
        this.naviagate_site("http://www.gmail.com");
    }
    public void naviagate_site(String site_name) throws Exception
    {
        web.window("/web:window[@index='0']").navigate(site_name);
        //  wait for object /web:window[@index='0' or @title='Yahoo! India']/web:document[@index='0']/web:a
        //    The Value waitForPage(null) - null uses the default time out from Open Script Preferences
        //  Got to View > Open Script Preferences
        //  In the Treeview Navigate to "Open Script > Playback > Web Functional"
        //  Set timeout to : 20 secs under Object Timeout
        web.window("/web:window[@index='0']").waitForPage(null);
       
    }

    public void finish() throws Exception {
    }
}

Open Script : Paused by Exception, Time out occurred

' Load a page
web.window["/web:window[@index='0'"].navigate("http://yahoo.com");

'Wait for the page to complete loading
web.window["/web:window[@index='0'"].waitForPage(null);

 ______________________________________________________________________________

'Note :If a Pause by Exception Occurs
with the Reason :
"Timeout(5 seconds occurred waiting for page to load"

Go To View > Open Script Preferences.
In the Tree Navigate to Open Script > Playback > Web Functional


Object Timeout : 5
'Change this value to 10 or time to wait for object to time out

Open Script : Wait for page to load

' Load a page
web.window["/web:window[@index='0'"].navigate("http://yahoo.com");

'Wait for the page to complete loading
web.window["/web:window[@index='0'"].waitForPage(null);

'Note :If a Pause by Exception Occurs
with the Reason :
"Timeout(5 seconds occurred waiting for page to load"

Go To View > Open Script Preferences.
In the Tree Navigate to Open Script > Playback > Web Functional

Object Timeout : 5
'Change this value to 10 or time to wait for object to time out

Open Script :Tip of the day : wait for 5 seconds

' Navigate to a page 
web.window("/web:window[@index='0']").navigate("http://www.google.com");

' Wait for 5 seconds - 5000 milliseconds
delay(5000);