Selenium With Java and Python For Mobile Apps & Web Apps......!

Tuesday, 21 April 2015

How to connect Excel Sheet Using Selenium Web driver.

To get data from excel sheet or write data in excel sheet we require some other  api which available  on below link:
Steps :
1. Download apache poi form https://poi.apache.org/download.html link.
2. Unzip and import jar your project.
3.Use given API to connect to excel sheet



package genricLib;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class ExcelLib {
String path="C:\\Users\\ADMIN\\Desktop\\EmployeeDetails.xls";
/**
* Get Data From Excel File.....
* @param sheet
* @param rowno
* @param colno
* @return data
*/
public String getData(String sheet,int rowno,int colno) throws InvalidFormatException, IOException {

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
Row rw=sh.getRow(rowno);
String data=rw.getCell(colno).getStringCellValue();


return data;
}
/**
* Save data in Excel Sheet
* @param sheet Name
* @param rowno
* @param colno
* @param data
*/
public void setData(String sheet,int rowno,int colno,String data) throws InvalidFormatException, IOException {

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
Row rw=sh.getRow(rowno);
Cell cel=rw.getCell(colno);
cel.setCellType(cel.CELL_TYPE_STRING);
cel.setCellValue(data);
FileOutputStream fos=new FileOutputStream("C:\\Documents and Settings\\PavanD\\Desktop\\pardeep.xlsx");
wb.write(fos);




}
/**
* To get Total no row in Excel File
* @param sheet
* @return No of Row
*/

public int getRowCount(String sheet) throws InvalidFormatException, IOException
{

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
int data=sh.getLastRowNum();

return data;
}
/**
* To Get Total No Of Column in Row
* @param sheet
* @param rowNo
*/
public int getColomnCount(String sheet,int rowNo) throws InvalidFormatException, IOException
{

FileInputStream fis=new FileInputStream(path);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheet);
Row rw=sh.getRow(rowNo);
int data=rw.getLastCellNum();

return data;

}

}

Thursday, 16 April 2015

How to take ScreenShot using Selenium WebDriver.

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class ReportLib {


/**
* To Get Screen Shot If Test case Fail and 
* @throws IOException
*/
public void getsnapShot(String name) throws IOException{
String path="E:\\abc\\xyz\\project1\\TestData\\"+name+".png";

EventFiringWebDriver edriver=new  EventFiringWebDriver(Driver.driver);
File srcimg=edriver.getScreenshotAs(OutputType.FILE);
File dstimg=new File(path);
FileUtils.copyFileToDirectory(srcimg, dstimg);

}

}
For Example:

Wednesday, 15 April 2015

How to Handle Proxy Setting using Selenium Webdriver .

Mozilla FireFox:
FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("network.proxy.type", 1);

profile.setPreference("network.proxy.http", "localhost");

profile.setPreference("network.proxy.http_port", 3128);

WebDriver driver = new FirefoxDriver(profile);

or
String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

 Internet Explorer:

String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new InternetExplorerDriver(cap);

Chrome:
String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new ChromeDriver(cap);

Thursday, 9 April 2015

How to Interact with hidden elements using Selenium Webdriver.

Since a user cannot read text in a hidden element, WebDriver will not allow access

to it as well.However, it is possible to use Javascript execution abilities to call getText directly

from the element:

WebElement element =((JavascriptExecutor) driver).executeScript("return arguments[0].getText();", element);

Wednesday, 8 April 2015

How to handle SSL certificate in Mozilla,Chrome and Internet Explorer or How to work with Https websites .

Mozilla:

FirefoxProfile profile = new FirefoxProfile();

profile.setAssumeUntrustedCertificateIssuer(false);

driver = new FirefoxDriver(profile);

driver.get("https://google.com/ <https://mailbox.com/> ");


Chrome:

DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\Selenium 

softwares\\drivers\\chromedriver.exe");

    _driver = new ChromeDriver(capabilities);

    System.setProperty("webdriver.chrome.driver",

            "D:/Softwares/Selenium softwares/drivers/chromedriver.exe");

    //_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

Internet Explorer :

System.setProperty("webdriver.ie.driver",

    "D:/Softwares/Selenium softwares/drivers/IEDriverServer.exe");

    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();    

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_B

Y_IGNORING_SECURITY_DOMAINS, true); 

    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    capabilities.setJavascriptEnabled(true); 

    //capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-

certificate-errors"));

    _driver = new InternetExplorerDriver(capabilities);

    _driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);


    login();

    

Wait Statement in Selenium Webdriver.

Wait Statement during Navigation:



Thread.sleep(5000) Wait For 5 Seconds using Java API

Implicitly Wait Command:Implicit wait statement used wait till entire page get download
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);


Expected Conditions Commands:Explicit wait statement used to load web element not entire page used in dynamic app(ajax app)

WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("add project")));

FluentWait Command:An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page

->:Waiting 30 seconds for an element to be present on the page, checking
->: for its presence once every 5 seconds.
  
  Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }

   });

Set ScriptTimeoutSet the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

driver.manage().timeouts().setScriptTimeout(100,SECONDS);
PageLoadTimeout:Set the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.


driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

Tuesday, 7 April 2015

How to connect Data Base Using Using Java or Selenium Webdriver

Data Base Code

import java.sql.*;

public class DatabaseConnectDemo{

public static void main(String[] args) throws Throwable {

//Resgister the driver through 

Class.forName("oracle.jdbc.driver.OracleDriver");

System.out.println("registered driver successfully");

//Create the connection and assign to connection reference

Connection 

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", 

"username", "password");

System.out.println("connection successsfully");

//create a statement through connection reference and assign to statement reference

Statement stmt=con.createStatement();

System.out.println("statement object created successfully");

//call the executequery method through statement reference and pass the query as 

argument.

ResultSet rs=stmt.executeQuery("select * from emp");

System.out.println("query is executed");

while(rs.next()){

int i=rs.getInt(1);

String str=rs.getString(2);

String str1=rs.getString(3);

int i1=rs.getInt(4);

System.out.println(i+"\t"+str+"\t"+str1+"\t"+i1);

}}}

Translate

Popular Posts

Total Pageviews