Friday, November 13, 2020

 Types of Test Automation Frameworks

Although there are many test automation frameworks are designed to automate applications, following 3 frameworks are most popular in the testing world.

  1. Keyword-Driven Framework
  2. Data-Driven Framework and
  3. Hybrid Testing Framework

 Other Frameworks available to automate the software applications are:

  • Linear Scripting Framework
  • Modular Based Automation Framework
  • Library Architecture Testing Framework
  • Behaviors Driven Development Framework

Tuesday, April 15, 2014

How to check if my string is equal to null?

How to check if my string is equal to null?


Solution:

public static boolean isEmpty(String string) {
    return string == null || string.length() == 0;
}
Usage:
if (isEmpty(string)) {
  // do something
}
Explanation:
As noted above, the || and && operators short circuit. That means as soon as they can determine their value they stop. So if (string == null) is true, the length part does not need to be evaluated, as the expression would always be true. Likewise with &&, where if the left side is false, the expression is always false and need not be evaluated further.
As an additional note, using length is generally a better idea than using .equals. The performance is slightly better (not much), and doesn't require object creation (though most compilers might optimize this out).

Wednesday, July 17, 2013

How to download a file in WebDriver

Here's a solution. Set Firefox's preferences to save automatically, and not have the downloads window popup. Then you just grab the file, and it'll download.

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestCase {
   
    FirefoxProfile firefoxProfile = null;
    WebDriver driver = null;
   
    @BeforeClass(alwaysRun=true)
    public void setUp(){
        File downloadDir = new File("c:\\downloads");
        if (!downloadDir.exists()){
            downloadDir.mkdir();
        }
    }
   
    @BeforeMethod
    public void beforeMethod() {
        firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("browser.download.folderList",2);
        firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
        firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
        firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","image/jpg, text/csv,text/xml,application/xml,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/excel,application/pdf");       
    }
   
    @Test
    public void downloadFile(){
        driver = new FirefoxDriver(firefoxProfile);
        driver.get("http://support.vendhq.com/entries/21265746-Sample-CSV-file-to-upload-into-trial-account");
        driver.findElement(By.linkText("vend_products_31Aug12.csv")).click();
        driver.close();
    }
}

 Note:
The value of browser.download.folderList can be set to either 0, 1, or 2.
When set to 0, Firefox will save all files downloaded via the browser on the user's desktop.
When set to 1, these downloads are stored in the Downloads folder.
When set to 2, the location specified for the most recent download is utilized again.

Thursday, November 22, 2012

Difference between "==" operator and equals() method


Difference between "==" operator and equals() method

The “==” operator:

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location. A very simple example will help clarify this:

By definintion, the objects are all created on the heap. When you create an object, say,

Object ob1 = new SomeObject();
Object ob2 = new SomeObject();

We have 2 objects with exactly the same contents, lets assume. We also have 2 references, lets say ob1 is at address 0x1234 and ob2 is at address 0x2345. Though the contents of the objects are the same, the references differ.

Using == compares the references. Though the objects, ob1 and ob2 are same internally, they differ on using this operation as we comare references. ob1 at address 0x1234 is compared with ob2 at address 0x2345. Hence, this comparison would fail.

object.equals() on the other hand compares the values. Hence, the comparison between ob1 and ob2 would pass. Note that the equals method should be explicitly overridden for this comparison to succeed.

The equals() method: 

The equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.

The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal. Here is an example that will help clarify this:

String obj1 = new String("xyz");

String obj2 = new String("xyz");

if(obj1.equals(obj2))
   System.out.printlln("obj1==obj2 is TRUE");
else
  System.out.println("obj1==obj2 is FALSE");

Java’s String class overrides the equals() method to compare the characters in a string. This means that the comparison between the 2 String objects returns true since they both hold the string “xyz”.

Monday, November 12, 2012

Build Tools


Build tool allows developers and build managers to describe the build process also build tools help the programmer to build their project efficiently. 

Build tools are programs that automate the creation of executable applications from source code. Building incorporates compiling, linking and packaging the code into a usable or executable form.

Build process is repetitive process where programmer builds the project again and again to test the changes in the project code. While developing a project code changes needs to be test and the testing requires the build of the code and finally deploying on the server for testing. Build tools automate the repetitive process and allows the programmer to concentrate on the project development. It also reduces the overall build time. Build tools makes the build process just a single click work. 

In summary, a build tool provides a mechanism to describe the operations and structure of the build process. When the build tool is invoked it uses the description, examines the current state of affairs to determine the steps that need to be executed and in which order, and then manages the execution of those steps.

There are many build tools are available. Followings are few Java build tools available.
  • Rake
  • Ant
  • Maven

Thursday, September 27, 2012

waitForPageToLoad in Selenium 2.0/WebDriver

The following method can be used to wait until the expected page title appears.


public void waitForPageToLoad(String title){
     Integer i = 1;
     for(i=1;i<=100;i++) {
          if (driver.getTitle().equals(title)) {
               break;
          }
          Thread.sleep(10000);
     }
}
 

Wednesday, July 4, 2012

Encoding detector

http://code.google.com/p/juniversalchardet/

import org.mozilla.universalchardet.UniversalDetector;
 
public class TestDetector {
 
public static void main(String[] args) throws java.io.IOException {
   
byte[] buf = new byte[4096];
   
String fileName = args[0];
    java
.io.FileInputStream fis = new java.io.FileInputStream(fileName);

   
// (1)
   
UniversalDetector detector = new UniversalDetector(null);

   
// (2)
   
int nread;
   
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
      detector
.handleData(buf, 0, nread);
   
}
   
// (3)
    detector
.dataEnd();

   
// (4)
   
String encoding = detector.getDetectedCharset();
   
if (encoding != null) {
     
System.out.println("Detected encoding = " + encoding);
   
} else {
     
System.out.println("No encoding detected.");
   
}

   
// (5)
    detector
.reset();
 
} }