Pages

Browser based Test Automation Tool

Today I am going to write about one of my own part time development, I have been working on for a few weeks. I have been working in this project in our company where the task was to automate the browser based functionality testing with the help of a Web-Driver API(selenium). We developed a framework for that particular application. I have created a simple application with the same knowledge in my free time, and its still under progress. The Product's name is "Snapshots".
Well as the title suggests, its a tool to automate your browser based click, and regress test. The tool is a simple Desktop application, where you could

  • Create a test plan(open a url, click, type, snapshot etc)
  • Save the test plan for later use
  • Load the saved test plan
  • See the results.



If you are interested to use the software, here is a link.

Git tagging and recovering tag

There are two kinds of tagging in git.
1. Annotated/signed tag
2. LightWeight tag

In lightweight tag, it is just a pointer to a commit, and no extra information. This one is used for temporary purpose.
Annotaged tag, creates a unmodifiable branch of the state of the repo.

To see the list of tags
git tag

To create light Weight tag

git tag <tag_name>
eg
git tag version_1.0

To check which commit the following tag points to

git rev-parse version_1.0

Reset the current branch to the specific tag

git reset --hard <tagname>

Byte Stream and Character Stream | Java

Recently a need arises, which made me notice the difference between the byte stream and character stream in java. As you recall, in java, byte is a primitive data type whose size is 8-bits, where character is a datatype which holds uni-code character(ie 16 bits). So, the byte Stream could only read/write ASCII characters(0-255). That means, It can only work with english letters and not other languages. Character stream operates on unicode character and so can deal with other language character as well. It is only supported prior to java 1.1.

Byte Stream has two classes

  • Input Stream Class
  • Output Stream Class
Character Stream has also two classes

  • Reader Classes
  • Writer Classes

15 most challenging programming assignments | Java

The best way to learn is to do.

Its fun to learn a programming language and concepts by solving challenges. I have been teaching programming to some of the students, and I collected these assignments for them. These should help others too.

Here goes the questions.

1. Write a program to check if a number is even or odd. The program should ask for an integer input, and then check if the input is even or odd
and print the appropriate message.(Hint: divided by 2 if the remainder is 0 then its even)

2. Write a program to find if a number is prime in java.(Division up to the square root is enough)

3. Find if a number is power of 2 in java. (Hint: one possible solution is using bit shift operator)

4. Write a program to sort an integer array without using the inbuilt methods provided by java.

5. Write a program in java to check if a number is an armstrong number.
(Hint: An armstrong number is a number whose sum of the cube of its digits is equal to the number.
eg 153 = 1*1*1 + 5*5*5+3*3*3)

6. Write a program to reverse any string in java.(you could use iterative method or recursive method. Try implementing both)

7. Write a program in Java to print Fibonacci series up to given number? Try both iterative and recursive version.

8. Write a program in java to calculate factorial of an integer number.(Iterative, Recursive)

9. Write a program in java that will ask for an integer input and then print a pattern.
(for eg. if the input is 5)
*
**
***
****
*****
****
***
**
*
10. Write a program that displays the temperatures from 0 degrees Celsius to 100 degrees Celsius and its Fahrenheit equivalent. Note that the conversion from Celsius to Fahrenheit uses the following formula: F = C * 9/5 + 32;

11. Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.

12. Write a number guessing game. The program will ask for an input(1 to 5). Then it should generate a random number and check if its matches with the users input. The program should do this ten times for a user, and then finally prints the score of the user.

13. In the above program, add a method to save the final score in a file if it is the highest one. The next time another user plays the game, the program should read the previous score from the file. if the current score is greater than the previous score, the program should print "You are the new winner", and then save the new highest score in the file before exiting. If the file is not present, the program should create a new one.

14. Write a program that will prompt "Enter a command".
- If you enter "pwd", it should print the current directory's location.
- if you enter "ls", it should print the name of all the files and directory's in the current directory.
- if you enter "exit", the program should exit.
- if you enter "help", it should print a help text about the commands it support and what they do.

15. Write a program that will prompt "Enter a command".
- if you enter "ls", it should print the name of all the files and directory's in the current directory.
- if you enter "ls | grep <pattern>", the program should print all the files and directories that match the <pattern>
- if you enter "exit" the program should exit.

Reading Input from Console | Java

In java, for reading the input from console, there are two options.

  1. Scanner
  2. BufferedReader
There are however some basic differences in the use of them.

Scanners treat the given input as tokens(can tokenize the input using regex). It provides methods like
nextInt()
and
next()
and so it support parsing capabilities. A bufferedReader however is not capable of this and can only read the characters from an inputStream and store as string to the buffer.

Scanners are a little bit slower then bufferedReader.

BufferedReaders are synchronized but Scanners are not. If the scanners are to be used in multiple threads, synchronization should be done manually.

Scanner has a little buffer(1kB) compared to BufferedReader(8kB).

A BufferedReader could be passed to the scanner as the source of characters to parse.

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a line");
        
        //String next = scanner.next();
        String text;
        
        while(( text = scanner.next())!= null){//
            System.out.println(text);
        }
    }

How to insert code in the article | Blogger | Formatting

While writing articles about programming, you have to write codes frequently in the articles. The problem is that you could not write code simply using the font and formatting of the text that the rest of your blog is using. Otherwise it won't look cool. But its kind of tricky to format the codes in your blogger article compared to the wiki or something else.
Well here's what I do for my formatting. Its not great, but I like the simplicity and separation both.
Suppose you have to write this article.

Hello World, Below is my code
Integer i = 1;

First of all, open your blogger and in the place to write the article, Write all the non-code article. Now blogger has two modes(Compose, Html). Go to the compose mode and Just in the place where you want to write your code, write this line
<code>code</code>

Now come back to the Compose mode. You can see the text "code" is actually in the format of a code. You could now copy your actual code, and then select the text "code" in the article editor and then paste it. You could also add custom CSS to make it different, like add border, background etc.

There are also custom javascripts and css syntax Highlighter out there in the internet. But I personally don't like them because they are a little bit big work in the beginning and they also kind of degrade the response time and add the volume of data to be transferred on each page load.

Simplicity is Beauty

Quick Guide to Java logging | log4j


Logging is a very important feature of any software. You could see the problem in your software through logs, trace the error, fix them etc. It is not only helpful in the development phase but in the Production as well.
If the logging module is not properly planned, in an enterprise software, it could cause all sorts of disadvantages.

Log4j is one of the most used logging api for java.

The various components of Log4j are
1. Loggers
These are hierarchical. For example com.abc and com.abc.def will have different hierarchy of log when you pass their class names while instantiating the logger. In the configuration file, you could configure particular module to one level of logging while the Root to the other, with the hierarchy.

static Logger log = LogManager.getLogger(LogTest.class.getName());

2. Appenders
Console appenders, File appenders, database appenders and more. Appenders define where the output will be. Appenders needs to be registered in a Logger. Then any log message in the logger is forwarded to all the appenders.

3. Layouts
This is responsible for formatting the log messages.
Pattern layout, html layout etc. You can have your own formator or you could simply use Pattern layout and append timestamp, etc in the front of the log message.

4. Priorities
(DEBUG < INFO < WARN < ERROR < FATAL)

5. Configuration
(log4j 2+ will find log4j.xml in the classpath). You could configure it through the code, but its a good idea to configure it through xml's or jsons. It could also auto renew the configuration when the configuration file is changed, without having to recompile or restart the application.

An Example of the log4j.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorinterval="30">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="File1" fileName="output.log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    
    <Loggers>
        <logger name="tutorials.LogTest" level="TRACE" additivity="false" >
            <AppenderRef ref="Console"/>
        </logger>
        <Root level="debug">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File1"/>
        </Root>
    </Loggers>
</Configuration>

The Logger is configure to refresh its settings every 30 seconds. There are two appenders, console and File.
The root logger is set to level debug, and both the appenders are registered to it. However for the class tutorials.LogTest, its level is Trace and so its TRACE messages will also be logged. The additivity=false code will prevent any duplicate logging(when the appenders are registered to it as well).

The Example of using the Logger
package tutorials;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @author Rajan Prasad Upadhyay
 */
public class LogTest {

    static Logger log = LogManager.getLogger(LogTest.class.getName());

    public static void main(String[] args) {
        log.trace("Trace Message!");
        log.debug("Debug Message!");
        log.info("Info Message!");
        log.warn("Warn Message!");
        log.error("Error Message!");
        log.fatal("Fatal Message!");
    }
}