Tuesday 31 December 2013

Working with the Java Assertion




Java Assertion or assert keyword in java is little unknown and not many programmer is familiar with this and it's been rarely used specially if you have not writing unit test using JUnit which extensively uses Java assertion to compare test result.
Assertion not only improve stability of code but also help you to become better programmer by forcing you to think about different scenario while writing production quality code and improving your think through ability.
Test your assumptions during development.
Always assert that something is true.
AssertionError is thrown.
Disable by default.
Two different methods:
·         assert     (y > x);
·         assert     (y > x): “y is “+y+” x is “+x;

We were not supposed to make assumptions, but we can’t help it when we’re writing code.
if (x > 2 && y) {
// do something
} else if (x < 2 || y) {
// do something
} else {
// x must be 2
// do something else
}
You write print statements with them:
while (true) {
if (x > 2) {
break;
}
System.out.print(“If we got here something went horribly wrong”);
}
     Added to the Java language beginning with version 1.4, assertions let we test our
assumptions during development, without the expense (in both your time and program overhead) of writing exception handlers for exceptions that we assume will never happen once the program is out of development and fully deployed.

java -ea com.testing.TestClass
or
java -enableassertions com.testing.TestClass
Disable:
java -da com.testing.TestClass
or
java -disableassertions com.testing.TestClass
no arguments
package names
class names
Don’t use assertions to validate arguments to a Public Method.
Use assertions to validate arguments to a Private Method.
Don’t use to validate command-line arguments.
Don’t use assert expressions can cause side effects.


Monday 30 December 2013

Reusable Script In Selenium


Reusable Script In Selenium
import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import com.thoughtworks.selenium.DefaultSelenium;
public class ReusableScript {
public static DefaultSelenium selenium = new DefaultSelenium("localhost",
6666, "*iehta", "http://");
public static String gmail() throws Exception {
selenium.start();
selenium.open("http://www.gmail.com");
selenium.windowMaximize();
Thread.sleep(2000);
selenium.click("link=Create an account »");
selenium.waitForPageToLoad("30000");
return "pass";
}
public static String Register() throws Exception {
FileInputStream fi = new FileInputStream(
"D:\\Framework\\TestData\\Register.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
for (int i = 1; i < s.getRows(); i++) {
if (!s.getCell(3, i).getContents().equals("")) {
if (s.getCell(2, i).getContents().contains("Text")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.type(s.getCell(0, i).getContents(), s.getCell(
3, i).getContents());
}
} else if (s.getCell(2, i).getContents().contains("Combo")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.select(s.getCell(0, i).getContents(), "label="
+ s.getCell(3, i).getContents());
}
} else if (s.getCell(2, i).getContents().contains("chkbox")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.click(s.getCell(0, i).getContents());
}

} else if (s.getCell(2, i).getContents().contains("Radio")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.click(s.getCell(0, i).getContents());
}

}
else if (s.getCell(2, i).getContents().contains("Button")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.click(s.getCell(0, i).getContents());
}
}
}
}

return "pass";
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
gmail();
Register();
}
}

Wednesday 25 December 2013

what is validation?

Validation checks that the product design satisfies or fits the intended use (high-level checking), i.e., the software meets the user requirements. 

This is done through dynamic testing and other forms of review.
Validation: Are we building the right product? (This is dynamic process for checking and testing the real product. Validation always involves with executing the code)

Validation: The process of evaluating software during or at the end of the development process to determine whether it satisfies specified requirements. [IEEE-STD-610]

It is a High level activity.

Performed after a work product is produced against established criteria ensuring that the product integrates correctly into the environment.

Determination of correctness of the final software product by a development project with respect to the user needs and requirements.

Validation ensures that the software operates as planned in the requirements phase by executing it. Running predefined test cases and measuring the output with expected results.


Handle Mouse Events

Handle mouse events in Java 1.1 (deprecated)
Be careful: This way of handling events in Java is marked as being deprecated since Java 1.2 and will generate compiler warnings. But since many users still use Internet Explorer 5 which uses the JDK standard of 1.1 this way of handling events can still be used. But if you want to use an up to date event handling please read the chapter about the new event handling and use the method described there. Nevertheless this chapter here might explain some features in more detail so you might want to read it anyway
To do this you have to implement just one method in our Main class called mouseDown . This method should look like this:
// Method to handle mouse down events
public boolean mouseDown (Event e, int x, int y)
{
// Change direction
x_speed = - (x_speed);

// DON'T FORGET (although not necessary here)!!
return true;
}
Additionally to that you have to change a little bit in the run() - method to achieve the behaviour I described. Please see the source code to this chapter for details.

Of course you can handle other events than the mouseDown event. Java makes it possible to overwrite the following methods the same way as I did above and handle other mouse events that way. Here comes a list of the methods:

public boolean mouseDown (Event e, int x, int y): handles events that occur if mouse button is pressed down
public boolean mouseUp (Event e, int x, int y): handles events that occur if mouse button is released again.
Use the variable e.clickCount to get the number of clicks performed. That's how you can handle for example double clicks! You'll see a example for this in chapter 5 ("Our first game").
Mouse movement:
public boolean mouseMove (Event e, int x, int y): handles events that occur if mouse is moved over the applet
public boolean mouseDrag (Event e, int x, int y): handles events that occur if mouse is moved over the applet with pressed mouse button
public boolean mouseEnter(Event e, int x, int y): handles events that occur if mouse enters the applet
public boolean mouseExit (Event e, int x, int y): handles events that occur if mouse leaves the applet
With help of the variables int x and int y you can get the coordinates where the event happend, which is very important for many games. Also you have to take care of the return statement "return true;" that has often no meaning for the functionality of your application .but is expected by the method.

Tuesday 24 December 2013

String class and Function



Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

The most direct way to create a string is to write:

String greeting = "welcome";
Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "welcome”.

As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.

public class StringDemo{

   public static void main(String args[]){
      char[] hiArray = { 'h', ‘i’, '.'};
      String hiString = new String(hiArray); 
      System.out.println( hiString );
   }
}
This would produce the following result:

hi.
Note: The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make a lot of modifications to Strings of characters, then you should use String Buffer & String Builder Classes.

String Length:
Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object.

After the following two lines of code have been executed, len equals 17:

public class StringDemo {

   public static void main(String args[]) {
      String palindrome = "how are you doing";
      int len = palindrome.length();
      System.out.println( "String Length is : " + len );
   }
}
This would produce the following result:

String Length is : 17
Concatenating Strings:
The String class includes a method for concatenating two strings:

string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end. You can also use the concat() method with string literals, as in:

"My name is ".concat("soiya");
Strings are more commonly concatenated with the + operator, as in:

"Hello," + " world" + "!"
which results in:

"Hello, world!"
Let us look at the following example:

public class StringDemo {

   public static void main(String args[]) {
      String string1 = " are you ";
      System.out.println("how " + string1 + "doing");
   }
}
This would produce the following result:

How are you doing.
Creating Format Strings:
You have printf() and format() methods to print output with formatted numbers. The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object.

Using String's static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of:

System.out.printf("The value of the float variable is " +
                  "%f, while the value of the integer " +
                  "variable is %d, and the string " +
                  "is %s", floatVar, intVar, stringVar);
you can write:

String fs;
fs = String.format("The value of the float variable is " +
                   "%f, while the value of the integer " +
                   "variable is %d, and the string " +
                   "is %s", floatVar, intVar, stringVar);
System.out.println(fs);