Wednesday, April 25, 2012

Gambling Automation - Site Log in

01 UPDATE USER INTERFACE: add button, change text to 'login', change name to 'buttonlogin'
02 CREATE CODE FOR LOGIN:   Click Project-> add class.... , add new class called AutoSports.cs to put the automation methods.
create a login method in it, cut the Browser line created in last post in Form1.cs into AutoSports class, import 3 required packages, add the try block in the new created login method, this will prevent any crash if the login process goes wrong,  add writeLine in the catch block, so if things go wrong you will be informed.

open your ie8, go to http://www.sportsbet.com.au/, and press F12 to bring the developer tool, click the top left arrow and then click user name input box, this will bring you the code behind it, Check the element id is 'username', same you can find id for password box is 'password', and login-button's id is'login-button'.

add the following code into method login, I change browser object name to br, to save typing time. the if sentence is to check if being logged in already. the following lines are straightforward, fill in the info and click the button

go to form1.design change the password textbox to shown stars

create AutoSports object from the AutoSports class we just created in Form1.cs

double click Login button will bring you the code block, fill in the function you want the program to do after you click that button, here we want the program to DO log in


 03 TEST LOGIN: press F5, or click Debug->Start Debugging to try out, you should be able to see the following after click the log in button if you have an account with that site.

05 CODE:
*****************************
AUTOSPORTS.CS
*****************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using NUnit.Framework;
using System.Xml.Linq;
namespace sportsbetAuto
{
    class AutoSports
    {
        Browser br = new IE("http://www.sportsbet.com.au/");
        public bool login(string id, string pw)
        {
            try {
                if (!br.Element(Find.ById("log-out")).Exists)//do not login if already logged in
                {  
                    br.Element(Find.ById("username")).SetAttributeValue("value",id);
                    br.Element(Find.ById("password")).SetAttributeValue("value", pw);
                    br.Element(Find.ById("login-button")).Click();
                }
                return true; }
            catch (Exception e){
                System.Console.WriteLine("ERROR! Login Failed!");
                return false;
            }
        }
    }
}
****************
FORM1.CS
***************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WatiN.Core;
using NUnit.Framework;
using System.Xml.Linq;

namespace sportsbetAuto
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        AutoSports autos = new AutoSports();
        public Form1()
        {
            InitializeComponent();
        }
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            autos.login(textBoxLoginID.Text, textBoxPassword.Text);
        }
    }
}


2 comments:

  1. This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it, situs bandarq

    ReplyDelete
  2. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! esc online

    ReplyDelete