Java – selenium – Read Web Page Content

Reading HTTP Client is causing problems. Firewalls are detecting and closing connections.
The best route is mimic like browser.
Tested with Mac 10.12.4

package com.bible;

import java.io.BufferedWriter;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

//Chrome Driver: https://chromedriver.storage.googleapis.com/index.html?path=2.29/

/**
 * 
 */
public class ReadPassage {

	private static BufferedWriter writer;

	public static void main(String[] args) {
		WebDriver driver;

		File file = new File("resources/chromedriver");
		String absolutePath = file.getAbsolutePath();
		System.out.println("Chrome Driver Path==>" + absolutePath);
		System.setProperty("webdriver.chrome.driver", absolutePath);

		String BROWSER_URL = "http://usccb.org/bible/readings/061017.cfm";

		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get(BROWSER_URL);

		WebElement myDynamicElement = (new WebDriverWait(driver, 30))
				.until(ExpectedConditions.presenceOfElementLocated(By.id("readingssignup")));

		String source = driver.getPageSource();
		System.out.println(source);
		driver.close();

		try {
			Thread.sleep(10 * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bible</groupId>
	<artifactId>my-bible</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>my-bible</name>
	<url>http://maven.apache.org</url>
	<dependencies>

		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.3</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.3.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-chrome-driver</artifactId>
			<version>3.3.1</version>
		</dependency>



		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

Advertisements

The Concurrent Mark Sweep (CMS) Collector Vs The Garbage First or G1 garbage collector

G1 Garbage Collector got introduced in Java 7. People already written on this subject. Just giving links. Please go through and take care of GC Configurations.

 

Java Garbage Collection Basics

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

http://docs.oracle.com/javase/7/docs/technotes/guides/vm/G1.html

Bench marking

http://java-is-the-new-c.blogspot.com/2013/07/tuning-and-benchmarking-java-7s-garbage.html

 

To check quickly, check following in JVM Params

-XX:+UseG1GC

-XX:+UseConcMarkSweepGC

 

Multithreaded Custom Class Loaders in Java SE 7

http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html

-o-

Configure Tomcat’s Resource Factory

Tomcat 7, Oracle 11g

<Resource name="jdbc/dsName" auth="Container">
maxActive="100"
maxIdle="10"
removeAbandoned="true"
removeAbandonedTimeout="1200"
testWhileIdle="true"
testOnBorrow="true"
validationQuery="select 1 from dual"
poolPreparedStatements="false"/>

Note: Read this article and pick up required attributes based on project.
http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

Java – Check given date is in between two given dates or not

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Compare given date is between two gives dates or not
 * 
 * @author Bhavani Polimetla
 * @since 02/10/2014
 * 
 */
public class DateCompareTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			System.out.println(isDateBetween(SDF.parse("01/10/2014"),
					SDF.parse("01/01/2014"), SDF.parse("01/30/2014")));
			System.out.println(isDateBetween(SDF.parse("02/01/2014"),
					SDF.parse("01/01/2014"), SDF.parse("01/30/2014")));
			System.out.println(isDateBetween(SDF.parse("02/03/2014"),
					SDF.parse("01/27/2014"), SDF.parse("02/14/2014")));

		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

	static final SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy");

	public static boolean isDateBetween(Date given, Date startDate, Date endDate) {

		boolean status = false;
		if ((given.after(startDate) && (given.before(endDate)))
				|| (SDF.format(given).equals(SDF.format(startDate)) || SDF
						.format(given).equals(SDF.format(endDate)))) {
			status = true;
		}
		System.out.println(status + " given" + SDF.format(given) + " start"
				+ SDF.format(startDate) + " end" + SDF.format(endDate));
		return status;
	}

}