Making a POST request in Selenium without filling a form? - forms

I have an application A that should handle a form submit made with POST method. The actual form, that initiates the request, is in totally separate application B. I am testing application A using Selenium, and I like to write a test case for form submit handling.
How to do this? Can this be done in Selenium at all? Application A does not have a form that can initiate this request.
Note, that the request must use POST, otherwise I could just use WebDriver.get(url) method.

With selenium you can execute arbitrary Javascript including programmatically submit a form.
Simplest JS execution with Selenium Java:
if (driver instanceof JavascriptExecutor) {
System.out.println(((JavascriptExecutor) driver).executeScript("prompt('enter text...');"));
}
and with Javascript you can create a POST request, set the required parameters and HTTP headers, and submit it.
// Javascript example of a POST request
var xhr = new XMLHttpRequest();
// false as 3rd argument will forces synchronous processing
xhr.open('POST', 'http://httpbin.org/post', false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('login=test&password=test');
alert(xhr.response);
In modern bleeding edge browsers you can also use fetch().
If you need to pass over to selenium the response text then instead of alert(this.responseText) use return this.responseText or return this.response and assign to a variable the result of execute_script (or execute_async_script) (if using python). For java that will be executeScript() or executeAsyncScript() correspondingly.
Here is a full example for python:
from selenium import webdriver
driver = webdriver.Chrome()
js = '''var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://httpbin.org/post', false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('login=test&password=test');
return xhr.response;'''
result = driver.execute_script(js);
result will contain the return value of your JavaScript provided that the js code is synchronous. Setting false as the third argument to xhr.open(..) forces the request to be synchronous. Setting the 3rd arg to true or omitting it will make the request asynchronous.
❗️ If you are calling asynchronous js code then make sure that instead of execute_script you use execute_async_script or otherwise the call won't return anything!
NOTE: If you need to pass string arguments to the javascript make sure you always escape them using json.dumps(myString) or otherwise your js will break when the string contains single or double quotes or other tricky characters.

I don't think that's possible using Selenium. There isn't a way to create a POST request out of nothing using a web browser, and Selenium works by manipulating web browsers. I'd suggest you use a HTTP library to send the POST request instead, and run that alongside your Selenium tests. (What language/testing framework are you using?)

The easiest way I found is making an intermediary page solely for the purposes of submitting a POST request. Have selenium open the page, submit the form, and then get the source of the final page.
from selenium import webdriver
html='<html><head><title>test</title></head><body><form action="yoursite.com/postlocation" method="post" id="formid"><input type="hidden" name="firstName" id="firstName" value="Bob"><input type="hidden" name="lastName" id="lastName" value="Boberson"><input type="submit" id="inputbox"></form></body></html>'
htmlfile='/tmp/temp.html'
try:
with open(htmlfile, "w") as text_file:
text_file.write(html)
except:
print('Unable to create temporary HTML file')
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.get('file://'+htmlfile)
driver.find_element_by_id('inputbox').click();
#wait for form to submit and finish loading page
wait = WebDriverWait(driver, 30)
response=driver.page_source

Related

How to hit an API in Selenium IDE

I'm testing front end using Selenium IDE, and I want to hit some api to generate test data such as Test Email address and password, before actually starting test.
I want to first hit API and store it's response into some variable which I can use it later in UI flow.
I've found solution like this:-
Use command "Execute Script" to execute Javascript code under currently opened Browser window. Here is what documentation says:
Executes a snippet of JavaScript in the context of the currently selected frame or window. The script fragment will be executed as the body of an anonymous function. To store the return value, use the 'return' keyword and provide a variable name in the value input field.
Arguments:
script - The JavaScript snippet to run.
variable name - The name of a variable without brackets."
Hence, in order to hit some api, we can write code like this:-
var request = new XMLHttpRequest();
request.open('GET', 'https://google.com', false);
request.send(null);
return request.responseText;

issue capturing the hashed URI parameters in Coldfusion [duplicate]

I have such url - http://www.coolsite.com/daily-plan/#id=1
What the easiest way to parse that string and read a hash value (the value after #id=)?
Thank you
On client side (i.e. from JavaScript) you can check window.location.hash to get hash. On server side, general answer is 'it is impossible' since hash is not sent in request to server.
Upd: I maybe misunderstood the question. My answer is about how to get hash part of url either in browser or in server side code during request processing, not about string processing.
Upd2: Answer to comment here because it doesn't fit in comment.
How does it work when user clicks on your navigational links?
I assume hash is changed and corresponding content is downloaded via AJAX request from web service or REST.
For example if your user has URL www.example.com in his browser and this page shows a list of product categories. User clicks one category and URL changes to www.example.com/#id=5 and products from that category(with ID=5) are downloaded via AJAX and shown on the page. No postback, only partial page refresh.
Is this close to your scenario?
Now you want user to paste/enter www.example.com/#id=5 directly in the browser address bar and go directly to list of products in that category.
But /#id=5 is not sent to server with request by the browser, so there is no way to get that value on server side, and you can do nothing about it since it is the browser decided not to send this data and you don't have it on server side.
In our project we use solution when server returns only common page code/html, i.e. header, footer, without main/center part of the page. Then there is a JavaScript code which executes right after this common HTML loaded. It takes window.location.hash and sends it to web service via AJAX and web service returns content (HTML) for the main part of the page.
new URI("http://.../abc#xyz").getFragment();
See the Javadocs for URI
Here is how to capture anchor links. Works across all web frameworks.
I'll use an example scenario to illustrate: let's say we need to capture a deep URL http://server.com/#/xyz requested by an unauthenticated user so that they can be redirected to that deep URL post-login.
The unauthenticated user requests http://server.com/#/xyz (everything from the '#' onwards is not sent to the server).
All the server knows is that the user wants http://server.com/ and that they are unauthenticated. Server redirects the user to a login form.
Here's the clever bit: the client is still waiting on their original request so if the server includes a hidden element in the login form with some JS that references window.location.href, it can capture the full URL of the original request complete with the anchor portion:
<form action="/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/><br/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password"/>
</div>
<!-- XXXXXXXXX CLEVER BIT XXXXXXXXXX-->
<script>
document.write('<input type="hidden" name="from" value="'+document.location.href+'"/>');
</script>
<!-- XXXXXXXXXX-->
<div>
<input class="submit-button" type="submit" value="Submit"/>
</div>
</form>
The user authenticates themself and the original URL is sent with the POST. The server can then relay the user to the original deep URL.
String url = " http://www.coolsite.com/daily-plan/#id=1";
int sharpPos = url.indexOf('#');
String q = null;
if (sharpPos >= 0) {
q = url.substring(sharpPos);
}
Surely you can use various methods of string manipulation including regular expressions.
But actually your example is strange. Typically parameters of URL are passed after question mark. In this case you can just use standard class URL:
String q = new URL(" http://www.coolsite.com/daily-plan?id=1").getQuery();
what you are using to do this ?
If you are using jsp or servlet following will be useful to you
if (request.getParameter("#id") == null) {
out.println("Please enter your name.");
} else {
out.println("Hello <b>"+request.getParameter(i)+"</b>!");
}
If you are using javascript for it following function will be useful to you
function getURLParameters()
{
var sURL = window.document.URL.toString();
if (sURL.indexOf("?") > 0)
{
var arrParams = sURL.split("?");
var arrURLParams = arrParams[1].split("&");
var arrParamNames = new Array(arrURLParams.length);
var arrParamValues = new Array(arrURLParams.length);
var i = 0;
for (i=0;i<arrURLParams.length;i++)
{
var sParam = arrURLParams[i].split("=");
arrParamNames[i] = sParam[0];
if (sParam[1] != "")
arrParamValues[i] = unescape(sParam[1]);
else
arrParamValues[i] = "No Value";
}
for (i=0;i<arrURLParams.length;i++)
{
alert(arrParamNames[i]+" = "+ arrParamValues[i]);
}
}
else
{
alert("No parameters.");
}
}
REPLACE the '#' with '?' when parsing the url. Check the code below
String url = "http://www.coolsite.com/daily-plan/#id=1";
String urlNew = url.replace("#", "?");
String id = Uri.parse(urlNew).getQueryParameter("id");
If you URL will the same as you write and doesn't contains anythins else then whis code on Java will help you
String val = "http://www.coolsite.com/daily-plan/#id=1";
System.out.println(val.split("#id")[1]);
Don't forget check to null value.
P.S. If you use servlet you can get this parameter from request.getAttribute("id").
With best regards,
Psycho
if your url get from OAuth callback,then you can't!
because the full url won't send to service because of hash(#)

How to call a Scala function from Play html template

I am new to Scala/Play Framework.
Currently, I am trying to call a Scala function from my html page: test.scala.html and pass the hash parameters to the Scala function.
I added the following lines to routes:
GET /hello controllers.Application.test
POST /hello controllers.Application.hello
In my test.scala.html I have:
#params = { window.location.hash }
#helper.form(action = routes.Application.hello) {
}
And my hello function is defined as:
def hello() = Action {
Ok("Hello !")
}
I am completely confused by the concept of routing and # so I am not too sure which part I did right and which part I did wrong. Please point out my mistakes.
Thanks in advance.
If the function is returning an action, not content to be displayed formatted inside view (HTML), you may want to route request to this action, from a link click or a form submit, to url configured in routing (aka /hello).
To add a parameter you need to either add it as url query string (e.g. for a link → /hello?p=1), or with an input/field for a form (e.g. <input type="text" name="p" value="1" />).
You can use reverse routing to get URL to call configured action. For example for a form POST to hello: <form action="#routes.MyController.hello()" method="POST">.... In this case you will need to look at form mapping, to extract parameters from request.
1) Concept of routing
The main purpose of this routing is simply to translate each incoming HTTP request to an Action in any of your Controller. By Reverse Routing its simply let you use the right part, controllers.Application.hello, in your HTML/Controller/else.
So, for your 2 URLs above, it's likely to say that if there is a request /hello with method GET then it will go to Application controller and test method. If you don't understand the role of each Routing method, please read this documentation..
2) the magic # character
# is a character that you can use in your HTML file if you need to use the Scala code or variables. It's like you can combine PHP code in your HTML file, if you're a PHP developer.
Here is the full-documentation of what you can do with this # character.
3) pass the hash to the controller
To this specific case the simplest way would be passing the value trough a form:
#helper.form(action = routes.Application.hello) {
#helper.inputText(myForm("username"), 'id -> "username", 'size -> 30, 'value -> 'value here' )
}
However, if you're a new Play developer, then I'm afraid you need to read about Form Submission and Form Helper in Play Framework..

from javascript to jinja2

Is it possible to access data obtained via .getJSON call in jquery as a jinja2 variable ?
$.getJSON(
$SCRIPT_ROOT +"/gitem/"+node.id,
function(data){
if (data.length > 0){
$.each(data, function(index,val_dict) {
var button_id = "button_"+String(index);
var popup_id = "element_to_pop_up_"+String(index);
var append_string = sprintf('<div class="icony"><img src="%s" height="75" id="%s" >%s</div>',val_dict.img_url,button_id,val_dict.img_caption);
var bpopup_element = sprintf('<div id="%s"><a class="bClose"><img src="%s" width="500px">X<a/></div>',popup_id,val_dict.img_url)
$('.data_area').append(append_string+bpopup_element);
$('#'+popup_id).hide();
$('#'+button_id).bind('click', function(e) {
e.preventDefault();
$('#'+popup_id).bPopup();
});
});
}
else
{
var append_string = '<div class="icony">No Images to display for this category</div>';
$('.data_area').append(append_string);
}
$("#list_viewer").css("display", "block");
});
For example in the above I am getting the data and then constructing the DOM within js. While what would be best would be to import data via .getJSON and then set the data as a jinja2 variable.
Later I can use that variable within jinja2 template ?
it that possible ?
or better still...
can a jinja macro be called from within the .getJSON function ? that can also allow embedding of json data within jinja2....
thanks for any pointer...
Probably not. Jinja generally runs on the server, rendering your web site templates before sending them off to the client. The client (i.e. your web browser) will not have access to the templates.
If you'd like to use Jinja templates to render client-side JSON objects, you could take a look at Jasinja, which is a tool to convert your Jinja templates to JavaScript, which you can then use in the browser. A number of similar tools exist.
Finally, another solution is to send your JSON data to the web server, render them to HTML via Jinja, then send back the resulting HTML in the XMLHttpRequest response. However, if you've obtained this JSON data from the same server you use to render templates, you might be better off just having HTML sent to the client directly and added to the DOM from there.

How to get referrer http header at Gwt Entrypoint

I Couldn't find any class/method which gives me access to the referrer header in GWT.
anyone knows about this?
See
Document.get().getReferrer()
Since you can't get the headers in javascript, I don't think you can get them in a GWT client either: Accessing the web page's HTTP Headers in JavaScript
Update:
Maybe you can update login.php to write out the referrer to a hidden input tag, maybe something like this:
<input type="hidden" name="referrer" name="referrer" value="<?php Print referrer_value ?>">
Then, in gwt you should be able to get the value using something like this:
InputElement elt = (InputElement)Document.get().getElementById("referrer")
String referrer = elt.getValue()
Note: This is untested code, and I'm not even sure that is valid php, but hope this helps!
I had the same question, but I made some changes to charge the header link tag dinamically.
I used this code:
LinkElement link = Document.get().createLinkElement();
link.setHref("css/home.css");
I don't know if is the most graceful solution, but it works!
EDIT:
If you need to modify any current element you should to do this:
NodeList<Element> links = Document.get().getElementsByTagName("link");
for(int i = 0; i < links.getLength(); i++){
LinkElement l = (LinkElement)links.getItem(i);
if( l.toString().contains("href_to_replace.css") ){
l.setHref("new_href.css");
break;
}
}
You can access to the referrer in JavaScript and pass it to Java (rather to the JavaScript compiled from Java). You need to define a JSNI (JavaScript Native Method) method in Java with a JavaScript definition. This code can access the document and window objects of the browser, although you need to respectively use $doc and $wnd variables for that purpose.
More info at
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI
You can get the full URL String like so:
String url = Document.get().getURL();
get the index of a question mark and parse it by yourself