Monday, 29 June 2015

Java Custom Exception (Core JAVA)



package testing.automationTesting;

public class customException extends Exception{
 customException(){
 }
 customException(String s){
  super(s);
 }
 
static void validate(int age) throws customException{
  
  if(age<18)
   throw new customException("Invalid.Age.Format.Exception - Age should be 18");
  else if(age>18)
   throw new customException("Invalid.Age.Format.Exception - Age should be 18");
  else
   System.out.print("");
 }
 
public static void main(String[] args) {
  try{
   validate(19);
  }
  catch(Exception m){
   System.out.println("Exception is: "+m);
  }
 }
}

Wednesday, 24 June 2015

Cucumber Selenium JAVA Integration (Cucumber Project Using Selenium and JAVA)

For cucumber project there are 3 important files.

1. Feature File: It has .feature extension. We can have multiple feature files according to our requirements. We can place the feature file in any folder and specify the name of the folder in test runner file e.g. features = "Feature".




2. Java Step Definition File: This file describes the feature file. This is the main java file where do write our scripts. We can implement our functions in this file.

3. Java Test Runner File: This file acts as a controller to run different feature files and java files. In this file we specify the various parameters like features = "Feature"
,glue={"testfolder"}
,monochrome = false
,plugin={"pretty", "html:out"}


If you are using maven then include following dependencies in pom.xml or you can simply add jars to your project.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ZS</groupId>
  <artifactId>automationProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>automationProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    
    <dependency>
 <groupId>com.theoryinpractise</groupId>
 <artifactId>cucumber-testng-factory</artifactId>
 <version>1.0.1</version>
</dependency>
    
    <dependency>
   <groupId>com.sun</groupId>
   <artifactId>tools</artifactId>
   <version>1.6</version>
   <scope>system</scope>
   <systemPath>C:\Program Files\Java\jdk1.8.0_40\lib\tools.jar</systemPath>
 </dependency>
    
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-core</artifactId>
 <version>1.2.0</version>
</dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-jvm-deps</artifactId>
 <version>1.0.3</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>1.10.8</version>
</dependency>
    <dependency>
 <groupId>cobertura</groupId>
 <artifactId>cobertura</artifactId>
 <version>1.8</version>
</dependency>
    
    
    <dependency>
 <groupId>net.masterthought</groupId>
 <artifactId>cucumber-reporting</artifactId>
 <version>0.0.24</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>2.0.1-beta</version>
</dependency>
    <dependency>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <version>2.6</version>
</dependency>
    
    
    
    
    <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
    
    
    
  </dependencies>
</project>



<dependency>
 <groupId>com.theoryinpractise</groupId>
 <artifactId>cucumber-testng-factory</artifactId>
 <version>1.0.1</version>
</dependency>
    
    <dependency>
   <groupId>com.sun</groupId>
   <artifactId>tools</artifactId>
   <version>1.6</version>
   <scope>system</scope>
   <systemPath>C:\Program Files\Java\jdk1.8.0_40\lib\tools.jar</systemPath>
 </dependency>
    
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-core</artifactId>
 <version>1.2.0</version>
</dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-jvm-deps</artifactId>
 <version>1.0.3</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>1.10.8</version>
</dependency>
    <dependency>
 <groupId>cobertura</groupId>
 <artifactId>cobertura</artifactId>
 <version>1.8</version>
</dependency>
    
    
    <dependency>
 <groupId>net.masterthought</groupId>
 <artifactId>cucumber-reporting</artifactId>
 <version>0.0.24</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>2.0.1-beta</version>
</dependency>
    <dependency>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <version>2.6</version>
</dependency>
       
    <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>





You can run your test by right clicking on Java Test Runner file and runnig that file as JUNIT test.

Here is the structure for your project:



Test Result report can be seen under out folder in index.html


Please find below all 3 files:

1. Feature File:



Feature: Google Testing

Scenario: Click on Gmail
 Given User is on Google Home Page
 When Click on Gmail link
 Then Gmail opens successfully





2. google_test.java

package stepDefinition;

import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class google_test {
 public static WebDriver driver;
 @Given("^User is on Google Home Page$")
 public void User_is_on_Google_Home_Page() throws Throwable {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=g1OKVcrzFOfI8Aff75zYBw&gws_rd=ssl");
        driver.manage().window().maximize();
  }
 @When("^Click on Gmail link$")
 public void Click_on_Gmail_link() throws Throwable {
  driver.findElement(By.className("gb_ma")).click();
  }
 @Then("^Gmail opens successfully$")
 public void Gmail_opens_successfully() throws Throwable {
  Assert.assertEquals(driver.getTitle(),"Gmail");
        System.out.println("Gmail opens successfully");
 }

}




3. testRunner.java



package cucumberTest;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)
@CucumberOptions(
  features = "Feature"
  ,glue={"stepDefinition"}
  ,monochrome = false
  ,plugin={"pretty", "html:out"} 
  
  )
 
public class TestRunner {
 
}


Monday, 22 June 2015

IP Address Program (Core JAVA)



import java.net.InetAddress;

class IPAddress
{
   public static void main(String args[]) throws Exception
   {
      System.out.println(InetAddress.getLocalHost());
   }
}

Wednesday, 17 June 2015

Current Time Program (Core JAVA)

This code can also be used in selenium to get Test Case Start Time and Test Case End Time. We can develop function as developed in below mentioned code i.e. tNow(). This function can be used before the start of each test and at the end of each test (e.g. start of for loop and end of for loop).
Find below the code for Current Time:



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class timeFunction {

 public static void main(String[] args) {
  System.out.println(tNow());
  
 }
    
   //Generate current start time and end time 
   public static String tNow(){
   //DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
   DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
   Calendar cal = Calendar.getInstance();
   String currTime=dateFormat.format(cal.getTime());
   return currTime;
   }
 
 }

Monday, 15 June 2015

Jenkins Tutorial (GitHub MAVEN JENKIN integration)

For MAVEN PROJECT: "mavenJenkinPrac"

  • Click on New Item:



  • Specify "Item name" i.e. "gitMavenJenkinPrac",select "Maven Project" and click on "OK"



  • Under "Source code management", select Git.
  • Specify Repository URL "https://github.com/XXXXXXXX/xxxxxxTesting.git"



  • Under Build, Specify Root POM i.e. "com.project/pom.xml"
  • Also Specify "Goals and options" i.e. "clean compile test"
  • Click on Apply and Save



  • Click on "Build Now"















Jenkins Tutorial (Basic Configuration Settings)




  • Then click on Configure System



  • Under JDK Installation specify:



  • Under Git, specify:



  • Under Ant, specify:



  • Under Maven, Speficy




Click on Apply and Save

Jenkins Tutorial (MAVEN JENKINS Integration)

For MAVEN PROJECT: "mavenJenkinPrac"

  • Click on New Item:


  • Specify "Item name" i.e. "mavenJenkinPrac",select "Maven Project" and click on "OK"




  • Under build, "Root POM " specify the path till pom.xml
  • specify goals and options, i.e. Clean compile test package
  • Click on Apply and Save




  • Click on "Build Now"












Jenkins Tutorial (ANT JENKIN Integration)

For ANT PROJECT: "antJenkinPrac"

  • Click on New Item:


  • Specify "Item name" i.e. "antJenkinPrac",select "Freestyle project" and click on "OK"






  • Under build select "Invoke ANT".
  • Select ANT version which you specified under jenkin configuration settings:
  • Specify Targest i.e. Clean compile run










  • Under "Build File" give full path till "build.xml"
  • Click on Apply and Save




  • Click on "Build Now"







GitHub Tutorial (Creating and Updating repository)


  • Goto gihub.com, create account
  • Goto git-scm.com
  • Click on Download for windows
  • install
  • Add environment variable


  • Clik on "+" sign
  • Give name to repository: gurdialProjects
  • Specify the local path:C:\Users\Gurdial\Documents\GitHub\gurdialProjects or we can modify according to your requirement
  • Click on "Create Repository"


  • Now goto local path where you have created repository
  • There will be 2 text documents available









  • Add file "Gurdial-Documentation" to local location




  • Right Click on file and then click on Git Gui






  • this window will get open




  • Click on Stage Changed





  • Click on "YES"
  • Add Commit Message
  • Click on Commit then click on Push
  • this window will appear




  • Now goto github
  • File has been added




  • Click on Publish Repository , Add Description





  • Click on "Publish"






  • Now www.github.com and and under repositories you will see "gurdialProjects"







Selenium WebDriver Gmail Automation (Includes Compose, verify whether email has been sent or not and get number of the unread email in inbox folder)

Selenium WebDriver Gmail Automation (Includes Compose, verify whether email has been sent or not and get number of the unread email in inbox folder)



package zellaxSolutions.automationTesting;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class newclass {

 public static void main(String[] args) throws InterruptedException {
  String subject="Write Subject Of Testing Email !!";
  WebDriver driver;
  driver=new FirefoxDriver();
  driver.navigate().to("https://www.google.co.in/?gfe_rd=cr&ei=Gmd-VdWTD-XH8AeqsbH4Bg&gws_rd=ssl");
  driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
  driver.findElement(By.className("gb_la")).click();
  // Login to Gmail
  driver.findElement(By.id("Email")).sendKeys("Enter Email Address (FROM)");
  driver.findElement(By.id("next")).click();
  driver.findElement(By.id("Passwd")).sendKeys("Enter Password");
  //Click on SignIn
  driver.findElement(By.id("signIn")).click();
  Thread.sleep(3000);
  //Compose Email
  driver.findElement(By.xpath("//div[@class='z0']/div")).click();
  Thread.sleep(3000);
  driver.findElement(By.className("vO")).sendKeys("Enter Email Address (TO)");
  Thread.sleep(3000);
  driver.findElement(By.className("aoT")).sendKeys(subject);
  Thread.sleep(3000);
  driver.findElement(By.xpath("//td[@class='Ap']/div[2]/div[@class='Am Al editable LW-avf']")).sendKeys("Write Text For an email.!!! :)");
  Thread.sleep(3000);
  driver.findElement(By.xpath("//div/div[text()='Send']")).click();
  Thread.sleep(3000);
  driver.findElement(By.xpath("//div/div/span/a[text()='Sent Mail']")).click();
    
  // To verify whether an email has been sent or not
  if(driver.findElements(By.xpath("//tr/td[6]/div/div/div[2]/span/b[text()='"+subject+"']"))!=null){
   System.out.println("Email Sent Successfully... :)");
  }
  else{
   System.out.println("Email Sending Failed... :(");
  }
  
  // To get the number of the unread emails in inbox folder
  driver.findElement(By.xpath("//div[@class='TN GLujEb']/div/span[@class='nU n1']/a")).click();
  String inboxString=driver.findElement(By.xpath("//div[@class='aim ain']/div/div/div/span/a")).getText();
  String inboxElements = inboxString.substring(inboxString.indexOf("(") + 1, inboxString.indexOf(")"));
  System.out.print(inboxElements);
  driver.close();  
 }

}