Saturday, 22 April 2017

Use of Log4j in your Java Program


Download Sample Project from here.

Steps to use log4j :

1.) Download log4j dependency.(included in sample project)
2.) Download log4j.properties (included in sample project)

Create Java Program:

package com.asj;

import org.apache.log4j.Logger;

public class LoggerDemo {
/**
* @created by Aman
*/
public static Logger logger = Logger.getLogger(LoggerDemo.class);

public static void main(String[] args) {
logger.info("Hello!! you are in info log");

logger.warn("Hello!! you are in warn log");

logger.error("Hello!! you are in error log");

logger.debug("Hello!! you are in debug log");
}

}

Monday, 2 February 2015

Extract Hbase table through Mapreduce and print table data in to hdfs file (Sample program)

Create Hbase Table


create 'test1', 'cf1'
put 'test1', '20130101#1', 'cf1:sales', '100'
put 'test1', '20130101#2', 'cf1:sales', '110'
put 'test1', '20130102#1', 'cf1:sales', '200'
put 'test1', '20130102#2', 'cf1:sales', '210'

Create Java project with 3 files and include all Hbase required Jars:-

testDriver.java :

package TableDataHdfsFile;

import java.io.File;
import java.io.IOException;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.commons.io.
FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class testDriver
{   
      static String FilePath = "/home/admin/Test/mysummary";
      public static void main(String[] args) throws Exception
      {
          File file = new File(FilePath);
          if (file.exists())
          {
              FileUtils.cleanDirectory(file); //clean out directory (this is optional -- but good know)
              FileUtils.forceDelete(file); //delete directory
          }

        FileSystem fs = FileSystem.get(config);
          if (fs.exists( new Path(FilePath)))
          {
              fs.delete(new Path(FilePath), true); // delete file, true for recursive
          }
          Configuration config = HBaseConfiguration.create();
          Job job = Job.getInstance(config);
          job.setJobName("ExampleSummaryToFile");
          job.setJarByClass(testDriver.class);     // class that contains mapper and reducer

          Scan scan = new Scan();
          scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobs
          scan.setCacheBlocks(false);  // don't set to true for MR jobs
          // set other scan attrs

          TableMapReduceUtil.initTableMapperJob(
                  "test1",        // input table
                  scan,               // Scan instance to control CF and attribute selection
                  testMapper.class,     // mapper class
                  Text.class,         // mapper output key
                  IntWritable.class,  // mapper output value
                  job);
          System.out.println(job.getJobName());
          job.setReducerClass(testReducer.class);    // reducer class
          job.setNumReduceTasks(1);    // at least one, adjust as required
         
        

          FileOutputFormat.setOutputPath(job, new Path(FilePath));  // adjust directories as required

          boolean b = job.waitForCompletion(true);
          if (!b) {
                  throw new IOException("error with job!");
          }
      }
}



testMapper.java :-

package TableDataHdfsFile;

import java.io.IOException;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;


public  class testMapper extends TableMapper<Text, IntWritable>  {
    public static final byte[] CF = "cf".getBytes();

    private final IntWritable ONE = new IntWritable(1);
       private Text text = new Text();

       public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException {
          // byte[] bSales = columns.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("sales"));
            String val = new String(value.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("sales")));
            text.set(val);     // we can only emit Writables...
            context.write(text, ONE);
       }
}

testReducer.java :-

package TableDataHdfsFile;

import java.io.IOException;
import org.apache.hadoop.io.
IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;



public class testReducer extends Reducer<Text, IntWritable, Text, IntWritable>  {

    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int i = 0;
            for (IntWritable val : values) {
                    i += val.get();
            }
            context.write(key, new IntWritable(i));
    }
}

//Create a jar and run basic program given above , we may parametrize this program  also.

// Done

Saturday, 8 November 2014

Invoke Maven Project by - Jenkins !!



How to invoke maven project through Jenkins steps are below :-

1. Download and install jenkins.

2. Run jenkins.war by following command :-

java -jar jenkins.war --httpPort=9090
If you want to use https use the following command:
java -jar jenkins.war --httpsPort=9090

3. Create Jenkins Job:-

Create TestMaven Job


before creating job ensure that you set jdk path in jenkins configuration.


4. Set your POM.xml file path and build jenkins job :-









Done :)

For Creating Maven Project :- Link





Friday, 7 November 2014

How To Create Maven Project !!


Here is the way to create way to create maven project :-

1.) Install Maven in your latest eclipse.

2.) Create Maven Project.

3.) Create Below Java file in package com under src/main/java -

package com;
import javax.swing.JOptionPane;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass {
   
     public static void main(String[] args) throws InterruptedException {
            // Create a new instance of the firefox driver
            // Notice that the remainder of the code relies on the interface,
            // not the implementation.
            WebDriver driver = new FirefoxDriver();

            // And now use this to visit Google
            driver.get("http://www.google.com");

            // Find the text input element by its name
            WebElement element = driver.findElement(By.name("q"));

            // Enter something to search for
            element.sendKeys("cheese!");

            // Now submit the form. WebDriver will find the form for us from the element
            element.submit();

            Thread.sleep(5000);
            // Check the title of the page
            JOptionPane.showMessageDialog(null, "Page title is: " + driver.getTitle());
                
            driver.quit();
        }

}

4.)  Edit your pom.xml

Here is edited  pom.xml :-

<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>kepler_maven</groupId>
    <artifactId>kepler_maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.44.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.TestClass</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

5.) Run pom.xml as Maven Install

6.) Here is the complete zipped project Maven Sample Project

Done !!





Thursday, 16 October 2014

Automatic File Download Selenium Java



Below code snippet for Firefox :-

Automatic File Download Selenium Java

---------------------------------------------------START-----------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import atu.utils.windows.handler.exceptions.WindowsHandlerException;


public class autoDownLoad {

public static void main(String[] args) throws WindowsHandlerException, InterruptedException {

 FirefoxProfile profile = new FirefoxProfile();
 String path="d:\\downloads123";
 profile.setPreference("browser.download.folderList", 2);
 profile.setPreference("browser.download.dir", path);
 profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
 profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain,application/octet-stream");
 profile.setPreference("browser.download.manager.showWhenStarting", false);
 profile.setPreference("browser.download.manager.focusWhenStarting", false); 
 profile.setPreference("browser.download.useDownloadDir", true);
 profile.setPreference("browser.helperApps.alwaysAsk.force", false);
 profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
 profile.setPreference("browser.download.manager.closeWhenDone", false);
 profile.setPreference("browser.download.manager.showAlertOnComplete", false);
 profile.setPreference("browser.download.manager.useWindow", false);
 profile.setPreference("browser.download.manager.showWhenStarting",false);
 profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
 profile.setPreference("pdfjs.disabled", true);
 WebDriver driver = new FirefoxDriver(profile);
driver.get(YOUR URL);
}

}


More Type--- > MIME TYPES

-----------------------------------------------------END------------------------------------------------------

Monday, 22 September 2014

Loggers In Ruby



require 'logger'

logger = Logger.new($stdout)
logger.warn("This is a warning")
logger.info("This is an info")


//$stdout redirects all logs to console. If you want to redirect all this to a file then //need to initialize $stdout='/home/Aman_Test/log.txt', 'w'

Monday, 1 September 2014

Selenium WebDriver and AutoIT

It is often possible in this web world that we get to work with objects which is either out of box,flash or webpages that are added with extra layer of security like a single sign-on for example windows credentials


Also another example is if one works with Microsoft technologies or Oracle CRM Web Applications which are only compatible with IE browsers there LOV(List of Values) popup , not to forget the modal popup

In the above situations it's hard to achieve or progress with automation with Selenium RC/Selenium WebDriver 

Hence the simplest and fairly easy option is AutoIT

What is AutoIt ?

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!

Let's get started with having AutoIt installed on your local box

Download latest version of AutoIt from the official release page here 

It's better you download AutoIt Full Installation which will help you in scripting & compiling as well

Once installation is completed you should see the application being installed in your All Programs or which ever location you have installed

Now, let's get started - What is that we are trying to automate or use AutoIt for

Given this Scenario : Of Logging in the windows Authentication to your webapplication

Just like the below screen shot



Windows login modal as popped out by Windows OS :

Here is how the AutoIt Script should look


WinWaitActive("Connect to yourservername","","60")
  If WinExists("Connect to yourservername") Then 
     ControlSend("Connect to yourservername","", "[CLASS:Edit; INSTANCE:2]","DomainName\username")
     ControlSend("Connect to yourservername","", "[CLASS:Edit; INSTANCE:3]","Password") 
     ControlClick("Connect to yourservername","","[CLASS:Button; INSTANCE:3,button[,clicks[,153[,223]]]]") 
EndIf 



Once you have the above script compiled - You are all ready to use them in your webDriver / selenium RC script
 so here is example for JAVA

So here's the code that goes in your webDriver script

        @BeforeClass 
        public static void setUp()
        {
              System.setProperty("webdriver.ie.driver", "C:\\Eclipse\\Selenium\\IEDriverServer.exe");
              driver = new InternetExplorerDriver();
              driver.get("your url");
              try {
                        Runtime.getRuntime().exec("C:\\Eclipse\\Selenium\\Autentication.exe");
                  } catch (IOException e) 
                  {
                              e.printStackTrace();
                   }
        }

You are all set now : both the code snippets together should help in working on Windows Authentication
 Courtsey : automationwithseleniumblog