Read Data from CSV using CSVReader

package com.Testing;

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import au.com.bytecode.opencsv.CSVReader;
import jxl.read.biff.BiffException;

public class ReadDataUsingCSVReader {
	
	public static WebDriver driver;
	
	
  @SuppressWarnings("resource")
@Test
  public void ReadDatafromXLSL() throws InterruptedException, BiffException, IOException {	  
			  
	  System.out.println("----------------------------ReadDatafromXLSL ----------------------------"+"\n" +"\n");	 	  
	  
	  
	// This will load csv file 
	  CSVReader reader = new CSVReader(new FileReader("D:\\QA\\demo.csv"));
	  
	// this will load content into list
	  List<String[]> li=reader.readAll();
	  System.out.println("Total rows which we have is "+li.size());
	            
	 // create Iterator reference
	  Iterator<String[]>i1= li.iterator();
	    
	 // Iterate all values 
	 while(i1.hasNext()){
	     
	 String[] str=i1.next();
	   
	 System.out.print(" Values are ");
	 
	 for(int i=0;i<str.length;i++)
	{
	 
	   System.out.print(" "+str[i]);
	 
	}
	   System.out.println("   ");
	     
	    
	}
	  
	  
	        
      	  	
      
  }
  
 
}

 

Read Data from CSV using FileReader

package com.Testing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import jxl.read.biff.BiffException;

public class ReadDataUsingFileReader {
	
	public static WebDriver driver;
	
	
  @Test
  public void ReadDataUsingFileReader() throws InterruptedException, BiffException, IOException {	  
			  
	  System.out.println("----------------------------ReadDataUsingFileReader ----------------------------"+"\n" +"\n");
	  
	  FileReader fr = new FileReader(new File("D:\\QA\\demo.csv"));
	  
	  BufferedReader br = new BufferedReader(fr);
	  
	  String st;
	  
	  while ((st = br.readLine()) != null)
	  {
		  System.out.println(st);
		  
		  driver = new FirefoxDriver();		
			
		  driver.manage().window().maximize();
			
		  driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		  
		  driver.get("http://test.com");
		  
		  Thread.sleep(5000);
		  
		  driver.findElement(By.name("uid")).sendKeys(st);
		  	   
		  driver.findElement(By.xpath("//a[contains(text(),'Log in')]")).click();
		  
		  Thread.sleep(5000);	 
		  
		  Assert.assertEquals("Executive Summary", "Executive Summary", "Executive Summary");
		  
		  System.out.println("Assert Command on home dashboard Working Fine" +"\n" +"\n");
		  
		  driver.quit();
		  
	  }
	  
	        
      	  	
      
  }
  
 
}

 

Connect mySQL database using Selenium Webdriver

package testng;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.testng.annotations.Test;

public class ConnectmySQL {
	
	@Test
	public void testDB() throws ClassNotFoundException, SQLException
	{
		Class.forName("com.mysql.jdbc.Driver");
						
		Connection cnt = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentInfo","root","Akshaya@19");
					
		Statement smt = cnt.createStatement();
		
		ResultSet rs= smt.executeQuery("select * from studentName");
		
		while(rs.next())
		{
			String firstName = rs.getString("firstName");
			
			System.out.println("Database record is: --- "+firstName);
			
			
		}
		
		
	}

}

 

mysql> select * from studentName;
+———–+———-+
| firstName | lastName |
+———–+———-+
| Ram | Rao |
| Krishna | Kamath |
+———–+———-+

Return values in Seleneium Webdriver

package webdriver;

public class ReturnValue {

	public static void main(String[] args) 
	{
		ReturnValue obj = new ReturnValue();
		
		int sum=obj.add(250, 78);
		
		System.out.println("Addition Result: --" +sum);
		
		double result = obj.sub(725.5, 122.3);
		
		System.out.println("Substract Result: --" +result);		
			
	}
	public int add(int a, int b)
	{
		int c = a + b;
		
		return c;
		
	}
	public double sub(double x, double y)
	{
		double result = y-x;
		
		return result;
	}

}

 

Passing Parameters in Selenium WebDriver

package webdriver;

public class ParametersTest {

	public static void main(String[] args) 
	{
		ParametersTest obj = new ParametersTest();
		
		obj.add(250, 78);
		
		obj.sub(725.5, 122.3);		
			
	}
	public void add(int a, int b)
	{
		int c = a + b;
		
		System.out.println("Addition Result: --" +c);		
		
	}
	public void sub(double x, double y)
	{
		double result = y-x;
		
		System.out.println("Subtration Result: --" +result);
	}

}

 

Handling Page Scroll Down and Scroll Up

package testng;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class pageScroll {
	
	WebDriver driver;
	
	@Test
	public void scrollDrown() throws Throwable
	{
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://www.facebook.com");
		
		Thread.sleep(5000);
		
		((JavascriptExecutor)driver).executeScript("scroll(0,600)");
		
		driver.quit();
	}
	@Test
	public void scrollUp() throws Throwable
	{
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://www.facebook.com");
		
		Thread.sleep(5000);
		
		((JavascriptExecutor)driver).executeScript("scroll(0,600)");
		
		Thread.sleep(5000);
		
		Thread.sleep(5000);
		
		((JavascriptExecutor)driver).executeScript("scroll(0,-600)");
		
		Thread.sleep(5000);
		
		Thread.sleep(5000);
		
		driver.quit();
	}

}

 

1 2 3 4 5