In
this blog we will be automating a few samples Twitter API using Rest Assured.
Before Moving further I would highly recommend you visit this blog, to understand more about the twitter Oauth authentication key and generate those keys for further testing.
Please don't try to Access keys that I have shown in the screenshot or code as it won't work for your ids.
Please Note all the twitter API Official documentation could be access by clicking on this link.
Before Moving further I would highly recommend you visit this blog, to understand more about the twitter Oauth authentication key and generate those keys for further testing.
Please don't try to Access keys that I have shown in the screenshot or code as it won't work for your ids.
Please Note all the twitter API Official documentation could be access by clicking on this link.
Now let’s
Begin with one Simple POST API for Creating Tweet on Twitter account.
Please refer to this link to directly access the below request specification.
Please refer to this link to directly access the below request specification.
As
we can see https://api.twitter.com/1.1/statuses/ is Our Base URI
update.json
is resource URI and the Request type is POST.
And
the only mandatory parameter is status which we will be using for sending out text for our tweets.
First, let's execute this Request using Postman. After providing the URL in postman along with the Request type we need to pass Oauth Token as well.
For that click on Auth and Select Type as Oauth 1.0, provide the value of Consumer Key, Consumer Secret, Token and Token Secret that we already have.
Click on Add Params to Header and click on Update Request.
Pass status in the body of the postman
I would be able to see the tweet posted on my Twitter account
First, let's execute this Request using Postman. After providing the URL in postman along with the Request type we need to pass Oauth Token as well.
For that click on Auth and Select Type as Oauth 1.0, provide the value of Consumer Key, Consumer Secret, Token and Token Secret that we already have.
Click on Add Params to Header and click on Update Request.
Pass status in the body of the postman
I would be able to see the tweet posted on my Twitter account
Now let's implement the same through our Rest assured code.
Please Note in this request we need to pass our Oauth token as I showed in the code.
Please Note in this request we need to pass our Oauth token as I showed in the code.
Below is the code for creating a new tweet using Rest Assured.
The output of the above code execution would look something like this
Similarly, we can apply the concept of API Chaining which we have learned while doing Google Place API Testing where we can delete the tweet that we have generated from our first test by either manually passing the unique tweet id or passing it as a value through our code.
To Delete the tweet we need to use twitter destroy/delete API. You can Direct access to this API specification by clicking on this link.
As noted in this https://api.twitter.com/1.1/statuses is our Base URI,
whereas destroy/:id.json is our resource URI,
The request type is POST.
Similar to Create Tweet we can execute this request first in POSTMAN to delete the tweet that I have created earlier.
Similar to Create Tweet we can execute this request first in POSTMAN to delete the tweet that I have created earlier.
Below is the complete code first creating tweet on twitter and then deleting it using Rest Assured.
Now when I refresh my twitter account the tweet will be automatically get deleted.
Now when I refresh my twitter account the tweet will be automatically get deleted.
Please Note there are a lot of good twitter API available for you to explore freely on your own So feel free to use them :)
Hey Ankur - when trying to execute the code I'm dealing with these errors....
Replyany idea how to solve these.
java.lang.NoClassDefFoundError: com/github/scribejava/core/model/Token
Tried solving the token error and i got this error
java.lang.NoClassDefFoundError: com/github/scribejava/core/model/AbstractRequest
And after solving the abstract request i got this error
java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
Use Version 2.5.3 of Scribe java and Scribe core api
ReplyHi Ankur, my tweet is getting posted but TESTNG test is getting failed with the below error:
Replyio.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
Pls advise
Hi Ankur, I user version 2.5.3 of Scribe java and Scribe core api and use version 3.3.0 of rest-assured, and I get the below error:
Replyjava.lang.IllegalArgumentException: No enum constant com.github.scribejava.core.model.Verb.CONNECT
i got same error : java.lang.NoClassDefFoundError: com/github/scribejava/core/model/AbstractRequest
ReplyAfter using version 2.5.3 of Scribe java and Scribe core api .. problem solve..
But how to handle this problem with newer verion. Plz reply.
Where is the rest assured part?
ReplyI see postman only!