browser resizing using selenium


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

public class TestWindowResize {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.co.in");
System.out.println(driver.manage().window().getSize());
Dimension dimension = new Dimension(1024, 600);
driver.manage().window().setSize(dimension);
System.out.println(driver.manage().window().getSize());
}
}

delete cookies using selenium webdriver


import java.util.Set;

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

public class DeleteCookies {

public static WebDriver driver = null;

public static void main(String[] args) {
driver = new FirefoxDriver();
driver.get("http://google.com");

Set<Cookie> co = driver.manage().getCookies();
System.out.println(co.size());
// delete all the cookies of a webpage
driver.manage().deleteAllCookies();

// should be 0 since all the cookies have been deleted
Set<Cookie> coA = driver.manage().getCookies();
System.out.println(coA.size());

driver.get("http://google.com");
Set<Cookie> coB = driver.manage().getCookies();

// get the cookie name
for (Cookie cookie : coB) {
System.out.println(cookie);
}

// delete a single cookie using name
driver.manage().deleteCookieNamed("NID");

Set<Cookie> coC = driver.manage().getCookies();
for (Cookie cookie : coC) {
System.out.println("NIC is removed: " + cookie);
// Delete cookie
driver.manage().deleteCookie(cookie);
}
}
}

Kill a process using webdriver org.openqa.selenium.os.WindowsUtils


import org.openqa.selenium.os.WindowsUtils;

public class KillProcess {
public static void main(String[] args) {
WindowsUtils.tryToKillByName("notepad.exe");
}
}

Tools for web element locators

handling https certificate on firefox using selenium webdriver – this connection is untrusted issue.


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

public class HandleFirefoxSecurity {
public static WebDriver driver = null;

public static void main(String[] args) {
FirefoxProfile fp = new FirefoxProfile();
fp.setAcceptUntrustedCertificates(true);
driver = new FirefoxDriver(fp);
driver.get("untrusted website url");
}

}

sparse checkout, sub directory clone github

– git init init new repo
– cd to initialized repo
– git remote add -f origin repourl

– git config core.sparsecheckout true

– give the folder that you want to checkout

echo “dir/dir” >> .git/info/sparse-checkout
echo “dir/dir/dir” >> .git/info/sparse-checkout

finally do pull
git pull origin master

Read PDF file using Selenium Webdriver

Download pdfbox and fontbox jars from https://pdfbox.apache.org/download.cgi
To read pdf file and verify the content is present in the pdf using.

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.util.PDFTextStripper;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

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

	public void verifyPdfContent(String[] values) throws IOException {

		URL url = new URL(driver.getCurrentUrl());
		BufferedInputStream inputPdfFile = new BufferedInputStream(
				url.openStream());
		PDFParser pdfParser = new PDFParser(inputPdfFile);
		pdfParser.parse();
		String pdfContent = new PDFTextStripper().getText(pdfParser
				.getPDDocument());
		// pdfParser.getPDDocument().close();
		System.out.println("printing PDF content fully :" + pdfContent);
		for (int i = 0; i < values.length; i++) {
			if (pdfContent.contains(values[i])) {
				System.out.println((values[i] + " is present in the PDF file"));

			} else {
				System.out
						.println((values[i] + " is not present in the PDF file"));

			}
		}
	}

	@Test
	public void myTest() throws IOException {
		driver.get("http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf");
		// Values to verify
		String[] values = new String[2];
		values[0] = "xxxcsds";
		values[1] = "hey";
		// verifying the pdf with given values
		verifyPdfContent(values);

	}

}