Reputation: 2262
Well, the title says it all... It is possible to perform an XmlHttpRequest from Selenium/Webdriver and then render the output of that requests in a browser instance ? If so, can you enlight me please ?
Upvotes: 3
Views: 2724
Reputation: 2170
I think you want something like this:
fileupload_url = 'https://this-site.com/upload'
filedata = open('example.txt').read()
ajax_request = '''
var xhr = new XMLHttpRequest();
xhr.setRequestHeader('Content-type', 'application/octet-stream'); // text/plain
xhr.onload = function(){{ document.body.innerHTML += '<div>Passed!</div>';
alert (xhr.responseText); }} // success case
xhr.onerror = function(){{ document.body.innerHTML += '<div>Failed!</div>';
alert (xhr.responseText); }} // failure case
xhr.open ('POST', {}, true);
xhr.send ({});
'''.format(fileupload_url, filedata)
driver.execute_script(ajax_request)
Upvotes: 0
Reputation: 8995
Selenium is really designed to be an external control system for a web browser. I don't think of it as being the source of test data, itself. There are other unit-testing frameworks which are designed for this purpose, but I see Selenium's intended purpose to be different.
Upvotes: 0
Reputation: 474171
One option would be to make the XHR request using, for instance, requests
, save the response to a file and get()
it with selenium webdriver instance (How to use selenium webdriver on local (on my pc) webpage instead of locate somwhere on www?).
Upvotes: 1