PROVISIO DevBlog

Using the SiteKiosk Object Model for Automated Website Logins

In case you want SiteKiosk to automatically login to a website with a specific user and you can't use cookies for that, because you need to delete them regularly for security reasons, you can use the SiteKiosk Object Model to help you with the task. The SiteKiosk Object Model enables you to edit the Document Object Model (DOM) of the page, to automatically fill in the required user data and start the login process.

The example will use PROVISIO's own remote monitoring page www.siteremote.net to demonstrate the automated login.

Unless you are already familiar with the code of the page you want to use you can utilize the developer tools of any modern browser (e.g. Chrome, Firefox or IE) to view the code of the webpage and find out about the fields for user name, password and the way how to submit that data. Using the developer tools on the SiteRemote login page (https://www.siteremote.net/pub/login.aspx) shows us that the id of the user field is UserNameEdt, the id of the password field is PasswordEdt. There are different methods the login can be coded on a webpage, here it is a div with the id LoginBtn2 that does a callback, so we just use the Javascript click method to simulate a click on that div. If your login page uses a form element, you can usually call the submit method of that form instead of using the click method, that we use in our example.

Now that we got the required information about the DOM elements of the page we want to login to, we can create our script.

//Flag used to prevent numerous login attempts, you may reset the flag using an event like OnScreenSaverBegin
var loginattempt = false;

//Message event used to detect navigation to login page
SiteKiosk.Logfile.OnMessage = OnMessage;
function OnMessage(seq, time, utcoff, awtype, awlevel, facility, text)
{
	//Check the SiteKiosk logs if the login page has been called and then call the function to attempt the automated login
    if(text.indexOf("Navigation:") !== -1 
	&& text.indexOf("www.siteremote.net") !== -1 
	&& loginattempt === false){
        //Delays the automatic login to give the page time to load
		evtid = SiteKiosk.Scheduler.AddDelayedEvent(1500, AutomaticLogin);
		loginattempt = true;
	}
}
 
function AutomaticLogin()
{
	//Go through the open windows to find the browser window with the login page
    for (var i=1;i<=SiteKiosk.WindowList.Windows.Count;i++)
    {
        try
        {
			//Make sure the window is a SiteKiosk browser window and it displays the login page
            if((SiteKiosk.WindowList.Windows(i).WindowType === 1 || SiteKiosk.WindowList.Windows(i).WindowType === 2)
			&& SiteKiosk.WindowList.Windows(i).SiteKioskWindow.SiteKioskWebBrowser.WebBrowser.LocationURL.indexOf("https://www.siteremote.net/pub/login.aspx") !== -1)
            {
                SiteKiosk.WindowList.Windows(i).SiteKioskWindow.SiteKioskWebBrowser.WebBrowser.Document.getElementById('UserNameEdt').value = "username";
                SiteKiosk.WindowList.Windows(i).SiteKioskWindow.SiteKioskWebBrowser.WebBrowser.Document.getElementById('PasswordEdt').value = "password";
				SiteKiosk.WindowList.Windows(i).SiteKioskWindow.SiteKioskWebBrowser.WebBrowser.Document.getElementById('LoginBtn2').click();
            }
        }
        catch(e)
        {
			//Debug
            SiteKiosk.Logfile.Notification("Automated login failed for window: " + SiteKiosk.WindowList.Windows(i).ItemText);
        }
    }
}

The script uses a variable (loginattempt) as a flag to prevent more than one login attempt. You may use a SiteKiosk Object Model event like OnScreenSaverBegin or OnSessionEnd to reset the flag to allow for new login attempts.

The OnMessage event helps to detect that the login page has been visited in the SiteKiosk browser. Using the AddDelayedEvent method to give the page time to load, we call the AutomaticLogin method of the script to start the login process.

Within the AutomaticLogin method we use the WindowList object's Windows collection Count property to go through all open windows to identify all open SiteKiosk browser windows (WindowType 1 or 2) to find the one, which displays the SiteRemote login page (https://www.siteremote.net/pub/login.aspx). Then we edit the DOM of the page and fill the user name and password fields with the required values and click the login button.

Save the above example as a .js file in the ..\SiteKiosk\html folder. Open the configuration, go to Start Page & Browser, select the Internet Explorer engine and use for example the Metro IE Skin. On the same configuration page click on Advanced and then add the .js file you created as an external script.

Start the SiteKiosk browser and go to www.siteremote.net. An automatic login attempt by the script will occur. If you provided valid credentials as username and password, you will login automatically to your SiteRemote team, otherwise you will recieve an error message about invalid credentials.

For obvious reasons the code to identify the URL and the code to edit the required DOM elements will vary depending on the URL and code of the login page you need to use, please change these code parts accordingly.