Monday 28 April 2014

Constructor,Super(),this() in java



->The constructor is the first block of code. its called once the object created.

-> Constructor executes only once in the object lifecycle. its not execute after that.

-> it has the same name as the class name.

-> it can accept some arguments.

-> There is no return type for constructor. not even void.

-> if the class doesnot have the constructer, the default constructor will be generated by compiler with empty argument list. so only below code get execute.

car c new car();

-> if the class have any constructor, then its should match with constructor argument list and argument list while create object both should same, If it not match with any constructor argument then that statement will not get compiled.

car c new car(); //its not execute if class have any constructor.

car c new car(a,b); //Its execute if matched with constructor argument.




Super and this keyword

-> Super() is used to call parent class constructor.
this() is used to call same class constructor.

-> Super (with out paranthesis) is used to call parent class methods and variables.
this (with out paranthesis) is used to call same class methods and variables.

-> Both this and super not come together.

-> Super() and this() should be mentions in first line of constructor.



Java Arrays



Collection of similiar data types.

1) Declaring Arrays

int[] marks;

Also int marks[]; can possible. but its less readable. so we should not use this format.

2) Construction

marks = new int[5];

Array is the object. so we should use "new" keyword for construct the array. The size of the array is mandatory.

int[] marks=new int[5];

3) Initialization:

marks[0]=34;
marks[1]=37;
marks[2]=32;
marks[3]=30;
marks[4]=31;

Array index starts with 0 and ends with indexvalue-1.
We cannot use negative or maximum index value.


4) Initialization of objects

class pen {
pen[] pens=new pen[3];
}

we can create three object for above statement like pens[0],pens[1],pens[2].


5) Array intializer

if we know the values, we can give the value directly.

int[] marks={12,45,23,67}

Java Packages



-> Java pakages is a machanism for oraganizing java classes.

-> Pakaging name should start with reverse of our domain name.

Ex: The pakage for arunrajvdm.blogspot.com\drafts is

Pakage name = com.blogspot.arunrajvdm.drafts

-> Pakage statement should be on top of the java class file.

-> "import" statement is helpful to add pakage in class file.

-> There is no statement allowed in between the pakage statement and import statement while create the pakage.

Pakage com.mail;  \\
import com.blogspot.arunrajvdm.drafts; \\ here drafts is class. com,blogsport,arunrajvdm are pakages.

public class calc{
calc(){
System.out.println("hi");
}
}

Here not statement allowed in between import and pakage.


-> com.blogspot.arunrajvdm.*; - this statement says we can access all the available class inside the arunrajvdm pakage. But not sub pakage inside the arunrajvdm pakage.

Java Non Access modifiers



1) Final

-> Final can be applied in class, Methods, instance variable and local variables

-> If class marked as final, we cannot extended. That means we cannot create suib class for that.

-> IF method marked as final, we cannot override the method.

-> If variable marked as final, we cannot change the value.

-> If object reference as final, we cannot asign the new object or any value into the reference.


2) Static

-> Static can be applied for methods and variables.

-> If we are using static for methods or variables, all the objects are sharing the same memory location and refer those values. If we are not using static for methods or variablese, all objects create a new memory location and use those referances.

-> The methods or variables marked as static belongs to the class file memory location.

-> The static memeber should be accessed using the class name.

Ex:

public class ford{
public static void printCarInfo() {
System.out.println("Car Name: Ford);
}
}

Public class fordshowroom{
ford.printcarifo();
}

The above example method printcarinfo() is just print the message. If we are using object for this method, its crreates the seperate memory location for all instance for doing the same process for print those same message. So its waste of the memory location.

But if we are using the static for above method, all the object sharing the same memory location for print the same line of message. Static methods and static variables using .class file memory location. This is one advantage of save the memory allocations.

-> The static variables and methods are called class members. Because both are using the .class file memory location. But non-static variables and non-static methods are called as object members. Because all thos non-static variables referring the respective object memory location.

-> The method marked as static, they can access the other static methods and variables directly inside the same class.

-> Any object instance access the static variables, But the changes will reflect to all the objects.

-> Members marked as static can be final.



3) abstract

-> abstract can be applied for classes and methods only.

-> When a method is marked as abstract- Its should not have any implementation. So its end with ';'. So we have implemenation in sub class. So the abstract method should overriden in subclass or should marked as abstract.

-> when class is marked as abstract- we are not able to create object for those class. But we can create sub class and use those abstract class members.

-> if we have single abstract method in class, we should marked the whole class as abstract.

-> But the class can be marked as abstract even we dont have any abstract method in those class.

-> Abstract class can have concrete class(Non-abstract) methods on it.

-> Abstract methods cannot be marked as final.

-> Abstract class cannot be marked as final.

-> Abstract methods cannot be static.

-> Abstract methods cannot be private.

-> we cannot create object (instance) of abstract class. because abstract class have the incomplete method. its does not have the fully implemented method. so if we call the method by using object, its means incomplete task. so we cannot create instance for abstract class.

4)Strictfp

-> Strictfp is declared for methods and classes. Its have the implementation.

-> when declared inside a class or method, its confirm that those class or methods followed IEEE754 standared while writing code. and which makes methods or class behave in a platform independent way regarding the floating points.

-> strictfp cannot be used withh abstract.


5) native

-> Native modifier can only be applied to methods.

-> native method will always platform dependent code like C.

-> Its doesnot have any implementation. Its end with ;.

-> The native method implementation is omitted.


6) synchronized 

-> Synchronized can only be applied for methods.

-> Synchronized only allow one single thread to execute the code at a time.

-> Synchronized can be used with any access modifier.


7) transient

-> only instance variable marked as transient.

-> The variable marked as transient will not be serialized.


8) Volatile

-> only instance variable marked as volatile.


The following access and non access modifiers applicable for below item

Class - public,default,abstract,strictfp,final

Methods - public,protected,default,private,static,final,strictfp,native,synchronized,abstract

Instance variable - public,protected,default,private,static,final,transient

Local Variable - final

Access Modifiers in JAVA


1) Access modifiers helps to implement concept of encapsulation in oops.

2) Encapsulation helps to hide the data and behavior.

3) Access modifiers in java are
-> public
-> Private
-> Protected
-> default

4) Access modifiers is not applicable for local variables.

5) Access modifiers effect the accessbility in two levels
-> class
-> Members (Method and instance variable)



Access modifiers at class level

1) Public

-> when class is marked as public, it is visible to all other classes which are inside and outside

-> Its accessible to all class in other pakages, but should mention in import class.

-> There is no need import statement when a class used inside the pakage uses the class marked as public.


2) Default

-> Defult access has no keyword.

-> when there is no modifier declared, the access is defult level access.

-> The class marked with default access are accessible only to the class inside the same pakage. The outside pakage is not accessbile by the default modifier.


3) Private and protected

Private and protected is not applicable to class level declaration.



Access modifiers at member level

1) Public

-> The member with public class is access by any class inside and outside of the pakage.

-> if we want to use public member then we should import the pakage for the class.

-> If the class itself default modifier then we cannot use the public member in the class for outside the pakage. But we can use the default access modifier class have the public member inside the same pakage.



2) Default

-> The member with default access will be access to class that are in same pakage.

-> The member with default access will not be access to class that are in outside pakage.


3) Protected 

-> The member marked as protected is accessible all class in same pakage (same as default).

-> Protected members are also accessible by class outside of the pakage. But the accessing class should be a subclass of the member class.

-> Its used to acheive inheritance concept in java.


4) Private

-> When member marked as private, it is accessible only to the members inside the same class.

->  When member marked as private,other class inside and outside the pakage will not be able to access the private member of class.


Overloading in Java



1) overloading is applicable only in java.

2) Its applicable between the methods in same class or super class.

3) Overloading helps to implement the polymorphism concept in java.

4) overloading allows to define more methods in same name, so that its give many options to choose method.


overloading Rules:

1) overloading method argument types or argument number or return type should change in argument list.

2) Include anyone option in point (1), access modifiers also can change.

3) Overloading methods can have throws exceptions.

Ex:
class calc{
public int division(int a,int b)
{ c=a+b; return c; }
private double division(double a,double b)
{ c=a+b; return c; }
int division(int a,int b, int c)
{ d=a+b+c; return d; }
public int division(int a,int b)
{ c=a+b; return c; }
private double division(double a,double b,int c) throws ArithmaticException
{ c=a+b; return c; }
}



Overloading in Constructor:

1) constructor always can be overloaded.

2) overriding is not possble as oer rules of overloading.

Interfaces in JAVA




1) Interface does not have any method implementation.

2) All methods in an interface are public and abstract.

3) All the methods in interface should override in subclass. If sub class is not override interface means is called abstract class.

Ex: Public interface mother{
Public abstract void cooking();
}

    public class teacher implements mother
    {
Public void cooking(){
System.out.println("cook well");
}
    }

4)  we cannot use follwing access modifiers in interface methodPrivate,protected,final,static,synchronized,native,strictfp. That means interface method only allowed public and abstract.

5) By defaulr Interface variables are public,final and static

6) No multiple inheritance in java - Cannot extend more than one class at a time. But we can implements the more than one interface into the class.

Ex: The object teacher have many role(type) like teacher,Mother,housewife etc.

So its difficult to use all types in single class. Here we are using interface for implement those types.

Public class teacher extends mother implements housewife,citizen
{ }

So here we cannot extends more than one class at a time for reusability. But we can implements the more than one interface into the class.



7) Interface can extend any number of interfaces

Ex: Public interface car extends vehicle,fourwheeler{
}

Remember if we implements car interface we should override all interface like car,vehicle,fourwheeler


7) Any number of interface can be implemented by concrete class or abstract class.



8) Usage of Interface:

Ex: Selenium suports IE,Firefox,opera.. Here we can use common interface (WebDriver) for implement the function (Get()) for selenium interacts with muliple browser. In this way we can easily use the same variables and functions while developing the script.


Syntax:

Public interface calc{
int number=99;   // By default all fields are public and static
public abstract void add();
public abstract void add();
}



Interface


1) used to define the methods. Those methods are access by the class.
2) The main class able to access the all methods present in class and interface if we defined the object using class
3) But we not able to use methos which is not presented in interface if we defined object using interface.

public interface mobilphone

{
public void call();
public void recharge();
}

public class interfaceclass implements mobilephone

{

public void call()
{ system.out.println("call");

Public void recharge()
{ system.out.println("Rechaege");

public void ratecutter()
{ system.out.println("Rate cutter");

}


public class mainclass1
{
public static void main(String args[])
{
interfaceclass obj1=new interfaceclass();
obj1.call();
obj1.recharge();
obj1.ratecutter();


mobilphone obj2=new interfaceclass();
obj2.call();
obj2.recharge();
obj2.ratecutter();  // we not able to use methos which is not presented in interface if we defined object using interface.

}

Object Oriented Programming

The four feature of the object oriented programming

1) Inheritance
2) Abstraction
2) Encapsulation
4) Polymorphisam

1) Inheritance

we can use the property and behaviour from the main class to sub class using "extends" keyword.

Ex: if we want to write same moving behaviour as per the old car for new Ferrari car , we can extends the same behaviour from old car. So we can concentrate only writing code for the new behaviour. And we can inherite the pre writen functionality for make reuse of it. This is the use of Inheritance.

2) Abstraction:

-> Hiding the complexity
-> Composition
-> Aggregation

we can divide and segrigate the perticular related properties and behaviour into separate class. And we can reuse of it by creating the object for the perticluar class.

Ex: if we consider the Address class, its have proeprties of first line, second line, Street name, picode. We can create the seperate class and make reuse of another class.


3) Encapsulation:

-> Hiding

-> using Access Modifiers (Public, Default, Protected, Private)

THis mehod used for protect some Properties and behaviour of the some perticular object.

If we want Properties and behaviour of the some perticular object is should not used by other other object, that time we can use the Encapsulation for protecting the data.



4) Palymorphism

-> Overloading
-> Overriding

we can call the same function name in different place.





How java Platform independent language?


File.java -> JAVA COMPILER -> File.class -> JAVA VIRTUAL MACHINE -> Execution

1) .java flie convertied into .class file by using Java compiler. This .class file make readable and used by many operating system. .java is not readable for any operating system.

2) .class file converted into execution by using Java virutla machine.

3) Here Java virtual machine is not platform independent. Each and every operating system have the seperate Java virtual machine. But all java virtual machine can read and .class file and convert into appropriate operating systems.

Programming Types



1) Unstructured Programming -> No standared to write the program.

2) Structured Programming -> Follow some structured and standered to write the program.we can use label for this programming. But still here they have lack of reusability.

3) Procedural (Function) Programming -> we divide the program into mainidifferent procedure and make use of it. but its difficult to maintain those procedure in program.

4) Modular programing -> we can divide the procedure(function) as per the module and make reuse of it.

5) Object Oriented Programming -> Everything divided as a object. All objects have the PRoperties and Behaviour. Its used for identify those objects.

Ex 1: Car
Proeprties: Price, color,manufacturer
Behaviour : moving

The above informations are used to identify the perticular Pen(object).

Ex 2: Wheel
Properties : Price, color,manufacturer
Behaviour : Rotate

Car object used behaviour of Wheel object to completed the program. this is use of object oriented programming.

Saturday 12 April 2014

Working with excel using selenium webdriver (JXL.jar file)


Download  below JXL jar file and build with eclipse IDE for working working with excel using selenium webdriver (JXL.jar file)

http://www.findjar.com/jar/net.sourceforge.jexcelapi/jars/jxl-2.6.jar.html
http://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl
http://www.quicklyjava.com/jexcel-jar-download/


We will face below two issues while use JXL.jar file

1) Exception in thread "main" jxl.read.biff.BiffException: Unable to recognize OLE stream

-> if we are using .xlsx format(D:\Selenium_Testing\Selenium_Webdriver_Project\Login.xlsx), we will get this Exception.

-> The xls format (< Excel 2007) is comprised of binary BIFF data in an OLE container. The xlsx format (>= Excel 2007) is comprised of XML files in a zip container.

The Java Excel API only deals with the first format so it throws an exception when it doesn't encounter an OLE container.

You will need to restrict your input to xls files only or find another tool that handles both formats.

-> Apache POI handles both file types in Java: poi.apache.org/spreadsheet/index.html

http://stackoverflow.com/questions/5428794/biffexception-while-reading-an-excel-sheet


2) java.lang.ArrayIndexOutOfBoundsException - Its occures due to array doesnot initialize with any value.

-> If Excell cell does not have any value (Empty) means. its throw the above exception.






Jxl.jar in selenium webdriver


FileInputStream input1=new FileInputStream("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Login1.xls");
Workbook book1=Workbook.getWorkbook(input1);
Sheet Sheet1=book1.getSheet(0);
String Username=Sheet1.getCell(1,0).getContents();


-> getCell() - to retrive the vale from Excel cell.

Syntex :

1) getcell(ColNumber-1,RowNumber-1)

Example : getcell(2,5) -> Its rerun the value from Third col and six row.

OR

getcell(String Cell_Index)

Example: getcell("A1") -> Its return the value from A1.

**********************************************************

Read the Excel using selenium webdriver (JXL.jar Add on)

String First_name="Arun";
String Last_name="Raj";

FileInputStream input1=new FileInputStream("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Login1.xls");
Workbook book1=Workbook.getWorkbook(input1);
Sheet Sheet1=book1.getSheet("Sheet1");  // or getSheet(0); Sheet name or Sheet index starts from Zero.

String Username=Sheet1.getCell("A1").getContents();  //getCell("index of cell")
String Password=Sheet1.getCell(2,1).getContents();   /getCell(ColNumber-1,RowNumber-1)
String Password1=Sheet1.getCell(1,1).getContents();

System.out.println("UserName=" +Username);
System.out.println("Password =" +Password);
System.out.println("Password =" +Password1);

****************************************

Write the Excel using Selenium webdriver (JXL.jar Add on)

String First_name="Arun";
String Last_name="Raj";

FileOutputStream output1= new FileOutputStream("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Login1.xls");

WritableWorkbook wwb=Workbook.createWorkbook(output1);
WritableSheet ws=wwb.createSheet("Testdata1", 0);   //CreateSheet("SheetName","index") - If index <=0, add in front.
//if index>0, then sheet add as per the index.

Label l1=new Label(0,2,First_name);    //Label(ColNumber-1,RowNumber-1,Data)
Label l2=new Label(0,6,Last_name);

ws.addCell(l1);
ws.addCell(l2);
wwb.write();
wwb.close();

Referance = http://jexcelapi.sourceforge.net/resources/javadocs/current/docs/

**************************************

Write the data into different sheets of the same excel using selenium webdriver (Jxl.jar add on)


String First_name="Arun";
String Last_name="Raj
FileOutputStream output1= new FileOutputStream("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Login1.xls");

WritableWorkbook wwb=Workbook.createWorkbook(output1);
WritableSheet ws=wwb.createSheet("Testdata1", 0);
WritableSheet ws1=wwb.createSheet("Testdata2", 1);

Label l1=new Label(0,2,First_name);
Label l2=new Label(0,6,Last_name);

ws.addCell(l1);
ws1.addCell(l2);
wwb.write();
wwb.close();

PS: The above method is not appending the data in Excel. Those values are replaced by new one.

*****************************


Append data in Existing Excel using selenium webdriver (Jxl.jar Add on)


String First_name="Arun";
Workbook wb=Workbook.getWorkbook(new File("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Login1.xls"));
WritableWorkbook wwb=Workbook.createWorkbook(new File("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Login1.xls"), wb);
WritableSheet ws=wwb.getSheet(1);
Label l3=new Label(0,0,First_name);
ws.addCell(l3);
wwb.write();
wwb.close();


***********************

Jxl.Jar = Best for reading data from Excel.
Apache POI = Best for writing data into Excel.

********************

Apache POI

For Download = http://poi.apache.org/download.html

How to select perticular iterm from List box or drop down box using selenium webdriver


We can select the perticular item by using "value" or by using "Text" property. For identify those value for "Text" and "Value" prrperty we can use firebug.


Basic code

<option lang="LBLALL" value="" title="All">All</option>
<option lang="LBLDYNAMICREPORTS" value="1" title="Custom Reports">yahoo</option>
<option lang="LBLBASICREPORTS" value="2" title="Basic Reports">Google</option>


Example 1:(By using Value)

WebElement ReportCategory1=driver1.findElement(By.id("objectid"));
Select sel1=new Select(ReportCategory1);
sel1.selectByValue("2");



Example 2: (By using Text)

WebElement ReportCategory1=driver1.findElement(By.id("objectid"));
Select sel1=new Select(ReportCategory1);
sel1.selectByVisibleText("Google");


Example 2: (By using index)

WebElement ReportCategory1=driver1.findElement(By.id("objectid"));
Select sel1=new Select(ReportCategory1);
sel1.selectByIndex("2");



Example 4: (Get All values)

      
 We should use below method for get all values from drop down box
      
Coding :
      
<TD>
    <select name = "time_zone">
    <option value "-09:00"><script>timezone.Alaska</script></option>
    <option value "+00:00"><script>timezone.England</script></option>
    <option value "+02:00"><script>timezone.Greece</script></option>
    <option value "+05:30"><script>timezone.India</script></option>
   </select>
<TD>

Script

ArrayList allvalue=driver1.findElement("//select(@name='time_zone')").getText();

Difference between continue,return,break,exit statements


Continue = Its stop the current iteration and continue witn next iteration. That means its skip the remaining statements with in the braces ({}), and continue the process in next set of stataments.

Return = Its used to retun from the Loop statements. That means its not executed the remaining statements inside the loop and come out from the loop.

Break = Its used to come out from calling function. That means its not executed the remaining statements inside the calling function and come out from the function.

Exit = Its used to terminate the current Program or process.

Tuesday 8 April 2014

Generate Random Number in QTP

RandomNumber.Value(upperlimit,lowerlimit)

It would generate random numbers for that instance of QTP. But once QTP is restarted it will generate same numbers in same sequence. Example is shown in the images below.

Ex:

N=RandomNumber.value(1,15)

Its randomly pick the value from 1 to 15 and assign to N. Its randomly pick the any value for everytime QTP starts.

How to Start Selenium WebDriver

What is selenium.

-> born at 2005
-> Java script is main core for selenium. But its difficult to debug. SO they introdue for support many language like C#,PHP,Rupy,Phython.


Selenium RC

-> Its supports mozilla,IE,Chrome,Safari,Opera

Selenium IDE

-> only supports mozilla firefox. and provide Record and Run.
-> Its not support for dynamic web page.
-> Its selenium 1.0

GRID

-> We split whole test cases into seperate and run on different machine.
-> New version is Grid 2.0

Selenium Web Driver :

-> safari and opera is not supported.




Step 1) Download & Install Java on your Computer
Step 2) Install Eclipse on your computer
Step 3) Download Selenium Java Client Driver
Step 4) Configure Eclipse with Selenium WebDrive

Refer this link -> http://www.softwaretestingclass.com/how-to-run-your-first-selenium-webdriver-script-selenium-webdriver-tutorial/



    WebDriver driver1= new FirefoxDriver();
                UserDefindFunction obj1=new UserDefindFunction();
                driver1.get(baseUrl);
            */  
                // For Internet Explorer Browser
             
                System.setProperty("webdriver.ie.driver", "D:\\Selenium\\64 Bit\\IEDriverServer_x64_2.40.0\\IEDriverServer.exe");
                WebDriver driver1 = new InternetExplorerDriver();
                UserDefindFunction obj1=new UserDefindFunction();
                driver1.get(baseUrl);




How to develop the selenium script

-> Inall JDK,Eclipse ID and configure with Selenium
-> Download add-on for inspect the element as per the browser
-> And find the any unique value for any object in browser as per in the below preference

1) Id
2) Name
3) ClassName
4) Xpath
5) Css

-> And make sure those elements are unique by search those value by find button present in the Add ons.




Working with identify the objects in different browser

1)  Firebox Browser

-> Firebog or Xpather (Add on - need to download)
-> we can see this Add on in Tools-> Add on

2) IE Browser

-> IE Devloper Toolbar (No need to download. Available in latest version)
-> we can see this Add on in View-> Explorer bar-> IE Developer

3) Google Chrome

-> Chrome Devloper (Add on - need to download)

But we unable to find Xpath value using IE & Google Chrome Add on. So we should use Firebox Add on to find Xpath.




How to identify objects by location

-> As part of the selenium, the every object in web page treates as web Element. This concept is not like QTP. The QTP every object classified as the different classes like WebEdit,WebButton,WebCheckbox etc.


-> Below Five approach for identify objects uniquely


Id
Name
ClassName
Xpath
Css

The preference for for using above approache in codes  are from top to bottom.

-> If ID,Name,Class Name is not unique in the web page. Then only we should goto Xpath and CSS


How to verify the WebElement present or not in Webpage using selenium webdriver




The below code is used to identify the perticular web element is present or not in the webpage after moving from one page to another page.


 public void UserObject_Present(WebDriver driver1,String identify)
{
    boolean Result;

    do
    {
     try
    {
     WebElement elementt3= driver1.findElement(By.id(identify));
     Result=false;
    }
    catch (NoSuchElementException e)
    {
        Result=true;
    }
   
    }while(Result);
       
}

How to verify the WebElement Disabled or not in Webpage using selenium webdriver




-> we can use isDisplayed() function in Selenium webdriver for verify the Web Element Displayed or not in webpage.
-> Its return the boolean value.
-> Its returns "true" if WebElement Displayed
-> Its returns "false if WebElement not Displayed.



public void UserObject_Disabled(WebElement element1)
{
    boolean Result;
    System.out.println("Inside UserObject_Disabled");
    do
    {
   
     try
     {
        Result=element1.isDisplayed();
        if(Result)
            System.out.println("OBject Still display" );
        else
            System.out.println("OBject is not display");  
     }    
       
    catch (StaleElementReferenceException e)
    {
         Result=false;
         System.out.println("OBject is not display");  
    }
       
    catch (WebDriverException e)
    {
         Result=false;
         System.out.println("OBject is not display");  

    }while(Result);
       
}


But from above code, Selenium throw the exception if webElement is not displayed in web page. So i have included the above Exception handling

method for handle this situation.

How to verify the WebElement enabled or not in Webpage using selenium webdriver




-> we can use isEnabled() function in Selenium webdriver for verify the Web Element enabled or not in webpage.
-> Its return the boolean value.
-> Its returns "true" if WebElement Enabled
-> Its returns "false if WebElement not enabled.


public void UserObject_Enabled(WebElement element1)
{

     boolean Result;
     System.out.println("Inside UserObject_Enabled");
   
     do
     {
        try                
        {
        Result=element1.isEnabled();
        if(Result)
            System.out.println("OBject is Enabled" );
        else
            System.out.println("OBject is not Enabled");  
         }while(!Result);
       
}

Find the webElement inside the Frame using Selenium Web Driver



WebDriver driver1= new FirefoxDriver();
driver1.get("http://gmail.com");

WebElement element1= driver1.findElement(By.id("Frame1"));
driver1.switchTo().frame(element1);

WebElement element2= driver1.findElement(By.id("txtuid"));
element2.sendKeys("ARUNRAJVDM");

driver1.switchTo().defaultContent();


***********************************



How to identify Web Element inside frame in Selenium webdriver


-> we cannot direct access the web element which are inside <frame> or <iframe>.

-> we should use switchTo() method to access webelement inside the <frame> or <iframe>.


Sytex:

driver.switchTo().frame()


And below syntex for return back from frame tag.

driver1.switchTo().defaultContent()




We can identify frame and use driver.switchTo().frame() has below methods


1) driver.switchTo().frame(name_or_id)
Here your iframe doesn't have id or name, so not for you.

Ex:

<frame id="search123" frameborder="0" style="border:">
<a id="link1" href="http:gmail.com">
<span lang="LBLSHOVIDEOS">Selenium</span></a>
</frame>


Script :

WebElement element1= driver1.findElement(By.id("Frame1"));
driver1.switchTo().frame(element1);
driver1.findElement(By.xpath("//a/span").click
driver1.switchTo().defaultContent();





2) driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try driver.switchTo().frame(0)


Steps for know index of the Frame

1)goto html view

2)type iframe and find your required frame and count the value and switch to it using

oASelFW.driver.switchTo().frame(2);

if it is first frame then use oASelFW.driver.switchTo().frame(0);

if it is second frame then use oASelFW.driver.switchTo().frame(1); respectively




3) driver.switchTo().frame(iframe_element)

The most common one. You locate your iframe like other elements, then pass it into the method.


Ex:

<frame frameborder="0" style="border:">
<a id="link1" href="http:gmail.com">
<span lang="LBLSHOVIDEOS">Selenium</span></a>
</frame>


WebElement element1= driver1.findElement(By.xpath("/html/../Frame"));
driver1.switchTo().frame(element1);
driver1.findElement(By.xpath("//a/span").click
driver1.switchTo().defaultContent();



http://stackoverflow.com/questions/20069737/how-to-identify-and-switch-to-the-frame-in-selenium-webdriver-when-frame-does-no

Capture Screen or Screen shot of the full webpage using Selenium WebDriver



import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;

File sourcefile=((TakesScreenshot)driver1).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourcefile,new File("D:\\Selenium_Testing\\Selenium_Webdriver_Project\\Screenshot.png") );

We can see the screenshot output in above file location.

Working with Alert and Pop up window using selenium WebDriver



In web application mostly we see below two types of pop-ups,

1. alerts   ( only "OK" button )
2. confirm popup ( Both "OK" and "CANCEL" button)

Handling both alerts & confirms popup WebDriver has a Alert interface. Using WebDriver you can click on OK or Cancel button and also can get

alert message also to verify.

Logic

//First we need switch the control to alert.
       Alert alert = driver.switchTo().alert();
//Getting alert text
       alert.getText();
//Click on OK
       alert.accept();
//Click on Cancel
      alert.dismiss();

Finding the link inside Anchor, not with in the span (Using LinkTExt) using selenium webdriver


We can directly use the linkText() for finding the link inside <A> tag.


WebElement elementt5= driver1.findElement(By.linkText(identify));

Finding the link inside the span (Using Xpath) using Selenium WebDriver


The selenium driver is not find the link inside the span by using linktext() or partialLinkText(). SO we should use xPath for finding the link

which it was inside span by using below code.


1) WebElement elementt4= driver1.findElement(By.xpath("/html/body/div[5]/div/div[2]/div/div[2]/table/tbody/tr/td[3]/table/tbody[3]/tr[3]/td

[3]/div[2]/a/span[text()='Repot']"));


2) WebElement elementt4= driver1.findElement(By.xpath("/html/body/div[5]/div/div[2]/div/div[2]/table/tbody/tr/td[3]/table/tbody[3]/tr[3]/td

[3]/div[2]/a/span[@lang='REPOT']"));
                 

3) WebElement elementt4= driver1.findElement(By.xpath("/html/body/div[5]/div/div[2]/div/div[2]/table/tbody/tr/td[3]/table/tbody[3]/tr[3]/td

[3]/div[2]/a/span[contains(@lang,'REPOT')]"));