Wednesday, November 16, 2011

Initial Script for Web application testing - RFT

Summary:
IBM Rational Functional Tester makes cross browser testing a breeze. One can use browser Mozilla Firefox to automate a web page and run the finished tests in browser Internet Explorer and vice versa. Lets learn to close all browsers before firing up a test (web based) and invoke browser of choice as well as URL via "DATAPOOL".
Note: the following instructions are for invoking and closing browsers via Empty script (not in record mode). These steps were written using the IBM® Rational® Functional Tester for Java™. Code examples will be in Java, but all the concepts apply to the .NET version of Rational Functional Tester as well.

Introduction
 5 Step tutorial for creating the initial script for invoking any webpage in the desired browser via Datapool.
Step ONE
Prepare browsers in Enable Environments
Go to Menu - Configure > Enable Environments for Testing (in Rational Functional Tester)

Click on button Search. "Search for Web Browsers" dialog appears.
Click on Radio button "Search all" and click on button "Search".

(Operating systems and browsers RFT is compatible with -  https://www-304.ibm.com/support/docview.wss?rs=953&uid=swg27019698)
RFT will find browsers "Internet Explorer" & "Mozilla Firefox", if installed on the system. Make sure the compatible browsers are installed and compatible Java is enabled for the browsers. (Note: if your Operating system is 64-bit and the browsers are 32-bit, you may need to install both Java for 64-bit and 32-bit; Generously take the support of IBM to enable browsers with the correct version of JAVA)

Click on each browser and click on button "Enable" to enable each browser. Click on button "Test" highlighting each browser. The browser must invoke as such -

Click on button "Click Here to Run Diagnostics Tests".

Passed = Good;
Fail = Contact IBM support. They will help you enable the compatible JAVA for the browsers.
Click on tab "Details (Advanced)"

Java Enabled In Browser = true  =             Good, Now the browsers are ready for automation and tests.

Step TWO
Add Datapool with variables -
Create a Datapool with tabs (variables) - Browser, URL. More tabs may be added as per necessity.

Use full names of the browsers as they would appear in Enable Environments window.

Step THREE
Add Empty Script to RFT in the desired project / folder Associate the Datapool "Intital" to this script. I prefer to name it "Initial" Since this script is responsible for invoking intended browser and going to the intended URL. (Note: a company can have several websites with several back ends (meaning each application can have several URLs like one for QA, one for UAT, one for Sand Box etc.) Any of these URLs can be invoked via Datapool variable avoiding editing of a hard coded scripts all the time.)
Initial Script can be exported and imported into other projects to be used for other web applications. This saves a lot of time and keeps things simple.

Step FOUR
Invoke the "Test Object Map" via the Script Explorer. Bring up the Website that needs to be tested in a browser. Go to menu - Test Object > Insert Object.

"Insert a GUI Object into the Object Map" dialog box opens -

Using the Drag Hand Selection or Test Object Browser, select the page that needs to be tested and add it to the Test Object Map.
Right click on the element and click on "Add to Script 'so and so'". You will see the element added to the Test Objects in script explorer.  Rename it as "Website"
Right click on the element Browser, usually displayed as "Html: Browser: htmlBrowser: Html.HtmlBrowser" in the Test Object Map and add it to the script. Rename it as "Browser".

Step FIVE
Function to close all existing browsers
Imagine having the website you want to automate being open in more than one browser. RFT or any tool for that matter will have issues identifying and running scripts on one of the browsers. It is ideal to close all the browsers and invoke the browser of choice and the URL of choice and pursue testing.
Close all the existing browsers with the following code.

//Close Browsers if any are open
       void browser_Close()
      {
            // find browsers
            TestObject[] browsers = getRootTestObject().find(atChild(".class", "Html.HtmlBrowser"));

            // close one browser at a time
            for (TestObject browser:browsers) {
                ((BrowserTestObject) browser).close();
            }

            // unregister the test objects.
            // This step is important to avoid memory problems going forward.
            unregister(browsers);        
      }

Function to navigate to the desired browser and URL via DataPool
Add code for closing and invoking browsers and URL via Datapool.
      void ToPage()
      {
            setCurrentBrowser(dpString("Browser"));
      // Choose Browser via data sheet and set chosen browser as default.
            logInfo("Browser   = " + dpString("Browser"));
            logInfo("Environment   = " + dpString("Environment"));
            Browser().deleteCookies();
      // Delete cookies if required for your tests or comment this line out.
            startBrowser(dpString("URL")); // Choose URL
      }

Function to verify the website exists

      void Website()
      {
            sleep(5);
            if (Website().exists()) {     // Name "Website" can be replaced with any other name as per the website invoked
                  logInfo("Website Invoked", Website().getScreenSnapshot());
            } else {               
                  logWarning("Website not invoked, Unknown error", Browser().getScreenSnapshot());
                  stop();
            }

Call all functions together

public void testMain(Object[] args)
      {
            browser_Close();
            sleep(5);
            ToPage();
            Website();
      }

Final Script -
Final script should look more or less like this.

public void testMain(Object[] args)
      {
            browser_Close();
            sleep(5);
            ToPage();
            Website();
      }
void browser_Close()
      {
            // find browsers
            TestObject[] browsers = getRootTestObject().find(atChild(".class", "Html.HtmlBrowser"));

            // close one browser at a time
            for (TestObject browser:browsers) {
                ((BrowserTestObject) browser).close();
            }

            // unregister the test objects.
            // This step is important to avoid memory problems going forward.
            unregister(browsers);        
      }
void ToPage()
      {
            setCurrentBrowser(dpString("Browser"));
      // Choose Browser via data sheet and set chosen browser as default.
            logInfo("Browser   = " + dpString("Browser"));
            logInfo("Environment   = " + dpString("Environment"));
            Browser().deleteCookies();
      // Delete cookies if required for your tests or comment this line out.
            startBrowser(dpString("URL")); // Choose URL
      }
void Website()
      {
            sleep(5);
            if (Website().exists()) {     // Name "Website" can be replaced with any other name as per the website invoked
                  logInfo("Website Invoked", Website().getScreenSnapshot());
            } else {               
                  logWarning("Website not invoked, Unknown error", Browser().getScreenSnapshot());
                  stop();
            }
}
Step FINAL
The Script for invoking the desired Browser as well as URL is complete. Now, this script is ready to be used in combination with other proceeding test steps.
Open a new script and right click on the script - "Initial" and click on "Insert as "callScript"

Call more routines or call more steps in conjunction with the initial script.

Example 01 -

      public void testMain(Object[] args)
      {
            callScript("Initial");
            Website().waitForExistence();
            while (!dpDone())       {
                  sleep(5);                          
            Steps();   
                  dpNext();
            }          
      }
Example 02 -

      public void testMain(Object[] args)
      {
            callScript("Initial");
            callScript("Steps");
            }          
      }

Conclusion -
Now we have a base script to kick start the browser, navigate to webpage of choice without depending upon RFT settings. Now, take this method and make it your own.

No comments:

Post a Comment