Writing data to text file using selenium webdriver

package selenium_examples;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test{
 public static void main(String[] args) throws IOException {
 
   
   BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test.txt"));
   bw.write("Helloooo");
   bw.newLine();
                 bw.write("This is a new line");
                 bw.newLine();
                 bw.write("One more new line");
                 bw.close();
                 System.out.println("Done!");
 
   }
 }

Script 2:-

package selenium_examples;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
 public static void main(String[] args) throws IOException {
 
  WebDriver driver = new FirefoxDriver();
         driver.get("https://www.google.com");
         String s=driver.getTitle();
    
  BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test1.txt"));
  bw.write(s);
         bw.close();
                System.out.println("Done!");
 
   }
 }

 

Selenium Webdriver Scripts - Executing multiple sql queries using JDBC

package selenium_examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
 public static void main(String[] args) throws ClassNotFoundException, SQLException {
  Class.forName("org.postgresql.Driver");
  Connection con = DriverManager.getConnection(dbUrl,username,password);
                //dbUrl="jdbc:postgresql://hostname:port/dbname"
  System.out.println("Connected to Database");
  Statement stmt =con.createStatement();
  Statement stmt1 =con.createStatement();
  ResultSet rs = stmt.executeQuery("select id from emp");
  ResultSet rs1 = stmt1.executeQuery("select deptno from dept");
 
  while (rs.next()) {
  
             int x = rs.getInt("id");
             System.out.println("id :"+ x);
 }
  while (rs1.next())
  {
  
            int z = rs1.getInt("deptno");
            System.out.println("deptno :"+ z);
 }
  con.close();
  System.out.println("Connection Closed");
 }

}


 

Selenium webdriver - How to get inner text of the cell of a html table

package selenium_examples;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Example {
 public static void main(String[] args)
     {

  WebDriver driver = new FirefoxDriver();
 
  driver.get("http://www.w3schools.com/html/html_tables.asp");
 
  String text=driver.findElement(By.xpath("//table/tbody/tr[3]/td[2]")).getText();
 
  System.out.println(text);
 
  }

Output:- Jackson
Above script gets 3rd row,2nd column cell text from below shown table.

working with webtables selenium webdriver
 

Reading data from notepad/text file using selenium webdriver

package selenium_examples;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test{
 public static void main(String[] args) throws IOException {
 
          
         FileReader r = new FileReader("E:\\test.txt");
         BufferedReader bfr = new BufferedReader(r);
         String x ="";
       
         while((x = bfr.readLine()) != null){
             System.out.println(x);
 
   }
         bfr.close();
 }
}
 

How to write data to doc file using selenium webdriver

Writing data to doc file using selenium webdriver

package selenium_examples;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Test{
 public static void main(String[] args) throws IOException {
 
           
    File file = new File("E:/test.doc");
    if (!file.exists())
        {
      file.createNewFile();
     }
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("abcd");
    bw.close();
   
   
 }
}

Script2:--
package selenium_examples;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test{
 public static void main(String[] args) throws IOException {
 
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.google.com");
  String s=driver.getTitle();
  System.out.println(s);
           
    File file = new File("E:/test.doc");
    if (!file.exists())
        {
      file.createNewFile();
     }
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(s);
    bw.close();
   
   
 }
}
 

How to write data into excel sheet using selenium webdriver

package selenium_examples;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Label;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
 public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.funandknowledge.blogspot.com");
  String s=driver.getTitle();
  System.out.println(s);
 
  FileOutputStream f = new FileOutputStream("E:\\Test.xls");
         WritableWorkbook book = Workbook.createWorkbook(f);
         WritableSheet sheet = book.createSheet("Output",1);
         Label l = new Label(0,0,s);
         sheet.addCell(l);
         book.write();
         book.close(); 
 }
}

Output:-QTP Scripts and Selenium Webdriver Scripts

Above script writes the title of website "http://www.funandknowledge.blogspot.com" to the excel sheet.

 

How to send keyboard keys combination in selenium webdriver


package selenium_examples;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Example {
simulating keyboard keys webdriver
 public static void main(String[] args)
     {

  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.google.com");
  driver.findElement(By.name("q")).sendKeys(Keys.NUMPAD1,Keys.ADD,Keys.NUMPAD2, Keys.ADD, Keys.NUMPAD3, Keys.EQUALS,Keys.ENTER);
 
  }
 
  }
   
Script2:-
Script in Selenium Webdriver to Simulate CTRL + A,to select all content in Webpage.
package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
 public static void main(String[] args) {
 
 
  WebDriver driver = new FirefoxDriver();
     driver.get("http://google.com");
     driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a"));
 
   
  }
}

 

How to Read data from doc file using selenium webdriver


Reading data from doc file using selenium webdriver.

package selenium_examples;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test{
 public static void main(String[] args) throws IOException {
 
  FileReader r = new FileReader("E:\\test.doc");
        BufferedReader bfr = new BufferedReader(r);
        String x ="";
      
        while((x = bfr.readLine()) != null){
            System.out.println(x);
 
   }
        bfr.close();
   
   
 }
}

 

How to get Row Count in a table using selenium webdriver

Script to count no. of rows in a table using Selenium Webdriver
package selenium_examples;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Example {
 public static void main(String[] args)
     {

  WebDriver driver = new FirefoxDriver();
 
  driver.get("http://www.w3schools.com/html/html_tables.asp");
 
  WebElement table_element = driver.findElement(By.xpath("//html//body/table"));
       Listtr_collection=table_element.findElements(By.xpath("//html/body/div[3]/div[1]/div/div[2]/table[1]/tbody/tr"));
     System.out.println("NUMBER OF ROWS IN TABLE = "+tr_collection.size());
 
 
  }
 
}

Output:- NUMBER OF ROWS IN TABLE = 5

 

How to get excel sheet rows and column count using selenium webdriver

Selenium webdriver excelsheet row and column counts


package selenium_examples;

import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {


 public void login() throws BiffException, IOException, InterruptedException
 {
  
 
   FileInputStream fi=new FileInputStream("F:\\Sel.xls");
   Workbook WB = Workbook.getWorkbook(fi);
   Sheet sheet = WB.getSheet("Sheet1");
   System.out.println("Number of Rows in Excel Sheet =" +sheet.getRows());
   System.out.println("Number of Columns in Excel Sheet =" +sheet.getColumns());
  
 
   fi.close();
 }

   public static void main(String[] args) throws BiffException, IOException, InterruptedException 
   {
    Sample s1=new Sample();
    s1.login();
   
   }
    
 }


Output:-
Number of Rows in Excel Sheet =7
Number of Columns in Excel Sheet =4
 

Drag and Drop in Selenium Webdriver

Handling "drag and drop" actions using WebDriver (Selenium 2)

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Test {

drag and drop selenium webdriver
 public static void main(String[] args) {


     WebDriver driver = new FirefoxDriver();
     driver.get("http://jqueryui.com/droppable/");
     driver.switchTo().frame(driver.findElement(By.className("demo-frame")));
     WebElement dragElement=driver.findElement(By.id("draggable")); 
     WebElement dropElement=driver.findElement(By.id("droppable"));
    
     Actions builder = new Actions(driver);
     builder.dragAndDrop(dragElement, dropElement).perform();
     System.out.println("Done!");
    
    
  }
}

Connecting to DataBase using Selenium WebDriver


Selenium Webdriver Script to connect postgresql database.

package selenium_examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
 public static void main(String[] args) throws ClassNotFoundException, SQLException {
  Class.forName("org.postgresql.Driver");
  Connection con = DriverManager.getConnection(dbUrl,username,password);
                //dbUrl="jdbc:postgresql://hostname:port/dbname"
  System.out.println("Connected to database");
 
                Statement stmt =con.createStatement();
  ResultSet rs = stmt.executeQuery("select id from emp");
  while (rs.next()) {
  
                int x = rs.getInt("id");
                System.out.println("Employee ids="+ x);
 }
 
  con.close();
  System.out.println("Connection closed");
 }

}

Output:-Displays all employee ids.

 

Datadriven testing using selenium webdriver


Datadriven or Parameterization in Selenium Webdriver


package selenium_examples;


import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {
           
                 WebDriver driver = new FirefoxDriver();

public void search() throws BiffException, IOException, InterruptedException
{


driver.get("http://www.google.com");
FileInputStream fi=new FileInputStream("F:\\Sel.xls");
Workbook WB = Workbook.getWorkbook(fi);


Sheet sheet = WB.getSheet("Sheet1");
System.out.println("Number of Rows in Excel Sheet =" +sheet.getRows());

for (int i=0;i {

String s=sheet.getCell(0,i).getContents();
System.out.println(s);
driver.findElement(By.id("gbqfq")).sendKeys(s);
driver.findElement(By.id("gbqfb")).click();
Thread.sleep(3000);
driver.findElement(By.id("gbqfq")).clear();
}
fi.close();
}

public static void main(String[] args) throws BiffException, IOException, InterruptedException
{
Sample s1=new Sample();
s1.search();

}

}

Output:-
Number of Rows in Excel Sheet =4
Selenium
Selenium Ide
Selenium RC
Selenium Webdriver


The above script gets values from excel sheet and uses in google search.


Important things to note in this script is Excel Sheet format should be in .Xls.If we use .xlsx format excel sheet,we get error : "Exception in thread "main" jxl.read.biff.BiffException: Unable to recognize OLE stream".
So save the excel sheet as "Excel 97-2003 Workbook".


 

Selenium Webdriver Scripts : Working with Calendar using Selenium webdriver

Selenium webdriver script to select current month date and next month date through calendar.

package selenium_examples;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://hotels.spicejet.com/index.html");

driver.findElement(By.xpath("//html/body/div[3]/div/table/tbody/tr/td[1]/div[1]/form[1]/fieldset/div[2]/div[2]/div[1]/a/img")).click();
driver.findElement(By.linkText("8")).click();
driver.findElement(By.xpath("//html/body/div[3]/div/table/tbody/tr/td[1]/div[1]/form[1]/fieldset/div[2]/div[2]/div[2]/a/img")).click();
driver.findElement(By.xpath("//html/body/div[5]/div[2]/a[2]/span")).click();
driver.findElement(By.linkText("12")).click();

}

  }


Above scripts selects ‘Check in date’  as 8th of the current month i.e. 8/11/2013   and ‘Check out date’ as 12th of the next month i.e. 12/12/2013 from calendar.

Working with calendar - selenium webdriver

 



 

Data Driven Testing for Login Functionality using Selenium Webdriver


Below is the code for Mercury Tours Login functionality using Selenium WebDriver.
We are Providing Login Details from Excel Sheet.



package selenium_examples;


import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

WebDriver driver = new FirefoxDriver();

public void login() throws BiffException, IOException, InterruptedException
{


FileInputStream fi=new FileInputStream("F:\\Sel1.xls");
Workbook WB = Workbook.getWorkbook(fi);


Sheet sheet = WB.getSheet("Sheet1");
System.out.println("Number of Rows in Excel Sheet =" +sheet.getRows());

for (int i=1;i {

String s=sheet.getCell(0,i).getContents();
String t=sheet.getCell(1,i).getContents();
System.out.println("Username=" +s);
System.out.println("Password= " +t);
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys(s);
driver.findElement(By.name("password")).sendKeys(t);
driver.findElement(By.name("login")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("SIGN-OFF")).click();
}
fi.close();
}

public static void main(String[] args) throws BiffException, IOException, InterruptedException
{
Sample s1=new Sample();
s1.login();

}

}


Output:-

Number of Rows in Excel Sheet =4
Username=mercury
Password= mercury
Username=mercury1
Password= mercury1
Username=mercury2
Password= mercury2



 

How to get auto populated Search results using selenium webdriver

How to get auto populated Google search results?

package selenium_examples;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Testing");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
WebElement table = driver.findElement(By.className("gssb_m"));    
List rows = table.findElements(By.tagName("span")); 
System.out.println("Total no. of rows: "+ rows.size());
Iterator i = rows.iterator();  
while(i.hasNext()) {
 WebElement row = i.next();
 System.out.println(row.getText());
}

     
}

       }


Output:-

Total no. of rows: 4
testing
testing tools
testingken
testing interview questions

webdriver autocomplete script

 




 

How to upload files using selenium webdriver


package selenium_examples;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.files.com/");
driver.manage().window().maximize();
WebElement uploadElement = driver.findElement(By.name("file_0"));
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
uploadElement.sendKeys("E:\\index.jpg");

 
}
}


 
upload file in selenium webdriver


How to scroll down the page through Selenium Web Driver

Page Scroll down script using selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 300)");

 

}
}


Page Scroll up script using selenium webdriver

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(300, 0)");
 

}
}



 



 

How to right click on a link and open link in new tab using selenium webdriver

Script1:-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 
The above script clicks on 'About' link in google page and opens 'About google' page in new tab.

right click on link in webdriver


contextClick()-Performs a context-click at the current mouse location.

contextClick(WebElement onElement)-Performs a context-click at middle of the given element

Script2:-

Below script clicks on 'About' link in google page and opens 'About google' page in new window.

package selenium_examples;



import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 

How to highlight an element using selenium webdriver

Script to highlight element border

Script1:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("http://www.seleniumhq.org/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("q"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid blue'", element);
       }
}

Above script highlights search textbox border in www.seleniumhq.org website.

highlight element in selenium webdriver



Script2:-

package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("https://www.google.co.in/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("btnK"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid red'", element);
       }
}



 


Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge

↑