MainMenu

Home Java Overview Maven Tutorials

Wednesday, 26 April 2017

Singleton Class in Java with example



For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


For Video : CLICK HERE



What is singleton class ?
Singleton means one, that means you can create only one instance of a class.
If you are allowing a class to create only one instance that means class is singleton.
"Singleton" is not a keyword , it is a concept in java.
The Main purpose of he Singleton is to control object creation of a class.

In Java, a Singleton is a design pattern that restricts the instantiation of a class to one single instance. This is useful when exactly one object is needed to coordinate actions across the system. The Singleton pattern involves a single class that is responsible for creating its own instance and ensuring that only one instance is created throughout the application's lifecycle.


How it can be acheived?
By creating a static object, private constructor of class and a method with return statement for object.
Step 1. We need to create constructor as private so that we can not create an object outside of the class.
Step 2. We need to create object with static keyword.


Example :

public class Singleton {
// Private static variable to hold the single instance of the class
private static Singleton instance;

// Private constructor to prevent instantiation from outside
private Singleton() {
// Constructor code goes here
}

// Public static method to get the single instance of the class
public static Singleton getInstance() {
// Lazy initialization: create the instance if it doesn't exist yet
if (instance == null) {
instance = new Singleton();
}
return instance;
}
// Other methods of the class go here
}



Code :

public class SingletonA {
public static void main(String[] args)
{
MainSingleton obj1 = MainSingleton.getobject();
obj1.prt();
}
}
class MainSingleton
{
// static MainSingleton obj = new MainSingleton(); this object will be created when your class will be loaded.
static MainSingleton obj;
private MainSingleton()
{
System.out.println("www.way2testing.com");
}
public static MainSingleton getobject()
{
if(obj ==null)
{
obj = new MainSingleton();
}
return obj;
}
public void prt()
{
System.out.println("chandan");
}
}

Tags : Singleton class in java, what is singleton class in java, Singleton class with example

Sunday, 9 April 2017

Automate Android Chrome Browser using Appium



For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


For Video : CLICK HERE


First of all connect your device with usb cable
Start appium
write & run the script
Scenarios :
Open the Andriod Chrome Browser
navigate to URL "www.way2testing.com"
Scroll the browser
click the link "appium tutorial with example"

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
public class Appauto
{
@Test
public void g() throws MalformedURLException, InterruptedException
{
//create object of DesiredCapabilities class
DesiredCapabilities capabilities = DesiredCapabilities.android();
//set the capability to execute test in chrome browser
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, BrowserType.CHROME);
//set the capability of Andriod platform
capabilities.setCapability(MobileCapabilityType.PLATFORM, Platform.ANDROID);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Andriod");
//set the device name
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "7c94b480");
//set the andriod version
capabilities.setCapability(MobileCapabilityType.VERSION, "5.1");
//create object of URL class and specify the appium server address
URL url = new URL("http://127.0.0.1:4723/wd/hub");
WebDriver driver = new AndroidDriver(url, capabilities);
driver.get("http://www.way2testing.com");
Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor) driver;
//Scroll page down to 250px
js.executeScript("window.scrollBy(0,750)", "");
driver.findElement(By.linkText("Appium Tutorials with example")).click();
Thread.sleep(5000);
}
}