Wednesday 27 July 2016

How many ways we can create object in java?

How many ways we can create object in java?:
===================================
We can create object in java in 5 ways:

1. By using NEW keyword
2. By using new Instance (Reflection API)
3. By using clone() method
4. By using Deserilization
5. By using class loader

1. By using new Keyword: Most commonly we use create object in java by using new keyword
Whenever we use new in java object is going to be created

package com.rameshsoft.keys;

public class Test {

    String name = "RAMESHSOFT";
   
    public void hello() {
         System.out.println("WELCOME TO RAMESHSOFT: " +name);
         System.out.println("MASTERS IN JAVA WITH SELENIUM REAL TIME: " +name);
    }
   
    public static void main(String[] args) {
         //creating object using new keyword
         Test test = new Test();
         test.hello();
        
    }
}

2.By using clone() method

We can create object using clone() method as well. this method is present in Object class(java.lang.Object)
syntax of clone() method:
public Object clone();

package com.rameshsoft.keys;

public class Test {

    String name = "RAMESHSOFT";
   
    public void hello() {
         System.out.println("WELCOME TO RAMESHSOFT: "+name);
         System.out.println("MASTERS IN JAVA WITH SELENIUM REAL TIME: " +name);
    }
   
    public static void main(String[] args) throws CloneNotSupportedException {
         //creating object using new keyword
         Test test = new Test();
         //creating cloned object using clone() method
         Test testClone = (Test)test.clone();
         testClone.hello();
        
    }
}

3. Using class.forName(-)
If we know the name of the class & if it has a public default constructor we can create an object in this way.
Syntax:
Test test=(Test) class.forName("JDBC.........").newInstance();

4. using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
Syntax:
ObjectInputStream istream=new ObjectInputStream(some data);
Test test=(Test) instream.readObject();


5. By using class loader

Friday 22 July 2016

What are the Challenges faced using selenium Automation Testing ?

What are the Challenges faced using selenium Testing?


1.  Handling dynamic changing ID to locate element Is tricky. If element's ID Is changing every time when you reload the software web application page and you have to use that ID In XPath to locate element then you have to use functions like starts-with(@id,'post-body-') or contains(@id,'post-body-') In XPath. Ends-with (@id,'post-body-') etc.

2. If you have to execute your test cases In multiple browsers then one test case can run successfully In Firefox browser but same test case may fail In IE browser due to the timing related Issues (NoSuchElementException) because test execution In Firefox browser Is faster than IE browser. You can resolve this Issue by Increasing Implicit wait time when you run your test In IE browser

3.  Dealing with pop-up windows: To handle any kind of alert popup, you can apply a getAlert function. Before actually running the script, you must import a package that can generate a WebDriver script for handling alerts. The efficient interface brings with it the following commands: void dismiss (), void accept (), get Text (), void send Keys (String stringToSend). The first two basically click on the “cancel” and “OK” buttons respectively on a popup window.

4.  Timeout resulting from synchronization problems: One should ideally use selenium.IsElementPresent (locator) to verify that the object is in a loop with Thread. Sleep

5.  Testing Flash apps: To automate flash apps with Selenium, one can use Flex Monkium.

6.  Protected Mode must be set to the same value error occurs when trying to run Selenium Web Driver on a fresh Windows machine. This issue can be fixed by using capabilities

7.  Unexpected error launching Internet Explorer. Browser zoom level should be set to 100% by default for the IE browser to overcome this error.

8.  File Upload/Download Using: Java-Auto IT-Selenium


...................................

Types of variables in JAVA

Types of variables in JAVA:
=====================
There are 4 types variables are available in java

1. Non static variables

2. Static variables
3. Local variables
4. Object type variables


1.Non static variables: 
If the value of a variable is going to be change object to object  such type of variables we called as non-static  variables.
For every object a separate copy of instance variables will be created
Syntax:
Access specifier Datatype variable name = variable name;
When non static variables will be created?
Instance variables will be created at the time of object creation and destroyed at the time of object destruction
When do we declare a variable as non-static::
=======================================
Whenever the value of a variable is going to be change object to object then declare that variable as non-static
Access scope
=============:
 We can access non static variables throughout our program
Where do we declare non static variables?
After starting the class immediately we will declare non static variables directly
Default values:
Initialization of variables is optional. If we don’t perform initialization of variables JVM is going to assign the default values.
Ex:   public class TestDemo
{
Int x;
Public static void main (String [] a){
TestDemo testDemo = new TestDemo ();
System.out.println (testDemo.x);//0
}
}
Default values:
Int   0
Float  0.0
Double 0.0
String   null
Char null
Byte  0
Short  0

Non static variables are also called as instance variables or object level variables
Non static variables will be stored in heap area as the part of object

2. Static variables:
If the value of a variable is not varying from object to object then declare that variables as static variables.
For static variables at class level a single copy will be created and shared across every object of that class
Whenever we declare variable as static we can access that variables without creating object and we can access by directly, class name and by object
Access specifier static Datatype variable name = variable value;
When static variables will be created?
Static variables will be created at the time of class loading and destroyed at the time of class unloading
When do we declare a variable as static::
=======================================
Whenever the value of a variable is not changing from object to object then declare such type of variables as static variables

Access scope
=============:
 We can access static variables  throughout our program
Where do we declare static variables?
After starting the class immediately we will declare non static variables directly with static keyword
Default values:
Initialisation of static variables is optional. If we don’t perform initialisation of variables JVM is going to assign the default values.
Ex:   public class TestDemo
{
Static String x;
Public static void main(String[] a){
System.out.println (x);//null
}
}
Static variables are also called as class level variables
Static variables will be stored inside method area

3. Local variables:
Whenever we declare variables inside the method or constructors such type of variables we can call it as local variables.
When local variables will be created?
Local variables will be created at the time method or constructor creation and method or constructor execution is done local variables will be destroyed
When do we declare a variable as local:
=======================================
Whenever you need the values for certain portion of area to meet requirements then declare that variables as local variables

Access scope
=============:
 We can access local variables within that method or constructor but not outside of that
Where do we declare local variables?
We need to declare local variables within the method or constructors and blocks
Default values:
For local variables compulsory we must and should perform initialization otherwise we will get compile time error. I.e. For local variables JVM won’t provide any default values.
Ex:   public class TestDemo
{
Public static void main(String[] a){
Static String x;
System.out.println(x);//compile time error
}
}

Local variables will be stored in stack memory
Local variables are also called as temporary variables or automatic variables or stack variables
4.Object type variables:
By using object type variables we can refer objects.

SeleniumDemo demo = new SeleniumDemo();

Types of variables in JAVA

Types of variables in JAVA:
=====================
There are 4 types variables are available in java

1. Non static variables
2. Static variables
3. Local variables
4. Object type variables


1.Non static variables: 
If the value of a variable is going to be change object to object  such type of variables we called as non-static  variables.
For every object a separate copy of instance variables will be created
Syntax:
Access specifier Datatype variable name = variable name;
When non static variables will be created?
Instance variables will be created at the time of object creation and destroyed at the time of object destruction
When do we declare a variable as non-static::
=======================================
Whenever the value of a variable is going to be change object to object then declare that variable as non-static
Access scope
=============:
 We can access non static variables throughout our program
Where do we declare non static variables?
After starting the class immediately we will declare non static variables directly
Default values:
Initialization of variables is optional. If we don’t perform initialization of variables JVM is going to assign the default values.
Ex:   public class TestDemo
{
Int x;
Public static void main (String [] a){
TestDemo testDemo = new TestDemo ();
System.out.println (testDemo.x);//0
}
}
Default values:
Int   0
Float  0.0
Double 0.0
String   null
Char null
Byte  0
Short  0

Non static variables are also called as instance variables or object level variables
Non static variables will be stored in heap area as the part of object

2. Static variables:
If the value of a variable is not varying from object to object then declare that variables as static variables.
For static variables at class level a single copy will be created and shared across every object of that class
Whenever we declare variable as static we can access that variables without creating object and we can access by directly, class name and by object
Access specifier static Datatype variable name = variable value;
When static variables will be created?
Static variables will be created at the time of class loading and destroyed at the time of class unloading
When do we declare a variable as static::
=======================================
Whenever the value of a variable is not changing from object to object then declare such type of variables as static variables

Access scope
=============:
 We can access static variables  throughout our program
Where do we declare static variables?
After starting the class immediately we will declare non static variables directly with static keyword
Default values:
Initialisation of static variables is optional. If we don’t perform initialisation of variables JVM is going to assign the default values.
Ex:   public class TestDemo
{
Static String x;
Public static void main(String[] a){
System.out.println (x);//null
}
}
Static variables are also called as class level variables
Static variables will be stored inside method area

3. Local variables:
Whenever we declare variables inside the method or constructors such type of variables we can call it as local variables.
When local variables will be created?
Local variables will be created at the time method or constructor creation and method or constructor execution is done local variables will be destroyed
When do we declare a variable as local:
=======================================
Whenever you need the values for certain portion of area to meet requirements then declare that variables as local variables

Access scope
=============:
 We can access local variables within that method or constructor but not outside of that
Where do we declare local variables?
We need to declare local variables within the method or constructors and blocks
Default values:
For local variables compulsory we must and should perform initialization otherwise we will get compile time error. I.e. For local variables JVM won’t provide any default values.
Ex:   public class TestDemo
{
Public static void main(String[] a){
Static String x;
System.out.println(x);//compile time error
}
}

Local variables will be stored in stack memory
Local variables are also called as temporary variables or automatic variables or stack variables
Object type variables:
By using object type variables we can refer objects.

SeleniumDemo demo = new SeleniumDemo();

Session mechanism in selenium webdriver

Session mechanism in selenium web driver:
=================================

Why do we need Session Handling?
During test execution, the Selenium Web Driver has to interact with the browser all the time to execute given commands. At the time of execution, it is also possible that, before current execution completes, someone else starts execution of another script ,in the same machine and in the same type of browser.

In such situation, we need a mechanism by which our two different executions should not overlap with each other. This can be achieved using Session Handling in Selenium.

Example:
=========
package com.rs.blog;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;

public class SessionMechanism {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
SessionId session1 = ((RemoteWebDriver) driver).getSessionId();
System.out.println("FIRST SESSION ID: " + session1);
// System.out.println(s.toString());
WebDriver driver1 = new FirefoxDriver();
SessionId session2 = ((RemoteWebDriver) driver1).getSessionId();
System.out.println("SECOND SESSION ID: " + session2);
WebDriver driver2 = new FirefoxDriver();
SessionId session3 = ((RemoteWebDriver) driver2).getSessionId();
System.out.println("THIRD SESSION ID: " + session3);
}
}


How to execute scripts in parallel in selenium

How to execute scripts in parallel in selenium:
===================================
By using testing.xml we can execute the test cases parallel

TestOne.java:
=============
package com.rs.blog;

import org.testng.annotations.Test;

public class TestOne {

       @Test
       public void scriptOne() {
              System.out.println("script one");

       }

}

TestTwo.java:
===============
package com.rs.blog;

import org.testng.annotations.Test;

public class TestTwo {

       @Test
       public void scriptOne() {
              System.out.println("script one");

       }

}

Testing.xml:
=============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="3" parallel="methods" >
  <test name="Test">
    <classes>
      <class name="com.rs.blog.TestTwo"/>
      <class name="com.rs.blog.TestOne"/>
    </classes>
  </test> <!-- Test -->

</suite> <!-- Suite -->

Thread-count:  If the thread count is 3 so 3 web driver instances can run parallel in 3 different browsers
Parallel:
=======
The parallel attribute of suite tag can take three values:
1                              Tests: All the test cases inside <test> tag of testing xml file will run parallel
2                             Classes: All the test cases inside a java class will run parallel
3                              Methods: All the methods with @Test annotation will execute parallel










What is By in selenium webdriver

By class In selenium web driver:
========================
By is an abstract class and which is avalaible in org.openqa.selenium package.

These are the static methods present in By class and by using this classes we can identify web elements

1.public static By id(String id);
2. public static By name(String name);
3. public static By className(String className);
4. public static By cssSelector(String cssSelector );
5. public static By linkText(String linkText);
6. public static By partialLinkText(String partialLinkText);
7. public static By tagName(String tagName);

8. public static By xpath(String xpath);

Wednesday 20 July 2016

How to get hash code of the object:

How to get hash code of the object:
===========================
In java every object contains some unique number called hash code and it allocated to object by JVM.
In order to get hash code of the object ,Object (java. Lang package) class is having one method called hash Code().
Public int hashCode();
Whenever call hash Code() method we will get hash code of the particular object.

//Example on how to get hash code of the object
public class HashCode {

       @Test
       public void script() throws InterruptedException {
             
              WebDriver driver = new FirefoxDriver();
              driver.get("http://rameshselenium.blogspot.in/2016/02/fillo-api.html");
             
              //getting hashcode of the driver object
              int hashCode = driver.hashCode();
              System.out.println("hash code of the object is : " +hashCode());
              driver.quit();      
       }
}


Tuesday 12 July 2016

Browser refresh in different ways using selenium webdriver

Browser refresh in different ways using selenium web driver:
==============================================
1 1.  Refresh command: Most commonly used and simple command for refreshing a webpage.

Public class RefreshGetCurrentUrl {

       Public static void main(String[] args) throws Interrupted Exception 
{
              WebDriver driver = new FirefoxDriver ();
              driver.manage ().window ().maximize ();
              driver.manage ().deleteAllCookies ();
              driver.manage ().timeouts ().implicitly Wait (30, TimeUnit.SECONDS);
              driver.get ("http://rameshselenium.blogspot.in/");
              Thread. Sleep (2000);
              driver. Navigate ().refresh ();
              Thread. Sleep (2000);
              driver. Quit ();

       }
}

2.SendKeys command: Second most commonly used command for refreshing a webpage. As it is using a send keys method, we must use this on any Text box on a webpage.

public class RefreshSendKeys {
       public static void main(String[] args) throws InterruptedException {
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
              driver.manage().deleteAllCookies();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://rameshselenium.blogspot.in/");
              Thread.sleep(2000);
              driver.findElement(By.id("ramesh")).sendKeys(Keys.F5);
               Thread.sleep(2000);
              driver.quit();

       }
}

3.    To command: This command is again using the same above concept. navigate( ).to( ) is feeding with a page url and an argument.

public class RefreshGetCurrentUrl {
       public static void main(String[] args) throws InterruptedException {
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
              driver.manage().deleteAllCookies();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://rameshselenium.blogspot.in/");
              Thread.sleep(2000);
       driver.navigate().to(driver.getCurrentUrl());
              Thread.sleep(2000);
              driver.quit();

       }


4.  Get command: This is a tricky one, as it is using another command as an argument to it. If you look carefully, it is just feeding get command with a page url.

public class RefreshGetCurrentUrl {
       public static void main(String[] args) throws InterruptedException {
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
              driver.manage().deleteAllCookies();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://rameshselenium.blogspot.in/");
              Thread.sleep(2000);
              driver.get(driver.getCurrentUrl());
              Thread.sleep(2000);
              driver.quit();

       }
}