Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Friday, August 21, 2015

Basics of Selenium for Qtp/Uft Automation Testers

As a QTP/UFT automation tester you will feel java is difficult. But you have to understand the difference between freedom vs following standards. Once you follow the standards and explore little bit of oops concepts you will feel that Java is also a language that has more and more features than VBScript. Also the Open Source IDE's will make you feel better while writing java programs.

How to start with selenium
  1. Learn the basics of basics of java and eclipse
    1. Creating Java Project in Eclipse
    2. What is class file
    3. What is main method
    4. How to write different methods
    5. How to call methods in main method
    6. Syntax of Java
    7. Basics of HTML like Tags, Attributes
    8. Firebug
    9. Firepath
    10. Configuring Project with Selenium Libraries
If you are good at using vbscript class object concept then the above concepts are cakewalk for you. For others it will take maximum of an hour to understand clearly. There are so many sites available in internet which will give you spoon feed ways  to understand java basics.

After creation of project to write selenium tests the code goes like below.
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
Don’t get panic by seeing these two lines. So many lines will be generated while writing a program. These statements are like library loading statements in QTP. If we load a library we can call a function. Above two statements are mandatory if you want to automate application using Firefox browser

The code in class file looks like below 
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class RegisterUser {

 public static void main(String[] args) {

 }
}
A class is like a test and main method is driver for the class. So when you execute whatever is there in main method will be executed. Now we will start writing a small script for our very own flight reservation web application.
  1. Open Browser
  2. Navigate to http://newtours.demoaut.com/
  3. Click on Register Link
  4. Enter First Name, Last Name, Select Country, Give User Name, Password, Confirm Password and click on Submit image
Here we are going handle a link, text boxes, list box and image.

Opening Browser
To open a browser you must use below statement.
WebDriver driver = new FirefoxDriver();
Here driver is a variable. You can use this variable to do any operations in the browser.
WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
This will open Firefox and navigate to specific URL.

Clicking on Register Link
Now you have to handle an object. Remember that selenium doesn’t support all properties of an object like QTP. Here we call them as locators and you can use below locators
  1. Html id
  2. class name
  3. Name of an object
  4. Html tag name of an object
  5. css selector
  6. link text
  7. partialLinkText
  8. xpath
Though QTP supports xpath and Css very less people are using it. But when in selenium it’s common to use xpath. You can learn x-path from here.
Use below methods to click on link
driver.findElement(By.linkText("REGISTER")).click();
or
WebElement lnkregister=driver.findElement(By.linkText("REGISTER"));
lnkregister.click();
In first method we are finding the element using link text and clicking on it.
In Second method we are storing the link object in a variable and applying click operation on variable.
Driver.FindElement will return an element type object and you have to store it by specifying WebElement. This is similar to set keyword to store objects in vbscript.

Enter Text in First Name and Last name
driver.findElement(By.name("firstName")).sendKeys("qtp");
driver.findElement(By.name("lastName")).sendKeys("sudhakar");
We can use sendkeys to enter text on any object.

Select Item from List
Select lstcountry= new Select(driver.findElement(By.name("country")));
lstcountry.selectByVisibleText("INDIA");
You can use click and sendkeys directly on findelement. But when it comes to list value selection you must specify the type of object you are handling with.

The final program goes like below.
public class RegisterUser{

 public static void main(String[] args) {
  
  //Open Firefox browser
  WebDriver driver=new FirefoxDriver();

  //Specify Synchronization for Page Load
  driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);

  //Object Synchronization Timeout if object not avaible
  driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
  
  //Open Application URL
  driver.get("http://newtours.demoaut.com/");

  //Click on Register link
  driver.findElement(By.linkText("REGISTER")).click();  
  
  //Enter First Name and Last Name
  driver.findElement(By.name("firstName")).sendKeys("qtp");
  driver.findElement(By.name("lastName")).sendKeys("sudhakar");
  
  //Select Country
  Select lstcountry= new Select(driver.findElement(By.name("country")));
  lstcountry.selectByVisibleText("INDIA");
  
  //Enter Username, Password, Confirm Password
  driver.findElement(By.name("email")).sendKeys("qtpsudhakar");
  driver.findElement(By.name("password")).sendKeys("1234567");
  driver.findElement(By.name("confirmPassword")).sendKeys("1234567");

  //Click on Register Image
  driver.findElement(By.name("register")).click();
  
  //Close Browser 
  driver.close();
 }

}
In above program line number 8 and 11 will provide synchronization for page load and find object . These two are similar to sync method and object synchronization timeout in QTP.

Like this we can automate any functionality in application.

2 comments :

  1. Thanks Sudhakar. I'm planning to switch to Selenium from QTP. Your post is of help. I have worked in Java before, but from the code above I didn't see much of Java being used here, just a few basic things - import, class and main method. Is it always the case?

    ReplyDelete
  2. Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic.keep sharing your information regularly for my future reference.

    ReplyDelete