Friday 21 March 2014

Random

WebDriver driver=new FirefoxDriver();
driver.get("http://www.donebynone.com/?gclid=CLXXyPaikb0CFedV4godlE0Azw");
driver.manage().window().maximize();

Random randomGenerator = new Random();
java.util.List<WebElement> ll=driver.findElements(By.xpath("html/body/header/div[4]/div[1]/nav/section/ul/li"));
int la=ll.size();
System.out.println(la);
int ran1=randomGenerator.nextInt(la)+1;

System.out.println("ran1..."+ran1);
java.util.List<WebElement> l=driver.findElements(By.xpath("html/body/header/div[4]/div[1]/nav/section/ul/li[1]/ul/li[2]"));
int a=l.size();


System.out.println("szeTwo"+la);
System.out.println("szeOne"+a);

int ran2=randomGenerator.nextInt(a)+3;

System.out.println("ran2..."+ran2);
WebElement web=driver.findElement(By.xpath("html/body/header/div[4]/div[1]/nav/section/ul/li["+ran1+"]/ul/li["+ran2+"]/a"));
// WebElement web=driver.findElement(By.xpath("html/body/header/div[4]/div[1]/nav/section/ul/li[5]/ul/li[4]/a"));
WebElement web1=driver.findElement(By.xpath("html/body/header/div[4]/div[1]/nav/section/ul/li[1]"));
//WebElement web=driver.findElement(By.xpath("html/body/header/div[4]/div[1]/nav/section/ul/li[5]/ul/li[4]/a"));
Actions act=new Actions(driver);
act.moveToElement(web1).build().perform();
Thread.sleep(5000);
web.click();
System.out.println("over");

Monday 17 February 2014

Reading and writing data from Excel using Selenium Web driver

This code will work for reading data from excel sheet :

import java.io.IOException;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
@Test
public class Xlsheet {
public void asd() throws IOException, BiffException, Throwable{
Workbook workbook = Workbook.getWorkbook(new File ("FilePath"));
System.out.println(workbook);
Sheet sheet = workbook.getSheet("Sheet1");
System.out.println(sheet.getRows());

String s = sheet.getCell(1,1).getContents();
System.out.println(s);
}
}
This code will work for Writing data into excel sheet :

package sample;
import java.io.FileOutputStream;
import org.testng.annotations.Test;
import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.Label;
@Test
public class ExcelWrite {
public void aa() throws Throwable{

//Creating an excel and naming it. 

FileOutputStream exlFileName= new FileOutputStream("Filepath");

//Creating an instance for the above excel.

WritableWorkbook exlWorkBook = Workbook.createWorkbook(exlFileName);

//Creating Writable work sheets for the above excel.

WritableSheet exlWorkSheet1 = exlWorkBook.createSheet("Shivaprsad",0);
WritableSheet exlWorkSheet2 = exlWorkBook.createSheet("Suresh",1);

//Creating data(cell values) into above excel workbook

Label Sheet1cellContent = new Label(0,0,"Test sheet1");
Label Sheet2cellContent = new Label(0,0,"Test sheet2");

//Adding cell value to its respective sheet.

exlWorkSheet1.addCell(Sheet1cellContent);
exlWorkSheet2.addCell(Sheet2cellContent);

//Writing the values into excel.

exlWorkBook.write();

//Close the workbook

exlWorkBook.close();
}
}
   
        

Monday 27 January 2014

Verification points in Selenium ...

1.    To check Element Present:
if(driver.findElements(By.xpath("value")).size() != 0){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
Or
if(driver.findElement(By.xpath("value"))!= null){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
2.    To check Visible:
if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
System.out.println("Element is Visible");
}else{
System.out.println("Element is InVisible");
}
3.    To check Enable:
if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
System.out.println("Element is Enable");
}else{
System.out.println("Element is Disabled");
}
4.    To check text present:
if(driver.getPageSource().contains("Text to check")){
System.out.println("Text is present");
}else{
System.out.println("Text is absent")


Thursday 2 January 2014

Select methods in selenium Webdriver...

Select Methods
The following are the most common methods used on drop-down elements.

selectByVisibleText() :
deselectByVisibleText():

Selects/deselects the option that displays the text matching the parameter.
Parameter: The exactly displayed text of a particular option

selectByValue()
deselectByValue()

Selects/deselects the option whose “value” attribute matches the specified parameter.
Parameter: value of the “value” attribute remember that not all drop-down options have the same text and “value”, like in the example below.

selectByIndex()
deselectByValue()


Selects/deselects the option at the given index.
Parameter: the index of the option to be selected


isMultiple()

Returns TRUE if the drop-down element allows multiple selections at a time; FALSE if otherwise.
Needs parameters needed

deselectAll ():


Clears all selected entries. This is only valid when the drop-down element supports multiple selections.

No parameters needed

Navigate commands in Selenium Webdriver...


Navigate commands:
 These commands allow you to refresh go-into and switch back and forth between different webpages.

navigate ().to() :        It automatically opens a new browser window and fetches the
                                 page that  you specify inside its parentheses.

                                It does exactly the same thing as the get() method.

navigate().refresh() : It refreshes the current page


navigate().back():       Takes you back by one page on the browser’s history.

navigate().forward():  Takes you forward by one page on the browser’s history.

Tuesday 26 November 2013

How can I read background-color of an element in selenium Webdriver?



driver.get("http://www.google.co.in/");
String color = driver.findElement(By.name("btnK")).getCssValue("background-color");
   
System.out.println("The background color of Google search button"+color);

Wednesday 6 November 2013

How to find child elements in Selenium Web driver




When same object is getting duplicated for N number of parent, then user can refer parent and child relationship to identify the object in browser.

WebElement bodyElement = driver.findElement(By.id("body"));
WebElement bodyElement1 = bodyElement.findElement(By.id("body"));
System.out.println(bodyElement1.findElement(By.id("TotalScore")).getText());

User want to read all child elements of div(parent) container by using following methods.

WebElement bodyElement = driver.findElement(By.id("body"));   
List divElements = bodyElement.findElements(By.className("div"));
for(WebElement childDiv : divElements){
 List fieldSetEle = childDiv.findElements(By.tagName("fieldset"));
 System.out.println(childDiv.getTagName()+":"+fieldSetEle.size());
    }