Hi,
The below code extracts the branch number and the test environment to deploy that particular branches build on from a txt file (branch_dest.txt). Then it parses HTML pages to reach a webpage which contains some checkboxes. I want to check the "Select/Deselect All" checkbox and then click the "Deploy" button on that webpage.

How do i code this? I am basically from QA(Tester). So please help.

URL = "http://11.12.13.27:8080/cruisecontrol"

from urllib2 import urlopen
from HTMLParser import HTMLParser

import re

# Fetching links using HTMLParser
def get_links(url):
    parser = MyHTMLParser()
    parser.feed(urlopen(url).read())
    parser.close()
    return parser.links

# Build url for Deploy page
def get_deploy_url():
    url = URL + "/buildresults/xxx_%s_nightly_build" % branch
    print url
    for link in get_links(url):
        if link["href"].startswith("Deploy"):
            return "%s/%s" % (URL, link["href"])
        print link["href"]

# Build url for Destination page
def get_destination_url():
    url = get_deploy_url()
    print url
    destination_re = re.compile(r"%s" % destination)
    for link in get_links(url):
        if destination_re.search(link["href"]):
            return "http://11.12.13.27:8080/cruisecontrol/" + link["href"]

# Parsing HTML pages 
class MyHTMLParser(HTMLParser):
    def __init__(self, *args, **kwd):
        HTMLParser.__init__(self, *args, **kwd)
        self.links = []

    def handle_starttag(self, tag, attrs):
        if tag == "a":
            attrs = dict(attrs)
            if "href" in attrs:
                self.links.append(dict(attrs))

    def handle_endtag(self, tag):
        pass

if __name__ == "__main__":
    # Read the branch name and the test destination to deploy on
    lines = [x.split(':') for x in open("branch_dest.txt")]
    print lines
    branch = "%s" % lines[0][1].strip()
    print branch
    destination = "%s" % lines[1][1].strip()
    print destination
    
    final_url = get_destination_url()
    if final_url is None:
        print "Could not find a destination to deploy"
    else:
        print final_url

Recommended Answers

All 7 Replies

Hi,
Can anyone help me with this?

You must give the page html at least to check it out by somebody with experience in web pages (more than me).

Hi,

Below is the html of the web page.

<html>
<head>
	<title>Deploy Build</title> 
	<link type="text/css" rel="stylesheet" href="css/cruisecontrol.css"/>
	<style type="text/css">
		td.pad {
			padding: 2 5
		}

		.odd-row {
			background-color: white;
		}

		.even-row {
			background-color: #FFFFCC;
		}

		.header-row {
			background-color: white;
			color: darkblue;
		}
	</style>
	<script type="text/javascript">
		function setCheckboxes(checked)
		{
			var elements = document.getElementsByTagName('input');
			for( var i=0; i < elements.length; i++ )
			{
				if ( elements[i].type == 'checkbox' )
				{
					elements[i].checked = checked;
				}
			}
		}
	</script>

</head>

<body background="images/bluebg.gif">
	<h1 class="white" align="center">Select which clients to build</h1>

	<form name="cfg" action="DeployStart.jsp" method="POST">
		<input type="hidden" name="name" value="Test 4" />
		<input type="hidden" name="scriptPath" value="f:/Projects/PokerDeployer" />
		<input type="hidden" name="script" value="test4.cfg" />
		<input type="hidden" name="artifacts" value="f:/cruisecontrol/s_drive/artifacts/Poker-TTM_trunk_nightly_build/20100512230443" />
		<input type="hidden" name="configPath" value="f:/Projects/Poker-TTM_trunk_nightly_build/source/tools/PokerDeployer" />

	<table align="center" border="0" cellpadding="0" cellspacing="0" width="20%">
		<tr>
			<td bgcolor="#FFFFFF"><img border="0" src="images/bluestripestop.gif"/></td>
			<td bgcolor="#FFFFFF" align="right"><img border="0" src="images/bluestripestopright.gif"/></td>
		</tr>
		<tr>
			<td bgcolor="#DDDDDD" colspan="2" align="left" class="pad">
				<input type="checkbox" name="all_top" onClick="setCheckboxes(this.checked)"/> Select / deselect all<br>
			</td>
		</tr>

		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code" checked /> POK_code<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.SVS_code" checked /> SVS_code<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.PAT_code" /> PAT_code<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_gms" checked /> POK_code_gms<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_gms_cpn" checked /> POK_code_gms_cpn<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_mini" /> POK_code_mini<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.LTM_code" checked /> LTM_code<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.LTM_tot_code" /> LTM_tot_code<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_paradise" /> POK_code_paradise<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_forfun" /> POK_code_forfun<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_pokerheaven" /> POK_code_pokerheaven<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_totalpoker" /> POK_code_totalpoker<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_las" /> POK_code_las<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_italian" /> POK_code_italian<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_bulgarian" /> POK_code_bulgarian<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_finnish" /> POK_code_finnish<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_danish" /> POK_code_danish<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_french" /> POK_code_french<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_greek" /> POK_code_greek<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_german" /> POK_code_german<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_croatian" /> POK_code_croatian<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_polish" /> POK_code_polish<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_portuguese" /> POK_code_portuguese<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_romanian" /> POK_code_romanian<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_russian" /> POK_code_russian<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_slovak" /> POK_code_slovak<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_spanish" /> POK_code_spanish<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_czech" /> POK_code_czech<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_swedish" /> POK_code_swedish<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_hungarian" /> POK_code_hungarian<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_turkish" /> POK_code_turkish<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_albanian" /> POK_code_albanian<br>
			
			</td>
		</tr>
		

		<tr class="odd-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_austrian" /> POK_code_austrian<br>
			
			</td>
		</tr>
		

		<tr class="even-row ">
			<td colspan="2" align="left" class="pad">
			
				<input type="checkbox" name="client.POK_code_brazilian" /> POK_code_brazilian<br>
			
			</td>
		</tr>
		
		<tr>
			<td bgcolor="#DDDDDD" colspan="2" align="left" class="pad">
				<input type="checkbox" name="all" onClick="setCheckboxes(this.checked)"/> Select / deselect all<br>
			</td>
		</tr>
		<tr>
			<td bgcolor="#FFFFFF" colspan="2" align="center" class="pad">
				<input type="submit" value="Deploy" />
			</td>
		</tr>
		<tr>
			<td bgcolor="#FFFFFF"><img border="0" src="images/bluestripesbottom.gif"/></td>
			<td align="right" bgcolor="#FFFFFF"><img border="0" src="images/bluestripesbottomright.gif"/></td>
		</tr>
	</table>

	</form>
</body>
</html>

Here i need to check the Select/Deselect All checkbox and then click the Deploy button. How do i simulate this?

Looks like you need to do own version in python from this

function setCheckboxes(checked)
		{
			var elements = document.getElementsByTagName('input');
			for( var i=0; i < elements.length; i++ )
			{
				if ( elements[i].type == 'checkbox' )
				{
					elements[i].checked = checked;
				}
			}
		}

Which mean basically to set the status for all elements checked.

Hmm..

<form name="cfg" action="DeployStart.jsp" method="POST">
		<input type="hidden" name="name" value="Test 4" />
		<input type="hidden" name="scriptPath" value="f:/Projects/PokerDeployer" />
		<input type="hidden" name="script" value="test4.cfg" />
		<input type="hidden" name="artifacts" value="f:/cruisecontrol/s_drive/artifacts/Poker-TTM_trunk_nightly_build/20100512230443" />
		<input type="hidden" name="configPath" value="f:/Projects/Poker-TTM_trunk_nightly_build/source/tools/PokerDeployer" />

So this DeployStart.jsp need one POST method to send...

Let's see what specialists suggest, I have not enough experience in this, but I am willing to learn.

Hey Tony, thanks! I have sent a PM to the submitter. Hope he replies back.

try this

import urllib, urllib2, cookielib

cj = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

opener.addheaders.append(('User-agent', 'Mozilla/4.0'))
opener.addheaders.append( ('Referer', 'url to page') )


login_data = urllib.urlencode({'all_top' : 'setCheckboxes(this.checked),
                                'submit' : 'Deploy'})

resp = opener.open('url to page', login_data)
#f = open('test.html','w')
#f.write(f.read())
#f.close()
resp.close()

resp is the response after the button is pushed. Un-comment the code at the bottem to create a html page of the reponse to see if it worked or not.

The cookie handling may not be needed.

Also I noticed in the html that the button labeled "Deploy" doesn't have a name in the tag. I.E. <input type="submit" name="Name_of_button" value="Deploy" />
That name value is what should be in the login_data dictinary. This may not have an effect; just not so good html coding.

I can't really test this because I don't know the website hosting the page; that is if its not on only a local LAN.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.