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.

 

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge