Uft
Uft
Uft
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;
Dont 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.
Navigate to http://newtours.demoaut.com/
WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
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");
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();
}
}