LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-24-2012, 12:35 PM   #1
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Rep: Reputation: 30
Question How to trigger a javascript in HTML file through a bash script?


Hello there,

I need to write a shell script that triggers a javascript embedded in a separate HTML file. Please help me.

Best Regards
 
Old 04-25-2012, 07:51 AM   #2
FNC
Member
 
Registered: Feb 2001
Location: South Africa
Distribution: Fedora, Mandriva, PCLOS, SUSE - anything a can get my hands on
Posts: 140

Rep: Reputation: 15
View the html source, get the javascript and run it in the shell script.
 
Old 04-26-2012, 10:20 AM   #3
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks FNC, I do appreciate you response.
Here, in fact, I need to provide the scenario.
I have an HTML page, sections of which are :

Code:
...
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function validat() 
{
	if (document.form1.name.value=="" || document.form1.name.value=="0" || document.form1.name.value=="1") 
	{
		alert("Please enter your Roll No.");
		return false;
	}
	else
	{       return true;   	}
}
</SCRIPT> </HEAD>
...
<BODY>
...
<form name="form1" method="post" action="Result.asp" onSubmit="return validat();">
<input name="RollNo" type="text" id="RollNo">
<input name="Submit" type="submit" value=" Search ">
</form>
...
Clearly, the page:
  • has an input text field (for RollNo) and a 'search' button
  • when 'search' button is clicked validate() javascript is triggered.
  • If the input (RollNo) was not empty, neither a zero nor a one, then this input is passed to Result.asp (serverside script)
  • Result.asp returns a page containing Result of the RollNo passed to it.

What I want

I have to get the results of many or all. I know range of RollNos (suppose from 1 to 100). Doing this manually will be hard. I want to automate the process through a bash script in Linux.
Here is my approach for this:

In a shell script run a loop from i=1 to 1=100
For each 'i' do this:
  • Change:
    Code:
    <input name="RollNo" type="text" id="RollNo"> to <input name="RollNo" value = "i" type="text" id="RollNo">
    [value attribute is added with RollNo]
  • Trigger validate() script to submit the form.
  • Use wget (or something like that) to save downloaded page (that contains result) in temp.html
  • Use awk (or something like that) to append the result to AllResults.txt
  • Change i to i+1

My problem is how to trigger this validate() script, that in a separate HTML file through a bash script.

Best Regards

Last edited by mq15; 04-26-2012 at 10:21 AM.
 
Old 04-26-2012, 05:51 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,422
Blog Entries: 33

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Sounds to me like you want to "emulate" a fully functioning web browser and automate the "driving of input". As far as I know the only way you can do that is by using an actual browser which renders the DOM for a user to see. To automate that you would need something to automate controlling a real browser such as selenium-rc.

There are JS interpreters and library bindings out there such as the JavaScript module for Perl and the python-spidermonkey library for Python. But they are both bindings for SpiderMonkey and only run pure JS which isn't accessing DOM.

What you want to do can't be accomplished simply in a shell language like bash. You need to turn to more powerful languages such as Python or Perl to accomplish it. And in your case, an actual browser to automate. If you're attempting to do something like this in a headless environment then I don't believe it can be done with currently existing tools, not that I've seen anyway. If you figure it out how to do it headless then I'd be interested in seeing how you go about it.

Another way to go about it would be to access the submission URL with all possible valid inputs from the web form and analyze the return code or html output. This way you avoid the JavaScript. If the submission returns a proper http response code (i.e. 200 for success) then something like curl can be used in a bash script with the test utility. However if you have to interpret the HTML on the other end then you'll need to turn to something more powerful like Python or Perl. That's assuming you can build the URL yourself without requiring a session ID. Or on the server side make yourself a fake, valid, session ID in which you can test input. In that case I am assuming you're testing an application you wrote, or have access to the developers for the testing.

SAM

Last edited by sag47; 04-26-2012 at 06:00 PM.
 
Old 04-27-2012, 12:58 PM   #5
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Okay. Thanks a lot sag47.
Now while I am testing selenium-rc, how much time it will take to get hands on it, is there any other suggestion by someone else ??

Best regards.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script similar to javascript var.substring(start,end) piman Programming 3 09-02-2011 09:17 PM
[SOLVED] bash script to dynamically edit an html file melee Programming 24 04-17-2010 01:53 PM
Trigger bash script when screen saver starts of desktop gets locked BinWondrin Programming 4 08-01-2008 03:40 AM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
Can I get an incoming email to trigger a script file? davee Linux - General 2 10-16-2003 02:45 PM


All times are GMT -5. The time now is 08:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration