Day 5: Exception Handling in Java


First of all lets understand why we need exception handling in Java.

See there are many chances that you will get run time error while executing you code and your program terminates abnormally, to avoid these situation java provide us a mechanism which is known as exception handling.

What is exception :- An Exception can be anything which interrupts the normal flow of the program.

In any program exception can occur at run time as well as at compile time.
  • Exception comes at run time, known as run time exception. also known as Unchecked Exception.
  • Exception comes at compile time, known as compile time exception, also known as checked exception.
  Let take and example for compile time exceptions.


As you can see, we are getting an exception over here saying that there might be a chance that file will not found so compiler force us to write the code in try catch block or declare the method using throws keyword.

It means that all exceptions which are reflecting while compilation known as checked exception.

Now let take another example to under what is meant by run time error.


As you can see in the above example we tried to divide a number via zero  and while compilation we did not get to see any exception however when we run the program we get to see that program throws an error message saying that "Exception in thread "main" java.lang.ArithmeticException: / by zero".

So these type of exception which comes after running the program are known as run time exceptions.

Now let see how we can handle checked and unchecked exceptions in java.

There are 5 keywords used in java exception handling.
  • try
  • catch
  • finally
  • throw
  • throws
Note:- Java try block must be followed either by catch or by finally.


Now lets start with try and catch block first.

let me take an example to show you how we can handle this with try and catch block.



now see the below attached screen print after using try and catch block, remember Always remember that a catch block must always be associated with a try bloc, it can't be used on its own.





now lets understand how catch block actually works.


Declaring an instance of type Exception ensures that any type of exception can be trapped using object e, therefor when the type of execution being thrown is not known at design time,the class Exception can be used to trap the error.


and get message is a function of exception class which will provide the message.


NOTE:- We can have multiple catch blocks with one try block.


Now lets understand the use of Finally keyword.


finally block is something which will always be executed whether we get the exception or not.



let take the example.






now lets discussed about the other two keywords i.e. throw and throws.



Throw keyword is always used to throw the exception explicitly.


lets take an example



so i have explicitly created a exception and throw the same.

now let have a look on throws keyword.

look throws keyword can be used instead of try and catch.

however each time when we use the method in some other method then in that method too we need to throws the exception.


lets take an example






now lets handle this exception using throws in main as well.










Day 04:- Object Oriented Programming Continue

DAY 04- Object Oriented Programming Continue

========================================================================
TOPICS

  • Dynamic Binding
  • Abstract Classes
  • Interfaces In Java till Java-7
  • Final Keyword
  • Interface in Java with Java-8
  • Packages
  • Exception handling
========================================================================

DYNAMIC BINDING

The reference of child class object can be hold in the reference variable of parent class this is known as a Dynamic binding.

When  type of a object is determined at compile level then it is known as compile time binding or static binding.

However when type of the object is determined at run-time, it is known as dynamic binding.

Lets understand with the help of example having a child class and a parent class.


When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method. Thus while calling the overridden method, the compiler gets confused between parent and child class method.

Now lets understand how it works, look show() method is actually called by parent class but since this method is overridden thus JVM run the overridden method  i.e. child class method.

Because if reference of child class object is hold by parent class then only methods which are present in parent class can only be run, with the help of parent we can't call method written in child class except overridden methods.



ABSTRACT CLASS

A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body).

Before learning java abstract class, let's understand the abstraction in java first.

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Examples :-

A.  Bank ATM Screens (Hiding the internal implementation and highlighting set of services like withdraw, money transfer, mobile registration).


B. Mobile phones (The mobile persons are hiding the internal circuit implementation and highlighting touch screen).

C. Syllabus copy (the institutions persons just highlighting the set of contents that persons provided the persons are not highlighting the whole content).


In Java Abstraction can be achieved via 2 ways.

1. Abstract class
2. Interface 

Lets first discussed about the abstract class then will cover the interface part.

Note:- 
  • Abstract classes are declared with abstract keyword.
  • Abstract class may or may not be contain abstract method.
  • Abstract methods do not contain body.
  • If we declare any method as Abstract, then the class must be declared as abstract.
  • child class is responsible to provide the implementation for parent class abstract method.



Now make the class as abstract to remove the error.



let see we can have non abstract methods as well in abstract class


  • Abstract classes can’t be instantiated, because it contains unimplemented methods.


  • Abstract class can have a constructor.

  • To use an abstract class, we must inherit this class from another normal class.
  • The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.


Now let implement the abstract method and see that error will remove.


  • We can’t create a static method as abstract.
  • Abstract method can’t be private.
  • Data members can't be abstract.

Note:- Since abstract classes may or may not contain abstract methods that is the reason abstract class is used to achieve partial abstraction.
INTERFACE IN JAVA

Lets understand why we need interfaces instead of abstract class ?

Since java does not support multiple inheritance we need to go for interface.

Interface in java is like a blue print, it means in interface we can only declare methods we can't define them.

Note:- All methods present in interface are by default abstract and we know that abstract can't have definition, thus we need a class to provide the definition of interface methods.

Note:- To inherit an interface java provided us a keyword known as implement, just like we are implementing the blueprints.

lets take an example.



now create a class to implement the methods of Interface01.



As you can see when we try to implement interface it is a restriction for the class to provide implementation for all the methods present in the class.




As we can see we can call show() method with the help of child class as well as interface reference variable.

This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface.

Note: class implements interface but an interface extends another interface and we can achieve multiple inheritance using interface.

lets take an example




Now let see why java allows us to use multiple inheritance.

Because parent interface does't have any definition, so there is no chance for ambiguity or diamond problem.


So basically there are two benefits of interfaces.

1. It allows us to use multiple inheritance.
2. To achieve abstraction.

Note: 
  • Always remember a interface can't implement another interface.
  • Interface methods are always public.
  • Interface methods are by default abstract. 
  • Interface does not allowed constructor. 
  • When a class implements an interface user should define all the methods of class.
  • When a class implements an interface all the data members are available for class. 
  • The java compiler adds public and abstract keywords before the interface method. More, it adds public, static and final keywords before data members.
  • Interface does not contain main method because main is a static method and static method can't be abstract. 

To understand the data members in Interface lets first take a look for final keyword.


FINAL KEYWORD

Final keyword can be used with Classes, Methods, Variables.

Final keyword is basically use to provide restrictions.



 lets take an example of final variable.



DATA MEMBERS IN INTERFACES

All the data members in interfaces are by default public static and final.

It means the value of data member set in interface can't be change.



Also since variables are final we can't change the value of these variables.

INTERFACE IN JAVA 8

Till java 7 we are not allowed to provide definitions for method which we create in interface, since all the methods are abstract however in java 8 we can provide the definitions for the methods via two ways.

In Java 8 interface changes include static methods and default methods  that means we can create and provide definitions for the methods if they are either static or default.

lets have a look to below mention example for static and default method.


Note:- 

If a class implements two interfaces and both interfaces are having default method then we compiler won't allow us to do the same.

However if a class implements two interfaces and both interfaces are having static method then there would be no compilation error because in this case method can only be accessed with the help of class name.

========================================================================

Links for the files (.Java File)

https://drive.google.com/drive/folders/0B5M4BcbfVN07OEN3UVlUYkRHOTg

Links for recorded Video

https://www.youtube.com/watch?v=wvydHAyFoyc&feature=youtu.be


========================================================================



XPATHS IN SELENIUM


XPATH :-  It is a query language for selecting nodes from an XML document. It is use to identifying the element in the web page. In simple words, finding address of an element on the web page, you can use so many ways to identify the element using XPATH.

In XML document starting node is called as root node or context node.


Xpath in selenium can be categorized in two parts.

1. Complete or Absolute XPATH
2. Partial or Relative XPATH

> Firepath can be used to find out the complete and partial xpath.

Complete or Absolute XPATH
Complete xpath always start from the root node i.e. <html> tag of the document, however complete xpath is not recommended since if any division or developer change any thing in between then xpath will change thus it will be risky to work with complete xpath.

Partial XPATH

  • Partial Xpath will always start with \\tagname[@attribute = 'value']
  • It start with // (double slash).
  • we can use replace tagname with a (*) star so that it will search for all the element.
  • However suing (*) star in partial xpath is not recommended.



Note:- 
In partial xpath there will be a .(dot), don't really need to use this.
Firebug always try to create partial xpath using ID attribute.
Suppose we investigate any web element which does not building with the help of ID, then firebug will provide us complete xpath
We can create our own xpath by providing attribute which ever we want.



CUSTOM XPATH 

We can below methods to identify the element.

  • Single Attribute
    • //tagname[@attribute = 'value']
  • multiple Attribute
    • //tagname[@attribute = 'value' and @attribute1 = 'value']
    • //tagname[@attribute = 'value' ][ @attribute1 = 'value']
  • Contains
    • //tagname[contains(@attribute,'partialAttributevalue/fullAttributeValue')] 
    • Example :-.//*[contains(@class,'inputtext _55r1 inputtext _1kbt inputtext _1kbt')]

  • Starts-With
    • //tagname[starts-with(@attribute,'partialAttributevalue/fullAttributeValue')] 
    • Example :-.//*[starts-with(@class,'inputtext _55r1 inputtext _1kbt inputtext _1kbt')]

  • Text
    • \\tagname(text()='fullTextValue']
    • .//*[text()='Forgotten account?']
  • Ancestor
  • Preceding Node
  • Descendant
  • Following Node








EXCEPTIONS IN SELENIUM

NoSuchElement : An element could not be located on the page using the given search parameters.


NoSuchFrame : A request to switch to a frame could not be satisfied because the frame could not be found.

StaleElementReference : An element command failed because the referenced element is no longer attached to the DOM.

Firefox Not Connected : Firefox browser upgraded toop new version.

ElementIsNotSelectable : An attempt was made to select an element that cannot be selected.

UnknownCommand : The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.

ElementNotVisible : An element command could not be completed because the element is not visible on the page.

InvalidElementState : An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element).

UnknownError : An unknown server-side error occurred while processing the command.

JavaScriptError : An error occurred while executing JavaScript code.

XPathLookupError : An error occurred while searching for an element by XPath.

Timeout : An operation did not complete before its timeout expired.

NoSuchWindow : A request to switch to a different window could not be satisfied because the window could not be found.

InvalidCookieDomain : An illegal attempt was made to set a cookie under a different domain than the current page.

UnableToSetCookie : A request to set a cookie’s value could not be satisfied.

UnexpectedAlertOpen : A modal dialog was open, blocking this operation

NoAlertOpenError : An attempt was made to operate on a modal dialog when one was not open.

ScriptTimeout
: A script did not complete before its timeout expired.

InvalidElementCoordinates : The coordinates provided to an interactions operation are invalid.

IMENotAvailable : IME was not available.

IMEEngineActivationFailed : An IME engine could not be started.

InvalidSelector : Argument was an invalid selector (e.g. XPath/CSS).

unknown error: call function result missing 'value' :- due to version compatibility.


Day 03:- Object Oriented Programming Continue

DAY 03- Object Oriented Programming-continue

========================================================================
TOPICS
  • Inheritance
  • Method Overriding
  • Super Keyword
  • Types of Inheritance
  • Access Modifier In Inheritance
========================================================================
INHERITANCE 

Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the members defined by the superclass and adds its own, unique elements. 

To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. 

Lets understand with the help of example.

As per the above example we can say that child class i.e. Test.java inherits all the properties and functionality of parent class if they are not private.

So if we want to call the same functionality in different classes we can use inheritance so that we need not to re-define the function again and again.

Note:- we can call instance as well as static members and function of parent class.

Now suppose a condition when we have same function and same type of data members in both the class.

Instance function :- If we have same instance function in both the classes the child class function override the parent class function.


Instance data members:- If we have same instance data members in both the classes then preference goes to local i.e. child class data members.



Static function :- If we have same static function in both the classes then preference goes to local i.e. child class data members however we can access the static function of parent class using class name.

Static data members:-  If we have same static data members in both the classes then preference goes to local i.e. child class data members however we can access the static data member of parent class using class name.

Example for static data member as well as function.



Note:-

  • Static function can't be override and this concept is know as function hiding.
  • Data Members can't be override.
  • Constructor can't be inherited they never become a part of child.

SUPER KEYWORD

Super keyword is used to refer immediate parent class object which means that if we want to access the data member of immediate parent class then we can use super keyword.

Example:- 



Same goes for function as well.




CONSTRUCTORS IN INHERITANCE

If we have constructor present in the base class then child class constructor always run parent class default constructor first.


Now let take a look for parameterized constructor.

if a parent class is having any constructor except default then child class must have to call that constructor explicitly and for this we have to use super keyword in our class.

We know that default constructor will only be placed by compiler if class doesn't have any constructor neither default nor parametrize.


Now suppose we have a parameterize constructor in child class then, does it mean it run the default constructor of parent class ? ----> Yes.
USE OF SUPER KEYWORD

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.
TYPE OF INHERITANCE

  1. Single Level Inheritance :- TIll now what we have covered so far is known as single level inheritance which means one parent and one child.
  2. Multiple Inheritance :- Not supported by Java.
  3. Multilevel Inheritance :- In multilevel inheritance, As you can see in below flow diagram C3 is subclass or child class of C2 and C2 is a child class of C1.
  4. Hierarchical Inheritance. In hierarchical inheritance a parent class can have multiple child class.
  5. Hybid Inheritance : Not supported by Java.


Note:- Java does not support multiple and hybrid inheritance due to diamond problem.


There are 4 types of java access modifiers:

  • private
  • default
  • protected
  • public
Note :- 
Child class function must have stronger specifier or same specifier as in base class function because child class cannot reduce the visibility of the inherited method from parent.

Example 




It means we should consider the visiblity of method in both  the classes in inheritance. below is the sequence from stronger to weaker modifier.

1. Public
2. Protected 
3. Default
4. Private. :- private method can't be accessed outside of the class. it means that we can't access private members of class in another class using super keyword.
========================================================================

Links for the files (.Java File)

https://drive.google.com/open?id=0B5M4BcbfVN07VlJ4SDRFMmYzQnc

Links for recorded Video


https://www.youtube.com/watch?v=5LOW5OAIHT4&t=481s


========================================================================

SELENIUM INTERVIEW QUESTIONS AND ANSWERS


What is the difference between close and quit method.

Close method is use to close the current window on which the focus is current present.
Quite method is use to close all the windows open by web-driver.

Explain type of XPATHS in selenium.

In selenium we can have two type of xpath i.e.

  1. Complete or Absolute xpath.
  2. Partial XPATH.


What is the difference between implicit wait and explicit wait.

What is the difference between assert and verify.




how to switch from frame to main window tel syntax.
what is difference between pom and pagefactory?
where does automation fit into testing flow?
what are challenges that you faced while automating testcases?
scenario: there is a submit button in page it has id property.
by using id we got element not found expection, how will you
handle dis situation. what might be the problem in dis case.
scenario: if submit button contain in one of 3 frames in page, how will you handle this.
if element is loaded by taking much time how to handle dis situation in selenium.
what is the problem with thread.sleep in code?
what is the concept of selenium grid?

when we execute testcases in grid where results will be stored in node or hub?
 11> difference b/w quit and close.
 12> scenario: manually u opened a firefox browser window with gmail login, now with selenium you opened
 a firefox browser window with facebook login. what happens when we use quit method?
 will it closes all windows including gmail one?
 13> what all annotations used in testng in ur project?
 14> if we wanna do datadriven with testng what are all annotations required?
 15> is it possible to pass test data through testng.xml file, if yes how?
 16> how to run specific kind of testcases using testng?
 17> how to prioritize test cases in testng?
 18> what are all interfaces available in selenium?
 19> actions is class or interface?
 20> why we using testng? what are benefits we get using testng? cant we execute testcases in order
 without using testng?
 21> explain polymorphism in java.
 22> scenario: der are two methods in same class with same name with different arguments and
 different access modifers. like
 public void m1(int a){}
 private void m1(string b){}
 is it overloading or not?
 23>what are types of inheritance in java?
 24> is multiple inheritance is possible in java? tel reasons.
 25> is multilevl inheritance is possible in java? give reason.
 26> scenario: der are 10 pages in same window, an image is present in any page out of ten pages in same window.
 how will you validate this scenario?
 27> how to check image is loaded correctly or not in page?
 28> scenario: same image is present in ten pages how do you check same image present on each
 page or not. how will you validate this scenario?
 29> what is the purpose of sikuli?
 30> is it possible to compare two images with sikuli?
 31> how do you compare image in some path of drive and image on page?
 32> Tel syntax for sikuli code.
 33> how to handle file upload window in selenium?
 34> scenario: There are ten drop downs in page with same name.. in one of drop downs i have option called cts employee
 how select dat particular option in out of ten drop downs in page? what is ur approach?
 35> write a code for db connection.
 36> explain ur project structure and flow.
 37> how do u handle exception handling in selenium.
 38> explain run time and compile time polymorphiism.
 39> write a code for multiple handling windows.
 40> scenario: der is grid table 1st column contains links with same names in all rows.
 2nd column contains different name for those links present in 1st column.
 now based on 2nd column click on required link in 1st column how will you do it.
 41> how do you handle synchronization in selenium.
 42> how generate user defined exceptions, write syntax.
 43> difference between throw and throws keyword.
 44> scenario: <table
 <tr
 <td
 <td
 now based on 2nd td tag find tr tag element. write xpath for it.
 45> what is auto it? how will you exexute auto it code in selenium?
 46> how to handle elements like ajax?
 47> how to handle elements which has no attribute inside tag?
 48> what is proctractor?
 49> what are loctors present in proctractor?
 50> explain oops concepts.
 51> diff b/w sikuli and auto it.


Java:-

What is the difference between static and instance variables.

Instance variable 

  • An instance variable is one whose memory space is creating each and every time whenever an object is created.
  • Programmatically instance variable declaration should not be preceded by keyword static. 
  • Instance variable must be accessed with respect to object name 
  • Value of instance variable is not sharable. 
Static Variable 

  • Static variables are whose memory space is creating only once when the class is loaded by class loader subsystem (a part of JVM) in the main memory irrespective of number of objects. 
  • Programmatically static variable declaration must be preceded by keyword static. 
  • Static variables must be accessed with respect to class name
  • Value of static variable is always recommended for sharable. 
  • Static variable are also known as class level data members since they are dependent on classes. 

What is the difference between abstract classes and concrete classes.

Concrete classes :- 

A concrete class is one which contains fully defined methods. Defined methods are also known as implemented or concrete methods. With respect to concrete class, we can create an object of that class directly. 

Abstract classes:- 

An abstract class is one which contains some defined methods and some undefined methods. Undefined methods are also known as unimplemented or abstract methods. Abstract method is one which does not contain any definition. To make the method as abstract we have to use a keyword called abstract before the function declaration.