Automating Twitter API Using Rest Assured


Automating Twitter API Using Rest Assured


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.

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.



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


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.

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.

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.



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 :)





SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post
May 17, 2019 at 2:33 AM

Hey Ankur - when trying to execute the code I'm dealing with these errors....
any 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

Reply
avatar
May 18, 2019 at 8:28 AM

Use Version 2.5.3 of Scribe java and Scribe core api

Reply
avatar
May 29, 2019 at 4:40 AM

Hi Ankur, my tweet is getting posted but TESTNG test is getting failed with the below error:
io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document

Pls advise

Reply
avatar
June 5, 2019 at 8:56 AM

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:
java.lang.IllegalArgumentException: No enum constant com.github.scribejava.core.model.Verb.CONNECT

Reply
avatar
October 22, 2019 at 5:16 PM

i got same error : java.lang.NoClassDefFoundError: com/github/scribejava/core/model/AbstractRequest

After 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.

Reply
avatar
December 11, 2019 at 11:35 PM

Where is the rest assured part?
I see postman only!

Reply
avatar