Thursday, October 31, 2013

Handle Multiple Windows

When we are testing web applications each web app will have different behavior. Sometimes when we click on a link it might get open in a new window or may be in the same page after refreshing the current page. 

When we are doing testing in these type of scenarios it is mandatory to know which window we are in. this can easily be handle with selenium inbuilt classes and functionalists.  below code illustrate how we can manage multiple window opening and how we can switch on them. 

//Goes to the site 
WebDriver A = new FirefoxDriver();  
A.get("http://www.hdfc.com/"); 
//Return a set of window handles which can be used to iterate over all open windows of  this WebDriver instance by passing them to
Set<String> windowIds = A.getWindowHandles(); 
Iterator<String> itr = windowIds.iterator(); 
//Prints the window IDs until the next value is not null 
while (itr.hasNext ()){ 
System.out.println(itr.next());
      }
A.findElement(By.xpath("//img[@title='InstantHomeLoan']")).click();
windowIds = A.getWindowHandles();
itr = windowIds.iterator();
//Assigning the window ID to String variable 
String mainWindowId = itr.next(); 
String secondWindow = itr.next();  
System.out.println("mainWindowId"+mainWindowId); 
System.out.println("secondWindow"+secondWindow);
//Switching to the new Window  
A.switchTo().window(secondWindow); 
A.findElement(By.linkText("Your Applications")).click();
A.quit(); 

in this code the main role is played by "getWindowHandles()" method. "getWindowHandles()" Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to "SwitchTo().WebDriver.Options.window()" and this will return A set of window handles which can be used to iterate over all open windows.

If you want to read further about the java methods, and classes please ref this link Selenium - Definition Of All Classes

Handle Drop down values (Locate, Print count and print values)

Drop downs are also another famous input type where you almost in every website. Drop downs are also treated as Lists in java. same as the radio button post we can extract the values in the drop down to a list and do out operations . Below code will illustrate how we can perform that 

WebDriver A = new FirefoxDriver();  
A.get("http://www.gic.gov.lk/gic/index.php?option=com_findnearest&task=train");
WebElement From = A.findElement(By.id("startStation"));

List<WebElement> Options  = From.findElements(By.tagName("option")); 
System.out.println(Options.size());

for (int i = 0; i < Options.size(); i++) {
System.out.println(Options.get(i).getText() );

}

You can use this same example in any of your scenarios, Just change the link and the locator accordingly.

if we want to pass any value to the drop down to be selected then we can use the value given in the tag itself.

A.findElement(By.xpath("//*[@id='startStation']")).sendKeys("AGT");






Input Values to radio buttons, Read and Print

In my previous post I've explained how you can input a value to a text box. Now lets see how we deal with radio buttons. 

when ever we give radio button as an options there is always more than one. So in java we have to treat these as "Lists". First we have to extract all the available options in radio button and do the task. So lets see how we can do that. 

WebDriver A = new FirefoxDriver();
A.get("http://echoecho.com/htmlforms10.htm");

//Locating the radio button which has the values "Milk" and cliks on it 
A.findElement(By.xpath("//input[@value='Milk']")).click();
//extracting all the radio button values to a list  
List<WebElement> B = A.findElements(By.xpath(".//td[@class='table5']//input[(@type='radio')]"));

//Prints the size of the list
int count = B.size();
System.out.println(count);

//Prints the values in the list 
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(x);
       }

In above example i'm simply calling a URL which has a radio button options in it and extracting all the radio button values to a List. 

Once we create a list we can use many type of functions to play with the data. Such as "add(), equals(), get(), isEmpty()" and so on. In this example to make sure that we have extracted the total number of radio button options i'm printing the size of the list. 

in order to print the values inside the list (Actual radio button values) we can simply iterate the list inside the for loop. 

for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(x);
       }

in above code i'm iterating the List elements to get the values one by one. Below code will assign the radio button value to variable "x" at each iteration. and "z.getAttribute("value"); "  and getAttribute method will get the value of the given parameter. in this example i'm retrieving the text value inside the "Value" attribute.

String x = z.getAttribute("value"); 


Check radio button checked status
Sometimes we might have to check the status of the radio button, whether those are pre checked or not. Below code will be use full to check that status, You can just copy and paste this to your same class file 

//Check if the values are checked at the initial open
for (int i = 0; i <B.size(); i++) {
if (B.get(i).getAttribute("Checked") != null)
System.out.println(B.get(i).getAttribute("value")+"Is Cheked");
else 
System.out.println(B.get(i).getAttribute("value")+"Is not checked");
}

If you want specifically click on specific radio button you can use below code. 

String Xpath3 = "//table[3]/tbody/tr/td/table/tbody/tr/td/input[1]";
WebElement elementToClick = A.findElement(By.xpath(Xpath3));
elementToClick.click();
  

or may be inside a loop 

for (int i = 1; i <B.size(); i++) {
if (B.get(i).getAttribute("Checked") != null){
WebElement elementToClick = A.findElement(By.xpath(Xpath1+i+Xpath2));
elementToClick.click();
     }      }

you Selenium experts might have better, easier ways of doing this, and i would love your command and to know other possible options. and please do comment if there is any code that i can improve of. 






Input Values to text field and search

In a web site there so many input types available, Text fields, Check boxes, radio button, Drop downs and many more, this will illustrate how we can pass value, Input values to a text field 

WebDriver A = new FirefoxDriver(); 
A.get("http://www.google.lk");

//Locate the google search field 
WebElement SearchField = A.findElement(By.id("gbqfq") );

//Type "Gmail"
SearchField.sendKeys("Gmail"); 

//locate the search button and hit ENTER
A.findElement(By.id("gbqfb")). sendKeys(Keys.ENTER );

In java "sendKeys" is a special command which we used to pass any type of a input values to any type of a input field. so in the 4th line of this java code i'm passing the text "Gmail" to search input field. 

Not only any input values, but also "SendKeys" command is compatible with some other types of commands as wel. as an example the 5th line of this code "sendKeys(Keys.ENTER );" This will give the same command as the keyboard Enter key. You can find out more about these type of easy java commands in the selenium web site.



Extract and print all the URL links in a website

Sometimes when we are testing web sites we need check how many active URLs are available, May be per user, per access group level.  So lets see how we can do that .  

Extract All the URLs.

WebDriver A = new FirefoxDriver(); 
A.get("http://www.google.com"); 
//Get all the URLs and create a list 
List<WebElement>URLS2 = A.findElements(By.xpath("//a[(@href)]"));
//Get the list size (URL Size)
int count2 = URLS2.size(); 

//Prints the list size 
System.out.println(count2);
//Access each list element and get the URL in a loop
for(WebElement a : URLS2) {
String link = a.getAttribute("href"); 
System.out.println(link);
   }

Above code will load the google page and will extract all the available URLs to a List. In java Lists are type of a data structure where we can store different set of data. 
List<WebElement>URLS2 = A.findElements(By.xpath("//a[(@href)]"));

In this line we are creating a List named "URLS2" and assigning all the "@href" links to it. I hope you are familiarize with what meant by  "xpath("//a[(@href)]")" else you can refer my blog post on "Xpath". 

when we assign the URL values to the list we can access them one by one. The loop does it. we pass the list size parameter and prints the URL values one by one.  the out put of this code will be like below 


Ill be posting how the dead links can be checked in future. 

Load a URL/ Web Page

In my previous post I've explained that how we can call the web browser to the java script. So now lets see how we can simply cal a URL / web page using java web driver. Its simple. 

WebDriver A = new FirefoxDriver();
A .get("http://www.google.lk ");

Like in the previous post you have to create a object from the "webDriver" class and call the URL with the get() method. you can pass any URL to get method, Or you can pass the variable name which is assigned to URL as well.

WebDriver A= new FirefoxDriver();
String URL = "http://www.google.lk";

A.get(URL);

In a same test script you can call all 03 browses as well.

WebDriver A= new FirefoxDriver();
String URL = "http://www.google.lk";
A.get(URL);
 
System.setProperty("webdriver.chrome.driver","D://Nayana//ActivePresenter//drivers//drivers//chromedriver.exe");
WebDriver B = new ChromeDriver();  
B.get(URL);
 
System.setProperty("webdriver.ie.driver","D://Nayana//ActivePresenter//drivers//drivers//IEDriverServer.exe");  
WebDriver C = new InternetExplorerDriver(); 

C.get(URL);

Wednesday, October 30, 2013

Import Web Browsers to the web Driver

To begin with the testing first we need to know how we can call the browsers to out java script. There are selected browsers that selenium supports, You can find them in here Third Party Browser Drivers

In this example ill show you how we can play around with the "Chrome, Firefox and IE" browsers since those are the mostly used browsers in the world. 

Firefox Driver 
WebDriver A = new FirefoxDriver(); 

The code given above will create a new object (A) from the "webDriver" class and assigns the "FirefoxDriver" to it.  unlike the other browsers we do not have to set the system properties as it is in build in the Firefox browser itself. 

Chrome Driver
System.setProperty("webdriver.chrome.driver","D://User//ActivePresenter//drivers//drivers//chromedriver.exe");
WebDriver B = new ChromeDriver()

To run scripts with the chrome driver we have to download the supported third party browser drivers from selenium.  (Ref below screen dump)


once you downloaded you have to provide the extracted driver location in the System.setProperty clause.  and just like in the firefox we are creating a new object from the WebDriver class and assigning the chrome driver to it. 

IE Driver
System.setProperty("webdriver.ie.driver","D://Nayana//ActivePresenter//drivers//drivers//IEDriverServer.exe");  
WebDriver C = new InternetExplorerDriver(); 

To run scripts with the Internet explorer driver we have to download the supported third party browser drivers from selenium.  (Ref below screen dump)


once you downloaded you have to provide the extracted driver location in the System.setProperty clause.  and just like in the firefox we are creating a new object from the WebDriver class and assigning the InternetExplorerDriver to it. 

All in one 

WebDriver A = new FirefoxDriver();     
System.setProperty("webdriver.chrome.driver","D://Nayana//ActivePresenter//drivers//drivers//chromedriver.exe");
WebDriver B = new ChromeDriver();    
System.setProperty("webdriver.ie.driver","D://Nayana//ActivePresenter//drivers//drivers//IEDriverServer.exe");  

WebDriver C = new InternetExplorerDriver(); 

I hope this gives a clear idea on how you can import the browser to the web driver, Read my next blog post to see how you can call a url from a given browser.

All about Java Web Driver

To Start with the java web driver first we have to install "Eclipse". I hope you know how to install "Eclipse" if you are not please follow below steps to configure it. 
How to Install Eclipse

If you are done setting up "Eclipse" in your PC then now we can start configuring the java web driver. To start of first we have to download the "Selenium client and Web Driver Language Bindings" from "Selenium" site. You can find the java Web Driver downloadable file form here Selenium Java WebDriver Language Bindings


When you download this and extract it you will see couple of JAR files. These are the foundations classes, and functions of selenium testing. These needs to be extracted to "Eclipse"

You can click on your Project folder--> Properties -->  Java Build Path --> Libraries --> Add external jars -- >  Add the downloaded JAR files and all the library files.(Ref below screen dump)




 And now you are ready to test your java scripts 








Getting start with Selenium IDE

when i started to learn about the test automation tools i kinda attached to pure coding with java web driver. That was so interesting and logical to learn. But as a starter i have to learn how Selenium IDE works too. So here i'm explaining some basic functionalists that we can do with IDE to play around with the tool 

Basically IDE is used to automate the manual testing functionality in web based applications. This is mostly suitable for long term   and rapidly changing type of a projects. IDE supports many types of languages, such as XML, HTML, JAVA, C# and many more. 

it already has pre-configured parameters as commands where users can simply call them or users are free to define there own command as well. If a user wants to add any command then he/she can write the function in any  language like java and that can be added from "Core extensions" section in the tool 

Below is the tool that you get


as given in the diagram when we want to record a rest case or an activity that we do on the browser we can simply click on the record button, so that all the activities will be recorded in the IDE step by step. Later this can be extracted as a text, XML or HTML file. 

Once the recording is done a test suite will be generated, If the user wants he/ she can again change the order or the command of the test case. 

Here are some example scenarios 
01.Calling a URL and input some value
Calling the "www.google.lk" and search something. You can open your IDE and type the same commands and see :) 

Loggin Scenario
openhttp://www.google.lk
typeXpath=.//*[@id='gs_htif0']
typeid=gbqfqThis is a test scenario
clickAndWaitXpath=.//*[@id='gbqfb']

02. Mouse over actions 
This example is to handle mouse over menus and selects a sub menu 

MouseOver
openhttp://ibuy-shop.com/
mouseOverXpath=.//*[@id='header']/div[2]/ul/li[1]/a
mouseOverXpath=.//*[@id='header']/div[2]/ul/li[1]/ul/li[2]/a
clickAndWaitXpath=.//*[@id='header']/div[2]/ul/li[1]/ul/li[2]/ul/li[1]/a

03. Select a data from date picker and a value from drop down 
when we want to select a value from a drop down menu or from a date picker java calender script we can use the same method of sending the value as a text to the xpath

DateAndDropDownValuePicker
openhttp://www.gic.gov.lk/
clickAndWaitXpath=.//*[@id='btn_menu']/ul/li[3]/a/img
clickAndWaitXpath=.//*[@id='ja-col']/div[3]/table/tbody/tr[5]/td[3]/a/span
typeXpath=.//*[@id='startStation']
typename=startStationABL
typeXpath=.//*[@id='endStation']
typename=endStationFOT
typeXpath=.//*[@id='datepicker']
typename=datepicker2013-10-23
clickAndWaitXpath=.//div[2]/table/tbody/tr[9]/td[2]/input






Getting touch with Selenium - IDE

Selenium IDE is a Firefox plugin where you can add on to your browser and record user activities and play them back. Most of the time this feature is used to create simple test scripts which can be automated. 

You can download the latest version from here Download Selenuim IDE .


once you download the browser will as you to install the plug-In


Once you allow the access it'll ask to install 05 add-ons to your browser. 

And then you have to restart the browser to let it apply 


If you can see the selenuim IDE options under tools in the browser options then you are good to go 



Thursday, October 24, 2013

Working in a new place will lead you to learn new things :)

Actually it was the third day of work at my new place where my boss talked to me and explained about the test automation. Well as a Business analyst in my past experience i have never done QA or any sort of testing, That because of the organisational structure at my previous work place. 

but when you move to a new place you have to adapt to the culture of it. So i tot of giving a try to learn this test automation tool these people are talking about. Well at the end of the day it'll be a win win situation where company gets what they want and i will be equipped with new skill set.