Thursday, January 16, 2020

Design Patterns used in Selenium with Java

Which has better Performance Page Object model or Page Factory .


Page Factory-

1) Import Package -

import package ‘org.openqa.selenium.support.PageFactory

2)We use Page Factory pattern to initialize web elements which are defined in Page Objects.

 CreateMerchandisepageobject createMerch = PageFactory.initElements(driver, CreateMerchandisepageobject.class);
 

3)Every time when a method is called on a WebElement, the driver will first find it on the current page and then simulate the action on the WebElement. There are cases where we will be working with a basic page, and we know that we will find the element on the page every time we look for it, In such cases we can use annotation ‘@CacheLookup‘ which is another annotation in page factory

 

As you have just seen it is possible to implement the page object model without any support from Selenium. Luckily Selenium does offer the possibility to create Page Objects by using the Selenium integrated Page Factory. With the Selenium Page Factory, you can make your page objects even shorter and you will get a performance improvement as Selenium handles most of the heavy lifting.
Let us take a look at how we can modify our Login Page object from above to use the Selenium Page Factory pattern. Please note that with the code below Selenium will cache the lookup of all elements. You would only do this if you are certain that the elements do not change during the lifetime of the page object. On an AJAX-heavy application, you should avoid this and have Selenium lookup the element each time it is accessed.


 1)Difference between Page Object and Page Factory -

Page Object -In Page Object we use By Class

public class BasePage {

private By username = By.id("username");
private By password = By.id("password");
private By loginBtn = By.name("loginbtn");




In Page Factory we use Annotation
@FindBy
@CacheLook - but it can also result in Stale element Exception or should not be used in Ajax calls
- in such cases we can use @Ajaxfactory.


public class BasePage {
  @FindBy(id= "username") private WebElement userName;
  @FindBy(id= "password") private WebElement password;
  @FindBy(id= "login") private WebElement loginBtn;

---------------------------*-----------------------------------------------------
Refer Links :
https://crossbrowsertesting.com/blog/selenium/selenium-design-patterns/




Wednesday, January 15, 2020

Class and Polymorphism questions in Java

Can we create a static class in Java?

We cannot static class in Java at Top Level as it will give illegal modifier error.-

 

 we can create a nested Static class in java for example.



 ---

Is function over-riding  possible for waits in selenium ?

If we have both implicit and explicit wait then which wait will be over-rided?

Refer links -
http://makeseleniumeasy.com/2017/07/02/part-6-waits-in-selenium-what-happens-when-we-mix-implicit-wait-and-explicit-wait/

String in Java


Strings in Java


Strings in Java are Objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created.

API Testing Questions-

What are Rest API?

What is difference between REST  and RESTFULL API?

What is different HTTP code -

What is difference between Post and Put.

Post -It can have duplicate URI created for the same resource
Put- create only one URI

Successfull Response

200- ok
203-203 Non-Authoritative Information -
204 -Data not found
201 -Data created


Client error response .

400-Bad Request
401- Unauthorized Access.

403-Forbidden access-
the 403 Forbidden Error happens when the web page (or other resource) that you’re trying to open in your web browser is a resource that you’re not allowed to access. It’s called a 403 error because that’s the HTTP status code that the web server uses to describe that kind of error. You usually get this error for one of two reasons. The first is that the owners of the web server have properly set up access permissions, and that you’re really not allowed access to the resource. The second reason is that the owners of the web server have improperly set up permissions and you’re getting denied access when you really shouldn’t be.

404- Page not Found -




3-Redirection messages.
301-301 Moved Permanently

Server Response Error.
500-Internal Server Errror
502- Bad Gateway
503- Service Unavailable 
504- Gateway Timeout


What are different Component in REST API.
 Request 

URI
Resource
Content Type
Query Parameters
Header
Body
Message

RESPONSE

What assertions are used in SOAP .
--------*----
A REST message contains these components:
  • Resource Path
  • HTTP Verb
  • Body
  • Header

--

https://restfulapi.net/rest-architectural-constraints/

Architectural Constraints

REST defines 6 architectural constraints which make any web service – a true RESTful API.
  1. Uniform interface
  2. Client–server
  3. Stateless
  4. Cacheable
  5. Layered system
  6. Code on demand (optional)

---------

Difference between REST and SOAP

  • REST is an architectural style & SOAP is a protocol.
  • REST stands for Representational State Transfer & SOAP stands for Simple Object Access Protocol
  • REST can use SOAP web services because it is protocol independent and can use any protocol like HTTP, and SOAP & SOAP can’t use REST because it is a protocol.
  • The bandwidth and resource required by REST are less as compared to SOAP.
  • In REST we can use different data format such as Plain text, HTML, XML, JSON, etc. & In SOAP only XML format can be used.
  • REST allows data of different formats, like Plain text, HTML, XML, JSON, etc. & SOAP defines its own security.


Refer Links-

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

Tuesday, January 14, 2020

Java others


Difference between an Integer and int in Java with Examples

In Java, int is a primitive data type while Integer is a Wrapper class.
  • int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.
  • Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
  • Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.
How Can we can call the method of Base class in derived class?
through Super constructor calling - this.driver

What is difference between throws and throw ?-

Throws -
1)It is used with method signature .

Public void readFile() throws IOException
{
}

2) It is implicit exception where one do have to use the try catch function.

3)It is used to declare and exception.
4). If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by exception class names.
5)throws is used to declare and exception
6)throws can be used to handle multiple exception.

Throw -
1)It is used inside the method .

package testpack;

import org.testng.annotations.Test;

public class Throwexample {
    int Age;
  
  
    public void validateAdult(int Age)

    {
try {
    if(Age<18)

    {  
    throw  new ArithmeticException ("Error not an Adult");
    }
else
    {
System.out.println("Welcome to adulthood");
    }
}
  
catch (ArithmeticException e)
    {
e.printStackTrace();
    }
  
    }
  
    @Test
    public void throwmethod()
    {
    validateAdult(12);
        validateAdult(22);
      
    }

}
2) It is explict exception where one do have to use the try catch function .(for good practice it should be used with in the try block)


3). If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by exception class names.

4)throws can be used to handle Single exception only.


Function of e.printStackTrace().

The printStackTrace() method of Java.lang.Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.
The first line of output shows the same string which was returned by the toString() method for this object means Exception class name and later lines represent data previously recorded by the method fillInStackTrace().






----