Wednesday, September 28, 2016

First Program using Selenium WebDriver and Java



First Program using Selenium WebDriver and Java

Note: Detailed explanation about Objects, Elements and more would be coming in next blog.
Focus in this post is to understand basic requirements and write sample program and execute
Now that we are done with Selenium WebDriver configuration (refer to my other post on Selenium WebDriver Configuration) it is time to write the First Script using Selenium WebDriver

Prerequisites in brief, before moving ahead with writing script
  • JDK and JRE: Java JDK (Installing JDK, by default JRE gets installed.) and JRE required as the code which we write for automation includes Java + Selenium WebDriver code 
  • IDE for development: Any IDE (Integration Development Environment) to write the code. IDE in simple terms, is an Editor which have more features and easily used for maintenance. We have various IDE’s available in market like Eclipse, IntelliJ, NetBeans to name few. Throughout this post, I have taken Eclipse IDE as example.  
  • Selenium WebDriver jars: Selenium WebDriver jars are required to automate the Web application, as mentioned we use Selenium WebDriver + Java combination for automation. Download jars from (www.seleniumhq.org) for Java, and assign to project 
  • Firefox Browser: Require Mozilla Firefox browser (preferable version 47) 
  • Firebug and Firepath: Add-In to the firefox browser which would be helpful in identifying the objects and writing xpaths (about xpaths, another post would be published soon) 
  • SUT: Software Under Test. In simple terms, application to be automated.  
 For this post, let’s take Facebook Login scenario as example

Step 1: Create a Project with Project name, create package, and create class. 

Step 2: Assign the Selenium WebDriver Jars which are downloaded in above Step3 and assign to the project, which would be imported while using the Selenium related jars.

Step 3: After creating the class, next step is to write the first Script to login into facebook application using Selenium WebDriver and Java

Below is the initial default code which would be generated after creating class.

package fbPackage;

public class FacebookLogin {

    public static void main(String[] args) {
        //This is place where we need to write the login to launch firefox browser and run code
    }

}



Code extension
package fbPackage;

public class FacebookLogin {

    public static void main(String[] args) {                        

         WebDriver obj=new FirefoxDriver();
         obj.manage().window().maximize();
         obj.get("https://www.facebook.com");
         obj.findElement(By.id("email")).sendKeys("XXXXXX");
         obj.findElement(By.id("pass")).sendKeys("YYYYYY");
         obj.findElement(By.id("u_0_n")).click();


    }

}


XXXXXX - User ID
YYYYYY - Password

Code explained line by line

WebDriver obj=new FirefoxDriver();
This line explains new FirefoxDriver which will launch a new Firefox browser. However once firefox browser is launched, so as to move ahead and perform operations on firefox browser (in simple terms how we use mouse pointer to move across and perform operations in similar way we need to create one object using which selenium script would be able to perform operations on browser which is launched with help of code new FirefoxDriver();

So, object which we created here is obj, which is of Class WebDriver. Basicalluy WebDriver class have all the methods / functions using which we can perform operations on Firefox browser. We can give any name to object. Say example as below








WebDriver obj=new FirefoxDriver();

WebDriver fb=new FirefoxDriver();
and more
 ***************************************************************************** 
obj.manage().window().maximize();
This method would be used to maximize the browser window size. 
 *****************************************************************************
obj.get("https://www.facebook.com");
This method is to enter the URL in the address bar. Need to give complete URL including https

  *****************************************************************************
Before getting into understanding about the below line of code, need to understand about findElement and By, which would be provided in another blog which would be coming soon.

obj.findElement(By.id("email")).sendKeys("XXXX");
 obj - used to manage the facebook.com opened page in this example

findElement - is method which is used in order to identify the web element in the page just similar to how we use our eyes to search for the user name and password fields.

By - is important aspect here, which will tell obj to identify the webelement using few of the given properties like id, name, classname, linktext, partialLinkText, TagName and more. Which are similar to how we identify human beings using properties like face, height, color, weight and more. However each webelement have same properties but different values as like how every human have same properties like face, height, weight and more with the values of those differ from person to person

id : is one property and we have few other like classname, linkText and more

sendKeys: Is used to send the value from the keyboard

XXXX - need to send the parameter / input. Here in this example it is user name

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

obj.findElement(By.id("pass")).sendKeys("YYYY");

Similar to what is explained in above example. Here used same findElement and id but value is 'pass' and in earlier example it was 'email' which vary each web element differently from each other
  
*****************************************************************************    
 obj.findElement(By.id("u_0_n")).click();

Again similar example however now on this webelement, instead of sending the keys we use click() and object is button and action to be performed is Click();
 *****************************************************************************


Run this script in Eclipse to automatically execute Facebook Login code

No comments:

Post a Comment