Running a POST request in Selenium without filling out a form?

I have an application A that should handle submitting a form using the POST method. The actual form that triggers the request is in a completely separate application B. I am testing application A using Selenium, and I like to write a test case to handle submitting the form.

How to do it? Can this be done in Selena at all? Appendix A does not have a form that can initiate this request.

Note that the request should use POST, otherwise I could just use the WebDriver.get (url) method.

+8
post forms selenium testing request
source share
2 answers

With selenium, you can execute arbitrary Javascript, including programmatically submitting the form .

The simplest JS implementation with Selenium Java:

if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor) driver).executeScript("alert('hello world');"); } 

and with Javascript you can create a POST request, set the necessary parameters and HTTP headers and send it.

 var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://httpbin.org/post', true); xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.onload = function () { alert(this.responseText); }; xhr.send('login=test&password=test'); 

If you need to pass the response text to selenium, and instead of alert(this.responseText) use return this.responseText and assign the result of executeScript () to the java variable.

+3
source share

I do not think you can use Selenium. It is not possible to create a POST request from nothing using a web browser, and Selenium works by manipulating web browsers. I would suggest you use an HTTP library to send a POST request and run this along with Selenium tests. (What language / testing framework do you use?)

+4
source share

All Articles