API TESTING 2

    API TESTING 2


    I. What are HTTP status codes?

    Browsers and servers interact via the Hypertext Transfer Protocol (HTTP) status code. The browser transmits your request to a server, which then delivers a response when you use the internet to seek information from a server. The HTTP status code, which informs the browser of the request's status and whether or not something went wrong, as well as the reason for the error, is a component of that response. The requested page, the client's browser, and the server are among the details carried by HTTP headers for requests and answers, and these details can determine the status codes.

    II. What are common HTTP status codes?

    Numerous HTTP status codes are available. Wikipedia now lists 75 different status codes, the majority of which you've probably never heard of

    1xx informational response
    Communicates transfer protocol-level information. 
    • 1. 100 Continue, this response means everything is functioning properly and can proceed. The code indicates the server is processing the request and can complete it. The 100 code is an interim response and tells you the request has not caused an error. 
    • 2. 101 Switching Protocol, the server received the data request and can comply. The server, however, will change protocols based on information in the Upgrade header field. This status code does not indicate an error. 
    • 3. 102 Processing, the server received the request, but there isn't any response yet. The server is processing the request, which is ongoing. This code also indicates an error has not occurred.  

    2xx success
    The client accepts the request and is processed it successfully at the server.
    • 1. 203 Non-Authoritative Information, the returned data does not match the data available on the server. This often can indicate the data arrived via a proxy source of the server that contained the original data. This code means the server received a 200 OK from its origin, but it's returning a modified version of the response.
    •  2. 204 No Content, the server sends this code when it received and understood the request. With this code, though, the server is signaling it has no data to return. Even though there's no content to send, this code indicates the HTTP headers are proper. 
    • 3. 205 Reset Content, the server has completed the request, but there was a problem. This status code represents a correctable error. You can solve this by resetting the document view and trying again.  

    3xx redirection
    Indicates that the client must take some additional action in order to complete their request. Most of the codes related to this series are for URL Redirection. 
    • 1. 300 Multiple choices, The request has more than one potential response. The server often sends a report with this code that includes a list of the potential choices. To get the data, you can choose from the responses the server provides. 
    • 2. 301 Moved permanently, This code indicates the URL no longer exists or is broken. This message often includes the new URL for you to use. It also signifies to the user to update all references to this URL. 
    • 3. 302 Found, The Uniform Resource Identifier (URI) changed, but the change is not permanent. A 302 Found means more changes might occur in the future, so consider using the new URI to complete requests. This code is one way to create a temporary redirect. 
    • 4. 305 Use proxy, The request needs to use a server proxy to complete successfully. This message tells the user to send the request again. The second request will go to the server proxy, which can provide the data. 
    • 5. 307 Temporary redirect, This response means the Uniform Resource Identifier is different. The server has to use a new, temporary URI to return data. This code also indicates future requests likely won't use the temporary URI and will function as normal. 

    4xx client errors
    This category of error status codes points the finger at clients (these are specific to client-side errors.)  
    • 1.400 Bad request, this code indicates invalid syntax prevented the server from receiving a request. Servers return this code when the requester made the error and should correct it before trying again. This could mean the client used invalid message framing or improperly routed the message. 
    • 2. 401 Unauthorized, the server cannot complete the request. The server believes the user does not have the authorization to receive the data. After sending this code, a server won't complete the request until user authentication happens. 
    • 3. 403 Forbidden, the server understood the request, but it is refusing to fulfill it. Sometimes, the user does not have access to the requested content or data, and the server rejects the request. This also can mean the client needs an account to request data or the request would create a duplicate record. 
    • 4. 404 Not found, the 404 error is one of the most common codes servers send. The error means the server can't find information in that location now, but it might be able to in the future. It also can occur when a page has a new location, but a user didn't provide the old one with a redirect. 
    • 5. 408 Request timeout, this code means a request took too long to process. It also could indicate the request didn't make it through to the server. Sometimes, internet problems can interrupt the process, but the user can try again. 
    • 6. 410 Gone, the information a user requested from the server no longer exists. This code means the information existed at some point, but it doesn't anymore. The server also can't tell you whether the information exists somewhere else. 
    • 7. 422 Unprocessable Entity, the request was well-formed but was unable to be followed due to semantic errors. 
    • 8. 429 Too Many Requests, The user has sent too many requests in a given amount of time.

    5xx server errors
    The browser makes a valid request, but an error happened on the server side of the process (these are specific to the server-side error).
    • 1. 500 Internal Server Error, a generic error message, is given when an unexpected condition was encountered and no more specific messages are suitable. 
    • 2. 501 Not Implemented, the server either does not recognize the request method or lacks the ability to fulfill the request. Usually, this implies future availability (e.g., a new feature of a web-service API). 
    • 3. 502 Bad Gateway, the server was acting as a gateway or proxy and received an invalid response from the upstream server. 
    • 4. 503 Service Unavailable, the server cannot handle the request (because it is overloaded or down for maintenance). Generally, this is a temporary state 
    • 5. 504 Gateway Timeout, the server was acting as a gateway or proxy and did not receive a timely response from the upstream server 
    • 6. 505 HTTP Version Not Supported, the server does not support the HTTP protocol version used in the request. 

    III. What are the common API testing types?

    While there are certainly specialty tests, and no list can be asked to be comprehensive in this realm, most tests fit broadly into the following nine categories that you should remember before attending an API testing interview. 
    • 1. Validation Testing
    • 2. Functional Testing 
    • 3. UI testing 
    • 4. Load testing 
    • 5. Runtime/ Error Detection 
    • 6. Security testing 
    • 7. Penetration testing 
    • 8. Fuzz testing 
    • 9. Interoperability and WS Compliance testing

    IV. What are the Limits of API Usage?

    Many APIs have a certain limit set up by the provider. Thus, try to estimate your usage and understand how that will impact the overall cost of the offering. Whether this will be a problem depends in large part on how data is leveraged. Getting caught by a quota and effectively cut off because of budget limitations will render the service (and any system or process depending on it) virtually useless. 


    API TESTING 1

      API TESTING 1



      I. What is an API?

      API stands for Application Programming Interface which is useful for communication between different software systems. It facilitates data exchange between systems located in different remote places. API is a collection of functions that are executable by other functions of the applications (Web, Desktop, iOS App, and Android Apk).


      When you use a mobile application, browser, or desktop application, it connects to the Internet and sends information to a server. The data is subsequently retrieved, interpreted, and sent back to your phone by the server. The application then analyzes the data and displays the information you requested in a comprehensible manner. All of this occurs through an API.

      II. What is API Testing?

      API (Application Programming Interface) is a computing interface that enables communication and data exchange between two separate software systems. A software system that executes an API includes several functions/subroutines that another software system can perform. API defines requests that can be made, how to make requests, data formats that can be used, etc.

      III. What are the advantages of API Testing? 

      In an API interview, they are likely to ask about the advantages of API testing. So be prepared with the significant ones such as

      • Test for Core Functionality: API testing provides access to the application without a user interface. The core and code-level functionalities of the application will be tested and evaluated early before the GUI tests. This will help detect the minor issues which can become bigger during the GUI testing
      • Time Effective: API testing usually is less time-consuming than functional GUI testing. The web elements in GUI testing must be polled, which makes the testing process slower. Particularly, API test automation requires less code so it can provide better and faster test coverage compared to GUI test automation. These will result in cost savings for the testing project. 
      • Language-Independent: In API testing, data is exchanged using XML or JSON. These transfer modes are completely language-independent, allowing users to select any coding language when adopting automation testing services for the project.
      • Easy Integration with GUI: API tests enable highly integrable tests, which is particularly useful if you want to perform functional GUI tests after API testing. For instance, simple integration would allow new user accounts to be created within the application before a GUI test started.



      Sleep in Selenium


        Sleep in Selenium



        I. Waits in Selenium

        Wait is the process of matching the speed of action and reaction, selenium does the action, and web application shows the reaction.

        Example: Click Login button gmail login (selenium does this action) web application navigates to the inbox(reaction)

        public class Example
        {
        	public static void main(String[] args)
        	{
        		//open firefox browser
        		WebDriver driver=new ChromeDriver();
        		//goto gmail.com
        		driver.get("https://www.gmail.com");
        		//enter text in username
        		driver.findElement(By.xpath("//*[@id='email' type='email'   ]")).sendKeys("username");
        		//enter text in password
        		driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
        		//click on the signin button
        		driver.findElement(By.xpath("//*[@id='signIn']")).click();
        		//click on the compose button
        		driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")).click();
        	}
        }

        If you run the above program, you might face "NoSuchElementException" Exception because selenium starts searching for the compose button immediately after clicking on the SignIn button; it waits for few milliseconds, but usually, gmail takes 3 to 4 second to open so selenium will not able to find it.

        In most of the cases, the selenium does the action in micro or milliseconds, but the application takes time in seconds.

        At the same time, selenium will not wait till it loads or becomes visible at the time selenium will not able to find the element within the java processing time so selenium throws “NoSuchElementException᾿.

        We cannot alter the speed of the application, so we must slow down selenium.

        Type of Waits in selenium webdriver

        There are several ways to synchronize the selenium with application

        • Sleep
        • pageLoadTimeout
        • setScriptTimeout
        • ImplicitlyWait
        • ExplicitWait or WebdriverWait
        • FluentWait

        II. Sleep() in webdriver

        If you mention Thread.sleep(20); then selenium waits for 20 milli-seconds sleep method take the argument in milliseconds as the input. If you want to make the selenium to sleep for the 20 seconds means we need to mention like this Thread.sleep(20000);
        Sleep() method will not worry about whether the element is present or not; it just sleeps till the mentioned time. This is also called a static wait or blind wait, dead wait.

        Syntax : Thread.sleep(Long milli-seconds);


        Complete program for Sleep()

        public class A
        {
        	public static void main(String[] args) throws InterruptedException
        	{
        		//open firefox
        		WebDriver driver=new FirefoxDriver();
        		//open google.com
        		driver.get("https://oogle.com");
        		//wait for 5 seconds
        		Thread.sleep(5000);
        		//send text to the search bar
        		driver.findElement(By.name("search bar")).sendKeys("selenium-webdriver");
        		//click on the search button
        		driver.findElement(By.id("q")).click();
        	}
        }


        Code Example 2 for Sleep()

        public class GmailLogin
        {
        	public static void main(String[] args) throws InterruptedException							{
        		//open the firefox
        		WebDriver driver=new FirefoxDriver();
        
        		//sleep for 5 seconds
        		Thread.sleep(5000);
        
        		//open gmail.com
        		driver.get("https://www.gmail.com");
        
        		//sleep for 5 seconds
        		Thread.sleep(5000);
        
        		//enter the user name in the Email field
        		driver.findElement(By.id("Email")).sendKeys("username");
        
        		//sleep for 5 seconds
        		Thread.sleep(5000);
        
        		//enter the password in the password field
        		driver.findElement(By.id("Passwd")).sendKeys("password");
        
        		//sleep for 5 seconds
        		Thread.sleep(5000);
        
        		//sends the enter to the password field
        		//sendKeys always append the text present in the element
        		//it never erases the text in the element
        		driver.findElement(By.id("Passwd")).click();
        	}
        }

        Consider the above program in which we are using sleep() method every time it waits for 10 seconds.
        Let us consider a practical application in that we are having more than 8 pages and 50 operations. If we use Thread.sleep(10000) for 50 times, then it is 500 seconds around 10 minutes, so the dead time is more but no one prefers to wait.



        Switch Case Statement

           

          Switch Case Statement

          I. Switch case



          Syntax
          switch (expression) {
              case value1:
                  // statement sequence
                  break;
              case value2:
                  // statement sequence
                  break;
                  .
                  .
                  .
                  case valueN:
                      // statement sequence
                      break;
              default:
                  // default statement sequence
          }
          Example
          public class SwitchDemo {
              public static void main(String[] args) {
          
                  int month = 8;
                  String monthString;
                  switch (month) {
                      case 1:
                          monthString = "January";
                          break;
                      case 2:
                          monthString = "February";
                          break;
                      case 3:
                          monthString = "March";
                          break;
                      case 4:
                          monthString = "April";
                          break;
                      case 5:
                          monthString = "May";
                          break;
                      case 6:
                          monthString = "June";
                          break;
                      case 7:
                          monthString = "July";
                          break;
                      case 8:
                          monthString = "August";
                          break;
                      case 9:
                          monthString = "September";
                          break;
                      case 10:
                          monthString = "October";
                          break;
                      case 11:
                          monthString = "November";
                          break;
                      case 12:
                          monthString = "December";
                          break;
                      default:
                          monthString = "Invalid month";
                          break;
                  }
                  System.out.println(monthString);
              }
          }
          Output:
          August


          If Statement

             

            If Statement


            I. Java if statement



            Syntax
            if(condition){  
            //code to be executed  
            }
            Example

            public class IfStatementExample {
                public static void main(String[] args) {
                    int x, y;
                    x = 10;
                    y = 20;
                    if (x < y) {
                        System.out.println("x is less than y");
                    }
                    x = x * 2;
                    if (x == y) {
                        System.out.println("x now equal to y");
                    }
                    x = x * 2;
                    if (x > y) {
                        System.out.println("x now greater than y");
                    }
                    // this won't display anything
                    if (x == y)
                        System.out.println("you won't see this");
                }
            }
            Output:
            x is less than y
            x now equal to y
            x now greater than y

            II. Java if-else statement

            Syntax
            if(condition){  
                 statement 1; //code if condition is true  
            }else{  
                 statement 2; //code if condition is false  
            }  
            Example
            public class IfElseDemo {
                public static void main(String[] args) {
            
                    int testscore = 76;
                    char grade;
            
                    if (testscore >= 90) {
                        grade = 'A';
                    } else if (testscore >= 80) {
                        grade = 'B';
                    } else if (testscore >= 70) {
                        grade = 'C';
                    } else if (testscore >= 60) {
                        grade = 'D';
                    } else {
                        grade = 'F';
                    }
                    System.out.println("Grade = " + grade);
                }
            }
            Output:
            Grade = C

            III. Java if-else-if statement


            Syntax
            Syntax:
            
            if(condition1){  
            //code to be executed if condition1 is true  
            }else if(condition2){  
            //code to be executed if condition2 is true  
            }  
            else if(condition3){  
            //code to be executed if condition3 is true  
            }  
            ...  
            else{  
            //code to be executed if all the conditions are false  
            }  
            Example
            public class IfElseIfStatementExample {
                public static void main(String args[]) {
                    int month = 4; // April
                    String season;
                    if (month == 12 || month == 1 || month == 2)
                        season = "Winter";
                    else if (month == 3 || month == 4 || month == 5)
                        season = "Spring";
                    else if (month == 6 || month == 7 || month == 8)
                        season = "Summer";
                    else if (month == 9 || month == 10 || month == 11)
                        season = "Autumn";
                    else
                        season = "Bogus Month";
                    System.out.println("April is in the " + season + ".");
                }
            }
            Output:
            April is in the Spring.


            Arithmetic Operators

             

            Arithmetic Operators

            1. The Arithmetic Operators



            public class BasicMath {
                public static void main(String args[]) {
                    // arithmetic using integers
                    System.out.println("Integer Arithmetic");
                    int a = 1 + 1;
                    int b = a * 3;
                    int c = b / 4;
                    int d = c - a;
                    int e = -d;
                    System.out.println("a = " + a);
                    System.out.println("b = " + b);
                    System.out.println("c = " + c);
                    System.out.println("d = " + d);
                    System.out.println("e = " + e);
                    // arithmetic using doubles
                    System.out.println("\nFloating Point Arithmetic");
                    double da = 1 + 1;
                    double db = da * 3;
                    double dc = db / 4;
                    double dd = dc - a;
                    double de = -dd;
                    System.out.println("da = " + da);
                    System.out.println("db = " + db);
                    System.out.println("dc = " + dc);
                    System.out.println("dd = " + dd);
                    System.out.println("de = " + de);
                }
            }
            Output:
            Integer Arithmetic
            a = 2
            b = 6
            c = 1
            d = -1
            e = 1
            
            Floating Point Arithmetic
            da = 2.0
            db = 6.0
            dc = 1.5
            dd = -0.5
            de = 0.5

            2. The Modulus Operator

             returns the remainder of a division operation
            public class Modulus {
                public static void main(String args[]) {
                    int x = 42;
                    double y = 42.25;
                    System.out.println("x mod 10 = " + x % 10);
                    System.out.println("y mod 10 = " + y % 10);
                }
            }
            Output:
            x mod 10 = 2
            y mod 10 = 2.25

            3. Increment and Decrement

            x = x + 1; or x++;
            x = x - 1; or x--;

            4. Arithmetic Assignment Operators

            public class OpEquals {
                public static void main(String args[]) {
                    int a = 1;
                    int b = 2;
                    int c = 3;
                    a += 5;
                    b *= 4;
                    c += a * b;
                    c %= 6;
                    System.out.println("a = " + a);
                    System.out.println("b = " + b);
                    System.out.println("c = " + c);
                }
            }
            Output:
            a = 6
            b = 8
            c = 3