Search

Thursday, July 16, 2015

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]

Problem : You are getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error while connecting to MySQL database from Java Program. You may be running your Java application directly from command prompt, shell script, ANT or Eclipse.

Cause : In order to connect to MySQL database, you need JDBC driver for MySQL. A class which implements java.sql.Driver interface for MySQL. Every vendor is responsible to implement this class for their databases. This driver implementation is provided by MySQL as MySQL java connector library. There is a class called com.mysql.jdbc.Driver which implements this interface.
Read more »

Wednesday, July 15, 2015

Steps To Install Firebug And Firepath Add-Ons In Firefox Browser

Earlier we learnt how to Install WebDriver Element Locator Firefox add-on and get XPath of any element using It as described In THIS POST. This planned post was pending to publish from last 2 years but was not getting time. Still many peoples (Specially beginners who has started learning selenium WebDriver) don't know how to use Firebug and FirePath to get XPath of any element and asking me to describe It. Let's Install firebug and fire path add-ons In Firefox browser to get XPath of element. Meanwhile you can read THIS POST to know how to locate element in IE only website using Fire-IE-Selenium Tool.

Steps To Install Firebug Add-On In Firefox Browser
Firebug Is Firefox add-on and helps us to get XPath of any element very easily. First of all you need to Install Firebug add-on In Firefox browser. Follow bellow given steps to Install firebug.
  • Click On "Add to Firefox" as shown In above Image. It will open Software Installation dialog as bellow.

  • Click on "Install Now" button as shown In above Image.
  • It will start Installing Firebug In Firefox browser. At the end of Installation, It will show message like "Firebug has been Installed successfully." and show Firebug Icon In toolbar section as shown In bellow Image.

Now Firebug Is Installed In Firefox browser. Next you need to Install Fire path.

Steps To Install FirePath Add-On In Firefox Browser
Follow the steps given bellow to Install Fire path

  • Open Mozilla Firefox browser.
  • Go to Fire path add on Installation page using URL -> https://addons.mozilla.org/en-us/firefox/addon/firepath/. It will take you to fire path Installation page as shown In bellow Image. Click on "Add to Firefox" button as shown In bellow given Image.
  • It will show Software Installation dialog as shown In bellow Image. Click on "Install Now" button. It will start Installing fire path add-on.
  • It will ask to restart Firefox browser at the end of Installation process as shown In bellow Image. Click on "Restart Now" as shown In bellow Image. It will restart Firefox browser.

  • Fire path Is Installed now.
How to verify FirePath Is Installed properly or not
Follow steps given bellow to verify fire path Is Installed properly or not,
  • Click on Firebug Icon from Firefox browser tool bar. It will open firebug window at bottom of the page.
  • You will see FirePath tab In Firebug window as sown In above Image. That means Firepath Is Installed properly and you can use It.

Next post will describe you how to attach firebug and firepath with firefox driver to inspect element in firefox driver browser instance on run time.

THIS POST will describe you how to get XPath of element using firebug and firepath.

Tuesday, July 14, 2015

'javac' is not recognized as an internal or external command

'javac' is not recognized as an internal or external command, operable program or batch file error comes when you try to compile a Java source file using javac command e.g. javac Helloworld.java but your PATH is not set properly. It means that javac.exe executable file, which exists in bin directory of JDK installation folder is not added to PATH environment variable. You need to add JAVA_HOME/bin folder in your machine's PATH to solve this error. You cannot compile and run Java program until your add Java into your system's PATH variable. Here is how this error looks like in command prompt of windows machine :
Read more »

Monday, July 13, 2015

How to use Final keyword in Java? Examples

You can use final keyword with variables, methods and classes in Java. You can use final modifier with variables to make them constant. A final variable is initialized only once in its lifetime, once initialized you cannot change its value. Though, you can initialize final variable at the time you declare them or you can initialize them at constructor, if they are blank final variable. A static final variable, also known as constant must be initialized in the same line. You can also use final modifier with methods to prevent method overriding. In Java, you cannot override final methods. This is usually done to signal that method is complete and its not designed for extension. It is also done to prevent someone deliberately or accidentally changing core logic of method by overriding it e.g. in template design pattern, the method which keeps the algorithm, also known as template method should be final, because outline of algorithm should be changed by child classes. You can also use final keyword with classes to make sure they are not extensible. In Java, you can not extend a final class to create sub-classes. Why you make a class final? doesn't it take your right of code reuse via Inheritance and extending its functionality? This is usually done to protect the class' invariant e.g. due security reasons. This is why String is also made final in Java. If you are beginner, just started learning Java, I suggest you to refer at-least one book alongside your research and practice. That will make you progress more in less time. One of the good Java book for beginner is Cay. S. Horstmann's Core Java Volume 1 and 2, which is both readable, easy to understand and comprehensive enough to learn Java in deep.
Read more »

Sunday, July 12, 2015

IEDriver - How To Resolve Set IE browser Zoom Level To 100% Error On RunTime

If your IE browser zoom level Is not set to 100% then you will get an error like "Browser zoom level was set to XYZ%. It should be set to 100% ".  Earlier we learnt In THIS POST -> How to resolve this error by setting IE browser's zoom level to 100% manually before running software test automation script. Now If you need
to run your software test script In different computers then everywhere you need to set browser's zoom level every time manually before running your software web application's test script. Otherwise your software automation test script will fail.

As described In my previous post, We can resolve "Enable Protected Mode For All Zones" error run-time. So can we do same thing for browser zoom level? Answer Is Yes. We can apply some extra code In our software automation test script to set browser zoom level 100% run-time automatically.

Here we will use same class DesiredCapabilities of WebDriver Interface which we have used In previous post to resolve error. Here we will set browser's capability to Ignore zoom setting and disable the native events. Then we will set browser zoom level to 100%. Bellow given code snippet will perform above given all operations and set browser zoom level 100%.

// Set desired capabilities to Ignore IEDriver zoom level settings and disable native events.
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("EnableNativeEvents", false);
caps.setCapability("ignoreZoomSetting", true);

// Initialize InternetExplorerDriver Instance using new capability.
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

// Press CTRL + 0 keys of keyboard to set IEDriver Instance zoom level to 100%.
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));

To experiment above code snippet practically,

  • Set your IE browser's zoom level to 50% as shown In bellow Image



  • And then run bellow given test script In eclipse.

package Testing_Pack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class IESetZoom {
public static void main(String[] args) throws Exception {

// Set path of IEDriverServer.exe
// Note : IEDriverServer.exe should be In D: drive.

System.setProperty("webdriver.ie.driver", "D://IEDriverServer.exe");

// Set desired capabilities to Ignore IEDriver zoom level settings and disable native events.
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("EnableNativeEvents", false);
caps.setCapability("ignoreZoomSetting", true);

// Initialize InternetExplorerDriver Instance using new capability.
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

// Press CTRL + 0 keys of keyboard to set IEDriver Instance zoom level to 100%.
driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));

// Load sample calc test URL
driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");

// Execute sample calc test.
driver.findElement(By.xpath("//input[@id='1']")).click();
driver.findElement(By.xpath("//input[@id='plus']")).click();
driver.findElement(By.xpath("//input[@id='6']")).click();
driver.findElement(By.xpath("//input[@id='equals']")).click();
String result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
Thread.sleep(5000);
System.out.println("Calc test result Is : " + result);
driver.close();
}
}

Above test will be executed without any error. This Is the best way to resolve Set IE browser Zoom Level To 100% error. It will apply zoom level settings automatically on each machine where this software test script will be executed. So you no need to worry about It. Next post will show you how to handle SSL certificate error in IE browser.

Saturday, July 11, 2015

How to parse String to long in Java? Example

You can parse a String literal containing valid long value into long primitive type using parseLong() and valueOf() method of java.lang.Long class of JDK. Though there are couple of difference between valueOf() and parseLong() method e.g. valueOf() method return a Long object while parseLong() method return a Long object, but given we have autoboxing in Java, both method can used for parsing String to create long values. In last article, you have learned how to convert Long value to String in Java and in this tutorial, you will learn opposite, i.e. how to parse an String to long value in Java. As I said, there are couple of ways to do it, but the most important method is parseLong(). This method is responsible for parsing input String and creating primitive long value corresponding to input String. It does input validation and throws NumberFormatException if you pass String which is not valid long value e.g. alphanumeric String, String containing characters other than +, - and numbers, long values which are out of range, lonely + or - character etc.  You can also use constructor of Long class which accepts a String and returns a Long object, but internally it also uses parseLong() method. BTW, if you have just started learning Java or looking forward to learn Java from scratch, I suggest you to take a look at Cay S. Horstmann's Core Java Volume 1, 9th Edition book. You will learn most of Java fundamentals in quick time.
Read more »

Friday, July 10, 2015

How to convert long to String in Java? Example

There are three main ways to convert a long value to String in Java e.g. by using Long.toString(long value) method, by using String.valueOf(long) and by concatenating with an empty String. You can use any of these method to convert a long data type into String object. It's not very different than from how you convert an int to String in Java. Same method applies here as well. String concatenation seems the easiest way of converting a long variable to String, but others are also convenient.  If you look Java code for valueOf() method from java.lang.String class you will realize that it actually calls the toString() method of java.lang.Long class for conversion, which means there is only one method where logic of converting primitive long to String is written. Nevertheless, if you are not converting long in loop all method will perform same, but if you have to convert large number of long values to String then directly using Long.toString() method can be quite useful. 
Read more »