Thursday, March 3, 2016

Java : Custom Dialog box (JDialog)

Custom Dialog box (JDialog)



JTextField username = new JTextField();
JTextField password = new JPasswordField();
Object[] message = {
    "Username:", username,
    "Password:", password
};

int option = JOptionPane.showConfirmDialog(null, message, "Login", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
    if (username.getText().equals("h") && password.getText().equals("h")) {
        System.out.println("Login successful");
    } else {
        System.out.println("login failed");
    }
} else {
    System.out.println("Login canceled");
}

Wednesday, March 2, 2016

Selenium :Run Testng.xml using a Java file

Run Testng.xml using a Java file

http://stackoverflow.com/questions/23272861/how-to-call-testng-xml-from-java-main-method




// Create object of TestNG Class
TestNG runner=new TestNG();

// Create a list of String 
List<String> suitefiles=new ArrayList<String>();

// Add xml file which you have to execute
suitefiles.add("C:\\Automation Test\\Git\\vne_automation\\testng.xml");

// now set xml file for execution
runner.setTestSuites(suitefiles);

// finally execute the runner using run method
runner.run();

Saturday, February 27, 2016

Selenium : All About Reports


All ABout Reports :



http://examples.javacodegeeks.com/enterprise-java/testng/testng-html-xml-reports-example/
https://seleniumexperience.wordpress.com/2013/07/25/generating-custom-reports-with-testng/
http://kaczanowscy.pl/tomek/2009-11/testng-listeners-apache-velocity-and-css
https://solidsoft.wordpress.com/2011/01/23/better-looking-html-test-reports-for-testng-with-reportng-maven-guide/

Gradle :File Operations

Gradle :File Opertions 



https://www.safaribooksonline.com/library/view/gradle-beyond-the/9781449373801/ch01.html

Gradle: A trivial copy task configuration

task copyPoems(type: Copy) {
  from 'text-files'
  into 'build/poems'
}


Gradle: A copy task that copies all the poems except the one by Henley

task copyPoems(type: Copy) {
  from 'text-files'
  into 'build/poems'
  exclude '**/*henley*'
}


Gradle: A copy task that only copies Shakespeare and Shelley

task copyPoems(type: Copy) {
  from 'text-files'
  into 'build/poems'
  include '**/sh*.txt'
}


Gradle: A copy task taking files from more than one source directory

task complexCopy(type: Copy) {
  from('src/main/templates') {
    include '**/*.gtpl'
  }
  from('i18n')
  from('config') {
    exclude 'Development*.groovy'
  }
  into 'build/resources'
}


Gradle: A copy task mapping source directory structure onto a new destination structure

task complexCopy(type: Copy) {
  from('src/main/templates') {
    include '**/*.gtpl'
    into 'templates'
  }
  from('i18n')
  from('config') {
    exclude 'Development*.groovy'
    into 'config'
  }
  into 'build/resources'
}


Gradle: Renaming files using regular expressions

task rename(type: Copy) {
  from 'source'
  into 'dest'
  rename(/file-template-(\d+)/, 'production-file-$1.txt')
}


Gradle: Renaming files programmatically

task rename(type: Copy) {
  from 'source'
  into 'dest'
  rename { fileName ->
    "production-file${(fileName - 'file-template')}"
  }
}

 
Gradle: Copying a file with keyword expansion

versionId = '1.6'

task copyProductionConfig(type: Copy) {
  from 'source'
  include 'config.properties'
  into 'build/war/WEB-INF/config'
  expand([
    databaseHostname: 'db.company.com',
    version: versionId,
    buildNumber: (int)(Math.random() * 1000),
    date: new Date()
  ])
}

Java :Classpath and library error even when dependencies are added to Gradle

Classpath and library error even when dependencies are added to Gradle


I have added all dependencies in "build.gradle" required for a selenium project.But still i am getting error when i try to launch a firefox driver using : WebDriver driver = new FirefoxDriver();


Solution : 
 
1. Make sure that you have imported the FirefoxDriver class in your code. Add the following line in your import section.

    import org.openqa.selenium.firefox.FirefoxDriver;

2. Clean the Gradle project (https://www.jetbrains.com/idea/help/gradle-tool-window.html)
3. Refresh the project (File>Synchronize)
4. Use the refresh button in Gradle Tool Window if ".gradle" file is edited
https://www.jetbrains.com/idea/help/gradle-tool-window.html


Note :All Dependencies add should appear in Gradle Tool Window >Dependencies

Java : Ivy (Gradle)

Java : Ivy



Apache Ivy was developed as an extension to the Ant build tool to provide Maven-style binary artifact management. It was never deployed as widely as Maven-style repos

Gradle :Declaring an Ivy repository using the default Maven layout

repositories {
  ivy {
    url 'http://build.megacorp.com/ivy/repo'
  }
}

Java :Maven Repositories(Gradle)

Java :Maven Repositories


https://www.safaribooksonline.com/library/view/gradle-beyond-the/9781449373801/ch04.html

Since a Maven repository is nothing more than a website containing downloadable POM files and modules in a predictable directory structure, declaring a Maven repository in Gradle begins with telling Gradle the URL of the repo. '

The most common Maven repository http://repo1.maven.org/maven2/ ( to avoid scripting that URL directly into your build).

Gradle: Declaring the Central repository
repositories {
  mavenCentral()
}


Other open-source Maven repositories exist on the Internet, Sonatype’s Nexus and JFrog’s Artifactory

Gradle : Declaring Maven repositories in general
repositories {
  maven {
    url = 'http://download.java.net/maven/2'
  }
  maven {
    name = 'JBoss Repo'  //optional name
    url = 'https://repository.jboss.org/nexus/content/repositories/releases'
  }
}


To download build artifacts from a mirrored copy of the repo somewhere else in the network

Gradle: Setting the artifact URL as distinct from the default POM URL

repositories {
  // Overriding artifacts for an internal repo
  maven {
    url = 'http://central.megacorp.com/main/repo'
    artifactUrls = [ 'http://dept.megacorp.com/local/repo' ]
  }
  // Obtain Maven Central artifacts locally
  mavenCentral artifactUrls: ['http://dept.megacorp.com/maven/central']
}

-----------------

Friday, February 26, 2016

Java :IS-A and HAS-A

IS-A and HAS-A

http://stackoverflow.com/questions/9677545/what-do-has-a-and-is-a-mean?lq=1
http://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language


Has-a means that the Class in question 'has a' field of a type.

Is-a means that the Class extends from a superclass or implements an interface. The instanceof operator would return true if tested against a class in the this case.

Example

class SteeringWheel{}

class Vehicle{}
 
interface transport 

class Car extends Vehicle implements transport
{
    SteeringWheel  sWheel;
   
}

  • A car is-a vehicle and it is for transport
  • A car has-a steering wheel

Java : Why multiple inheritance not allowed in Java (Diamond Problem) and Solution

Why multiple inheritance not allowed in Java (Diamond Problem) and Solution 


Problem :

class A { 
int i; 
void msg() {
        System.out.println("From A");
    } 
}

class B { 
  int i;   
void msg() {
        System.out.println("From B");
    } 
}

class C extends A,B {         // suppose if this was possible
    public static void main(String[] args) { 

        super.msg();                 // which msg() method would be invoked?   

 }
}

Solution 1: Using interface’s reference
interface Moveable{
    default void run(){
        System.out.println("I am running, kid !!");
    }
}
  
interface Crawlable{
    default void run(){
        System.out.println("I am running, daddy !!");
    }
}
  
public class Animal implements Moveable, Crawlable {
    public static void main(String[] args)
    {
        Moveable.super.run(); 
         Crawlable.super.run();
    }
}
So solve above conflict, caller class must decide which run() method it want to invoke and then call using

 
Solution 2 : Using Tree pattern

public class Canine extends Animal{}
public class Dog extends Canine{}

Solution 3 : Creating Object Instances 

class mobile_phone { void call(){}}
class tablet {void big_screen(){} }

class phablet {
                mobile_phone m;
                tablet  t;

phablet(){
                m= new mobile_phone ();
                t=  new tablet  ();
}
test(){
m.call();
t.big_screen();
}

Wednesday, February 24, 2016

Java :Method Chaining (Builder Pattern)

Method Chaining (Builder Pattern)


While writing codes , ever wondered how Java codes (Selenium) or vbs (QTP) allows user todo something like : Object.method().method() .... and so on .

Eg :
Browser("Jobs").Page("QTP").WebElement(" ") //In QTP
Webdriver.FindElement("xxxx").sendkeys("123) ; //In Selenium

The above style uses a pattern called Builder pattern  internally.

Example :

public class test1 {
    public static void main(String[] args){
        animal a =new animal();
        a.set_name("dog").bites(false);
    }
}



public class animal {
    String animal_name;
    boolean Bites;

    public animal set_name(String animal_name){
        this.animal_name=animal_name;
        return this;
    }

    public animal bites(boolean Bites){
        this.Bites=Bites;
        return this;
    }
}


Thing to notice :
1. Each method uses "this" keyword while returning
2. Each method returns an object of the class type.

End Result :
 a.set_name("dog").bites(false);

What it means ?

Tuesday, February 23, 2016

Java:Gradle Tut

Gradle



http://www.vogella.com/tutorials/Gradle/article.html
https://docs.gradle.org/current/userguide/tutorial_using_tasks.html
https://develosapiens.wordpress.com/2015/05/08/playing-with-gradle/
http://blog.bbv.ch/2012/09/12/the-gradle-build-system-a-short-overview/
http://www.drdobbs.com/jvm/writing-build-scripts-with-gradle/240168648?pgno=1

Java : IntelliJ gitIgnore

IntelliJ gitIgnore


download : https://plugins.jetbrains.com/plugin/7495?pr=idea

This is used to ignore those files that user does not want to merge ,copy or store it in Git.
  1. Download jar file to your local
  2. Open IntelliJ >File>Settings >Plugins
  3. Click button "Install plugin from disk"
  4. Select the file in the file browser 
  5. Click apply
  6. Restart IntelliJ

After restart , goto you main project right click >New > ignorefile>gitIgnore file

Java : Gradle Advantages

Gradle Advantages


Advantages of Maven:
- configure and intercept all build phases of your project with the use of plugins
- dependeny management
- distribution management
- scm integration
- well integrated with Continous Integration environments such as Jenkins.

Disadvantages of Maven:
 - XML syntax
 - extending maven with your own plugin can be too much expensive.
 - it is focused on Java projects
Advantages of Gradle: 

 - Possibility to write your own tasks in Groovy (tasks are based on task Ant model).
 - POM generation.
 - reuse of all Maven repositories
 - integration with Ivy repositories
 - polyglot build system to integrate projects with different tenchnologies and programmin languages.

- Gradle has goodness of Ant and Maven put together
- minus the noise of XML.

Java : Wrapper

Wrapper


Use : 
  1. To convert simple data types into objects
  2.  To convert strings into data types
Example :
int k = 100;
Integer obj = new Integer(k); 

 int m = obj.intValue(); //UnWrapp

The int data type k is converted into an object, obj using Integer class. The obj object can be used in Java programming wherever k is required an object.

Primitive datatype and     Wrapper and class

  • byte     and     Byte
  • short     and     Short
  • int     and     Integer
  • long     and     Long
  • float     and     Float
  • double     and     Double
  • char     and     Character
  • boolean and     Boolean

Code :

public class WrappingUnwrapping
{
  public static void main(String args[])
  {                                  //  data types
    byte grade = 2;
    int marks = 50;
    float price = 8.6f;                         // observe a suffix of <strong>f</strong> for float
    double rate = 50.5;
                                           // data types to objects      
    Byte g1 = new Byte(grade);                    // wrapping 
    Integer m1 = new Integer(marks);
    Float f1 = new Float(price);
    Double r1 = new Double(rate);
                                                                    // let us print the values from objects  
    System.out.println("Values of Wrapper objects (printing as objects)");
    System.out.println("Byte object g1:  " + g1);
    System.out.println("Integer object m1:  " + m1);
    System.out.println("Float object f1:  " + f1);
    System.out.println("Double object r1:  " + r1);
            // objects to data types (retrieving data types from objects)
    byte bv = g1.byteValue();                 // unwrapping
    int iv = m1.intValue();
    float fv = f1.floatValue();
    double dv = r1.doubleValue();
                                                                    // let us print the values from data types  
    System.out.println("Unwrapped values (printing as data types)");
    System.out.println("byte value, bv: " + bv);
    System.out.println("int value, iv: " + iv);
    System.out.println("float value, fv: " + fv);
    System.out.println("double value, dv: " + dv);
  }
}