I want to automate some tests on my webpage, mainly filling out forms with invalid data and see what happens. Is is possible with Javascript? If not, is it possible with PHP? The script should handle sessions and cookies.
Here is a very simple example that has 3 pages. In the first you should enter 'x' and click submit, in the second you should enter '3' and click submit. If that was successful the last page should show you "you got it!". Can that be automated so that I load a script and see the "you got it" right away? Please note: This has no cookies\sessions for simplicity, but I want an answer that dose support those.
I have tried making another page with iframe that includes the site above. But could not gain access to the elements inside the iframe
I have tried making an PHP script using cURL, that sends requests, but I could not forward cookies.
I have posted an comment on this answer but didn't get a reply.
For your convenience, here is the code: (you don't really need it, but just in case..)
index.html
<!DOCTYPE html>
<html>
First page: please enter x
<form method="post" action="next.php">
<input type="text" id="id" name="id" />
<input type="submit" />
</form>
<html>
next.php
<?php
if(isset($_POST) && isset($_POST['id']) && $_POST['id']!='x'){
echo '<script>window.location = "http://www.cs.bgu.ac.il/~kahilm/myNavigator/";</script>';
}
?>
<!DOCTYPE html>
<html>
Second page: please enter 3
<form method="POST" action="last.php">
<input type="text" name="code" />
<input type="submit" />
</form>
</html>
last.php
<?php
if(isset($_POST) && isset($_POST['code']) && $_POST['code']=='3'){
echo 'you got it!';
}else{
echo 'you sent something, please make it a "3"... :)';
}
?>
Consider the Selenium browser automation framework - it allows you to navigate through pages, verify conditions, fill in forms. Very stable, very mature.
http://seleniumhq.org/
You should look at programatically controlling a browser to perform this type of test. Selenium is a popular option, and has PHP bindings mentioned in the documentation (although I usually use Perl or Python).
PHP-based solution: Won't test from the front-end, only the server-backend, hence won't emulate real input.
JS: Will not persist across pages.
Hence, you are either looking for a browser-extension or a standalone utility separate from the browser entirely.
You could try:
Sikuli which however is generic, not targeted at web-pages, but at GUIs in general.
WatiN or Selenium
Related
I have a script that pulls an XML page and uses a form to update and save the values back. When I click the submit button it works, but then the page loads blank. I just want the page to refresh. There are about 100 different threads on this, and nothing I have tried has worked to resolve the issue. Out of curiosity, I just tried to run the window.location script and nothing else, and this piece actually doesn't work at all.
<?php
//if (isset($_POST['submit'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$ctstatus->nodeValue = $_POST['ctstatusform'];
//htmlentities($xml->save('test.xml'));
echo '<script type="text/javascript">window.location="http://google.ca";</script>';
}
?>
The inner contents of the form don't really matter at this point, I just want it to refresh the page after I hit the submit button.
I previously used isset but from reading it seems like that's obsolete, and my form action="" used to be blank. Either way my XML save works, but nothing to refresh the page. I also tried header and that didn't work either.
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input class="save" name="submit" type="submit" value="Save" />
</form>
Out of curiosity I tried an onClick function with a timer and this does work but it's not ideal at all, especially because the page could technically refresh before the POST is finished writing the file. I'd rather know why the echo doesn't execute.
PHP redirect would most likely be preferable to JavaScript redirect.
Typical structure when posting back to same page:
<?php // cannot be any output before this (space, linefeed, etc)
if(isset($_POST['submit']) {
// do stuff with the submission
header('Location: http://google.ca');
exit;
}
// does your script need to do some other data retrieval or calculation? do it here.
?>
<html>
... snip ...
<form method="post">
... snip ...
<input class="save" name="submit" type="submit" value="Save" />
</form>
Following this simple structure for procedural scripts--
Deal with user input / redirect
Do logic (collect, manipulate data)
Show output (using php only to insert variables and looping)
will help you avoid a lot of heartache and technical debt.
Okay, I have gotten this sorted out. It turns out that the problem was embarrassingly simple, but maybe will assist someone in the future. Along with reordering my code, as Tim suggested. I specified HTML as the DOCTYPE, and that worked to resolve the issue. I no longer need to worry about refreshing the page after submit, because it refreshes as it should automatically. Thank you to everyone who commented.
I have a landing page called `index.php' with the following form:
<form action="auto_mail.php" method="post">
<input id="signup" class="span8" type="text" placeholder="Your email" name="signup">
<input type="submit">
<?php
if (isset($_SESSION['got_it']))
{echo "<b>You're all signed up!</b>}
?></form>
In the file auto_mail.php I have:
// code scrubbing user input...
$user_email = $_POST['signup'];
session_start();
$_SESSION['got_it'] = '1';
// code sending me an email when someone signs up.
echo <<<EOD
</b>
<meta http-equiv="refresh" content="0, url=index.php">
</div>
</body>
</html>
EOD;
?>
I've looked at some other SO questions (Using $_SESSION to carry data), but it's not what I'm looking for.
All I want, is for a user to see "You're all signed up" when they enter a valid email; with the email confirm email being sent in the background. This code feels clumsy and awkward. It also flashes the auto_mail.php page briefly.
I tried to set <form action="index.php"..., but it doesn't work because I've set up auto_mail.php such that you can't access it directly.
How can use the code in auto_mail.php, which checks for a valid email address and sends confirm emails, without dealing with both $_POST and $_SESSION, or at least using them better?
If you don't want to have any page reloads whatsoever, you'll have to use AJAX to send the form, instead of utilising the form POST.
If you are using jQuery, or Mootools, they both have built in wrappers to handle ajax calls. Without a helper library, you'll have to look into making an XMLHttpRequest yourself.
Other than that, traditionally, you would redirect the user to a "form submitted" page, or alternatively, have the form action be sent to the same page (in your case, index.php, and have PHP code to handle form data if it is received).
I dont get completely what you want.
I think you try to Verify a Mail Address (after?) that form has been sent. But you cannot access the file via http that does the verification.
Have you thought about including the auto_mail.php?
I think you should consider using one of popular PHP frameworks. I guess you didn't use any in above example. Good framework that also offers MVC structure allows to do operations like this in such a simple way you can't even imagine.
Breaking it down to MVC structure will even make it extremely simple to handle post sending and displaying dependences and results made by it in one action.
Learing good framework at first might look like a waste of time, but believe me - it will pay off very quickly.
For start I recommend you looking at Kohana Framework or, if you're ambitions one - Symfony Framework.
In one site, www.example.com, I have a form. It is processed and stored in its database.
Is it possible to send this form data also to another site (www.client-site.com)? It is located on a different server. And this client-site should receive the data and store it on its own database.
I am not sure of the terminology and what should I've been looking for.
Tested different search queries here in SO and this is what most resembles it:
[php] [mysql] +form +"$_post" +external
I'd like to develop this with PHP and the databases run MySQL, and surely security is important in this data transaction.
And hoping this is not a SOAP matter... ;)
Justification:
www.example.com runs a mini site of one of my clients
www.client-site.com is the client main site
being a large company, they cannot provide me credentials to their main site database
I have to propose an alternate solution
You will need to have something like a webservice (this is the word you are looking for) on site b which you can use from the code within your site a.
SOAP is one possibility to create a webservice, but there are many other possibilities. One is shown in this answer on stackoverflow.
NEVER EVER try to archiv this using forms and something like cURL!
Further you should look for proper authorization on your endpoint (site b) and ensure that ssl is used, as security is important to you.
If you have a form hosted on www.example.com like this:
<form method="post" action="http://www.client-site.com/handler.php">
...
Then the page http://www.client-site.com/handler.php will be able to access the post variables.
This is why it is important that you validate your post data in your own PHP applications as you can never be sure where the data is coming from and therefore cannot trust it.
I can think of a very ugly solution wich will do the trick :)
<script language="JavaScript">
function submitForm(theform) {
theform.action = "SITE ONE";
theform.target="myframe1";
theform.submit();
theform.target="";
theform.action = "SITE2";
theform.submit();
}
</script>
<html>
<body>
<form action="" onSubmit="submitForm(this); return false;">
<input type="text" name="userName" value="" />
<input type="Submit" />
</form>
<IFRAME name="myframe1" src="about:blank" width="0"
height="0"></IFRAME>
</body>
</html>
This is not so conventional but will do the trick !
check it out :)
I have a simple form that posts signup info to a mailchimp account. The form and messages back from the include file work perfectly in Firefox, but nothing is happening in IE. If no email address is entered, it should come back with an error just like it does in Firefox. I have no idea how to trouble shoot this type of issue. If there is an error, it should echo back the error into a div on the form which again works perfectly in Firefox and nothing happens in IE. It makes no sense to me. I need to have this working by today. I have tried moving the files to the same location as the index.php, changed permissions, etc. with no luck. I need some major help! I can't even get this piece of code to come back in IE: if(!$_GET['email']){ return "No email address provided"; }
You can try the form at: www.terrybryan.com/index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Ajax Mailing List Sign Up System</title>
<link type="text/css" rel="stylesheet" href="css/default.css" />
</head>
<body>
<form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get" style="width:250px;">
<fieldset>
<label for="email" id="email-label">Email Address</label>
<input type="text" name="email" id="email" />
<label for="zipcode" id="zipcode-label">Zip Code</label>
<input type="text" name="zipcode" id="zip" />
<label for="events" id="events-label">Receive future info from us?</label>
Yes<input type="radio" name="events" value="Yes" checked="checked" />
No<input type="radio" name="events" value="No" />
<label for="hearabout" id="hearabout-label">How did you hear about us?</label>
<select name="hearabout" id="hearabout">
<option value="Ice">Ice</option>
<option value="Radio">Radio</option>
<option value="Friend">Friend</option>
<option value="Door Hanger">Door Hanger</option>
</select>
<br /><br />
<input type="image" src="i/join.jpg" name="submit" value="Join" class="btn" alt="Join" />
<br /><br />
<div id="response">
<? require_once 'store-address.php'; if($_GET['submit']){ echo storeAddress(); } ?>
</div>
</fieldset>
</form>
<!-- Some fancy Ajax stuff to store the sign up info. If you don't want to use it, just delete it and the form will still work -->
</body>
</html>
and here is the include file that the message comes back from:
<?php
/*///////////////////////////////////////////////////////////////////////
Part of the code from the book
Building Findable Websites: Web Standards, SEO, and Beyond
by Aarron Walter (aarron#buildingfindablewebsites.com)
http://buildingfindablewebsites.com
Distrbuted under Creative Commons license
http://creativecommons.org/licenses/by-sa/3.0/us/
///////////////////////////////////////////////////////////////////////*/
require_once('MCAPI.class.php');
function storeAddress(){
// Validation
if(!$_GET['email']){ return "No email address provided"; }
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
return "Email address is invalid";
}
// grab an API Key from http://admin.mailchimp.com/account/api/
$api = new MCAPI('********');
// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
// Click the "settings" link for the list - the Unique Id is at the bottom of that page.
$list_id = "********";
// Merge variables are the names of all of the fields your mailing list accepts
// Ex: first name is by default FNAME
// You can define the names of each merge variable in Lists > click the desired list > list settings > Merge tags for personalization
// Pass merge values to the API in an array as follows
$mergeVars = array('ZIPCODE'=>$_GET['zipcode'],
'EVENTS'=>$_GET['events'],
'HEARABOUT'=>$_GET['hearabout']);
if($api->listSubscribe($list_id, $_GET['email'], $mergeVars) === true) {
// It worked!
return 'Success! Check your email to confirm sign up.';
}else{
// An error ocurred, return error message
return 'Error: ' . $api->errorMessage;
}
}
// If being called via ajax, autorun the function
//if($_GET['ajax']){ echo storeAddress(); }
?>
Does anyone have any idea why this simple form would not work in IE like it does in Firefox and other browsers?
Thanks,
T Bryan
Here's why: When using an image button in IE, when you click it, instead of sending $_POST['nameofbutton'], it sends $_POST['nameofbutton_x'] and $_POST['nameofbutton_y']. Now, Firefox sends both $_POST['nameofbutton_x'] and $_POST['nameofbutton_y'] as well as $_POST['nameofbutton']. The _x and _y values store the x and y coordinates of where you clicked on the image button, so you can do different things depending on where the user clicked for doing something like an image map.
So, when you test for if($_GET['submit']){ echo storeAddress(); } ?> you don't get it on IE because IE doesn't have $_GET['submit'] it has $_GET['submit_x'] and $_GET['submit_y'], so you must test for $_GET['submit_x'] or y.
Least I think the format was _x and _y, might be slightly different. Easy way to tell is to print_r($_GET);
Oh, also, by the way, be careful using Get for forms, particularly if they have to transfer a lot of potentially long information, IE has like a 255 character limit in the address bar and anything after that will get cut off and left out of $_GET or malformed. Best to use Post for forms. Get is only really good for things like structuring links for a CMS controller or registration email verification links and all that.
I suggest editing your html code for submit:
From:
<input type="image" src="i/join.jpg" name="submit" value="Join" class="btn" alt="Join" />
To:
<input type="submit" name="submit" value="Join" class="btn" alt="Join" />
type submit is generally reliable, other types (including, sadly, , image submits, etc) are often implemented differently in different browsers. Another thing not to rely on is the contents of submit, since ie in some cases submits html instead of the value (generally with things other than the standard submit value), or submit multiple forms on the page, annoying things like that.
So in general, it is best to go with a simple <input type='submit' ...etc, and go with a simple check for the -presence- of the submit and don't be too specific about what the submit contains.
I would suggest a very simple approach, style your button using CSS, or do <img src="path/to/your/image" alt="my image button" />
Since PHP runs on the server, it should (generally) provide the same response to the browser for the same input. Since the form uses the "get" method, the input is entirely specified by the URL (plus headers, including cookies, though those don't appear to factor in here).
In any case, when you submit the form from Firefox, you should see that the URL of the form submission appears in the Firefox address bar. If the PHP script issues a redirect as a response, then the URL may only appear very briefly. Either way, it should be available in your browser history.
If you can copy that URL into the address bar of IE and submit the same request from IE (again, assuming cookies aren't involved), then you should get pretty much the same response from the server. If you're not seeing the same thing in the browser window of IE as you do in Firefox, there may be several reasons.
View the source. (View -> View Source). If it's completely blank, the script probably failed to execute properly.
If you have access to the server error logs, check for an error message related to your request. A sane PHP implementation (in cooperation with a sane server) will provide error logging. Sometimes this is really the only way to know what's going on with your script. If you have access to the web server through an FTP account, you will probably find the logs at or near the top level.
If you don't have access to the server, you can still get a low-level view of what's happening with your request through the use of Firebug Lite or Fiddler2.
I am using php, js, flash and mysql on 1 website.
I want to do a URL masking using frameset(or maybe iframe). Scenario:
An user click on a link, which direct him/her to my page with this url:
www.domain.com/index.php?var1=string1&var2=string2
How to mask the url so that visitor can only see www.domain.com/index.php, but actually there are some variables over there. I need the variables, but i dont want the visitors to see. How to do URL masking on this? (I dont expect to get any code, I just want to know the logic of the url masking method)
PS. I probably would not use mod_rewrite, because I dont know how to use/write the code. So please, answer with iframe/frameset methods :)
EDIT: I think I misunderstood your question, so here is another attempt:
In www.yourdomain.com/index.php:
<?php
session_start();
if (isset($_REQUEST['flashvar']) && ! isset($_SESSION['flashvar'])) {
// Store any parameters received
$_SESSION['flashvar'] = $_REQUEST['flashvar'];
// Redirecting without query parameters
header('Location: /index.php');
exit;
}
?>
<HTML>
<HEAD></HEAD>
<BODY>
<?php
echo '<embed src="player.swf?flashvar=',
urlencode($_SESSION['flashvar']), '"/>';
?>
</BODY>
</HTML>
This example will start a session and redirect the user to itself without needing to store any parameters in the query string. Naturally, it will only work if the user has cookies enabled.
Can you submit that parameters as POST data?
For example:
<form name="form1" action="index.php" method="POST">
<input type="hidden" name="var1" value="value1" />
<input type="hidden" name="var2" value="value2" />
</form>
Click me
When user clicks on the link, the form will be submitted to index.php with POST parameters var1 and var2. User will never see this parameters in their URL (still possible to see with various tools though).