OWASP O2 Platform Blog

Creating an API for JPetStore Browser automation

Once we have a number of Lambda functions to perform IE/Web automation, the next step is to create an API so that they can be easily consumed and reused.

This API is going to be called API_JPetStore.cs and will allow the simple invocation of JPetStore commands like this:

var topPanel = panel.clear().add_Panel();
var ie = topPanel.add_IE().silent(true); 
var jPetStore = new API_JPetStore(ie);
 
jPetStore.homePage(); 
jPetStore.login_DefaultValues();
jPetStore.logout();
jPetStore.login("asd","asd"); 
jPetStore.login("j2ee","j2ee");</pre>
&nbsp;

//O2File:API_JPetStore.cs
//O2File:WatiN_IE_ExtensionMethods.cs
//using O2.XRules.Database.Utils.O2
//O2Ref:WatiN.Core.1x.dll

Here is the current source code for the API_JPetStore.cs file (at the moment only implemeting a couple functions (like the login and logout)

// This file is part of the OWASP O2 Platform (<a href="http://www.owasp.org/index.php/OWASP_O2_Platform">http://www.owasp.org/index.php/OWASP_O2_Platform</a>) and is released under the Apache 2.0 License (<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>)
using System;
using System.Linq;
using System.Collections.Generic;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.DotNet;
using O2.DotNetWrappers.ExtensionMethods;
//O2File:WatiN_IE_ExtensionMethods.cs   
//O2File:WatiN_IE.cs
//O2Ref:WatiN.Core.1x.dll</pre>
&nbsp;

namespace O2.XRules.Database.APIs
{
    public class API_JPetStore
    {   
        public WatiN_IE ie;
        public string appUrl = "<a href="http://localhost:8080/jpetstore">http://localhost:8080/jpetstore</a>";
       
        public API_JPetStore(WatiN_IE _ie)
        {
            ie = _ie;
        }           
       
        public API_JPetStore open(string virtualPath)
        {
            if (virtualPath.starts("/").isFalse())
                virtualPath = "/{0}".format(virtualPath);
            var fullUri = "{0}{1}".format(appUrl, virtualPath).uri();
            ie.open(fullUri.str());
            return this;
        }
    }
   
      
    public static class API_JPetStore_ExtensionMethods
    {
        public static API_JPetStore homePage(this API_JPetStore jPetStore)       
        {           
            jPetStore.open("");
            return jPetStore;
        }
       
        public static API_JPetStore login_DefaultValues(this API_JPetStore jPetStore)       
        {           
            jPetStore.open("/shop/signonForm.do");
            jPetStore.ie.buttons()[1].click();
            return jPetStore;
        }
       
        public static bool login(this API_JPetStore jPetStore, string username, string password)       
        {           
            jPetStore.open("/shop/signonForm.do");
            var ie = jPetStore.ie;
            ie.field("username").value(username);
            ie.field("password").value(password);
            jPetStore.ie.buttons()[1].click();
            return ie.IE.Html.contains("Invalid username or password. Signon failed.");
        }
       
        public static API_JPetStore logout(this API_JPetStore jPetStore)       
        {           
            jPetStore.open("/shop/signoff.do");            
            return jPetStore;
        }

       
    }
}

 

July 13, 2011 - Posted by | JPetStore, Spring MVC, WatiN

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: