Thursday 4 February 2016

What is package in java

Packages in java:
==============

A package is a collection of classes and interfaces.

Packages are used in Java in order to prevent naming conflicts.

A Package can be defined as a grouping of related types as classes, interfaces, enumerations and annotations.

Some of the existing packages in Java are:
----------------------------------------------

java.lang - bundles the fundamental classes

java.io - classes for input , output functions are bundled in this package

How to create a package:
---------------------------
By using package keyword we can create a package.

package packagename;

package is a fixed keyword and package name can be anything but it should be very meaningful.

Any number of import satatements allowed but only one package statement is allowed

see the below example:
----------------------


package blog;
package blog1;==>Error only one package is allowed

import ;;;;
import;;;;;;
import;;;;;;
import;;;;;;

interface hello
{
void test1();
void test2();
void test3();
void test4();
}

Naming convention for packages:
--------------------------------
There is one universal naming acceptance convention is available

domainnameinreverse.modulename.submodulename;

ex:
package com.flipkart.purchase;

//example on packages:
--------------------------
package blog;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
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.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FindBrokenImages {

private WebDriver webDriver;
private int invalidImageCount;

@BeforeClass
public void setUp() {
System.setProperty("webdriver.chrome.driver",
"D:\\Ramesh\\ChromeDriver.exe");
webDriver = new InternetExplorerDriver();
// webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}

@Test
public void validateInvalidImages() throws ClientProtocolException,
IOException, InterruptedException {

webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati"))
.click();

invalidImageCount = 0;
List<WebElement> imagesList = webDriver.findElements(By.tagName("img"));
System.out.println("Total number of images are " + imagesList.size());
for (WebElement image : imagesList) {
if (image != null) {
validateImage(image);
}
}
System.out.println("Total number of of invalid images are "
+ invalidImageCount);

}

public void validateImage(WebElement imgElement)
throws ClientProtocolException, IOException {

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(imgElement.getAttribute("src"));
HttpResponse response = client.execute(request);
// verifying response code he HttpStatus should be 200 if not,
// increment as invalid images count
if (response.getStatusLine().getStatusCode() != 200)
invalidImageCount++;
}

@AfterClass
public void tearDown() {

webDriver.quit();
}

}

1 comment: