Saturday, May 24, 2014

Web Driver API commands and UI Elements

Selenium Web Driver API commands

Get your web page

By calling get, you can get your web page.
driver.get("http://www.google.com");

Web Elements

By ID
For locating the UI elements(Web Elements) , you can use WebElement.

See below example,
<div id="languages">
   .........
   .........
   .........
</div>

Now you can get that above element through below command.
WebElement element = driver.findElement(By.id("languages"));

By Class Name
The same concept you can apply to all other UI elements. But it will differ little.

See below example,
<div class="border"><span>RED</span></div>
<div class="border"><span>GREEN</span></div>

List<WebElement> border = driver.findElements(By.className("border"));

Above we are using className method, due to it belongs to class DOM.
and why we are using List, since the class name might be present in multiple places.

By Tag Name
Same concept DOM tag name should mentioned here.

See below example,
<iframe src="..."> </iframe>

WebElement frame = driver.findElement(By.tagName("iframe"));

By Name
You can find the element for an input element.

See below example,
<input name="javahit" type="text"/>

WebElement javaHit = driver.findElement(By.name("javahit"));

By Link Text
You can find the link element with matching visible text.

See below example,
<a href="http://www.google.com/search?q=test">test</a>>

WebElement Test = driver.findElement(By.linkText("test"));

By Partial link text
You can find the link element with partial matching visible text.

See below example,
<a href="http://www.google.com/search?q=color">search for color</a>>

WebElement Color = driver.findElement(By.partialLinkText("color"));

By CSS
It is a locator strategy by CSS. It should be handled with care. since browser version will differ.

<div id="test"><span class="sample">water</span><span class="sample ok">Checking CSS</span></div>

WebElement Checking CSS = driver.findElement(By.cssSelector("#test span.sample.ok"));

By XPATH
Mostly webDriver uses native XPATH, wherever possible cases. Suppose if the browser dont have native XPATH support, you have to give your own implementation.
So you should know the difference various XPATH engines.

Driver
Tag and Attribute Name
Attribute Values
Native XPath Support
HtmlUnit Driver
Lower-cased
As they appear in the HTML
Yes
Internet Explorer Driver
Lower-cased
As they appear in the HTML
No
Firefox Driver
Case insensitive
As they appear in the HTML
Yes

See the below example carefully. So that you can understand this XPATH concept clearly.

<input type="text" name="java" />
<INPUT type="text" name="hit" />

Above I given one input as in Small Letter and another one as Capital Letter.
List<WebElement> inputs = driver.findElements(By.xpath("//input"));

See the below table now.

XPath expression
HtmlUnit Driver
Firefox Driver
Internet Explorer Driver
//input
1 (“java”)
2
2
//INPUT
0
2
0


Sunday, May 18, 2014

Selenium web driver configuration in eclipse


Just right click on your java projects, click Build Path --- > Configure Build Path


After clicking configure build path, the below window will open. Now navigate to Libraies Tab. Click Add External JARs...


Now your browse window will open. Select your selenium jar means, selenium-java-2.41.0 and selenium-java-2.41.0-srcs. 

Again click Add External JARs.... Browse window will open. Now select all jar inside lib folder. 
click Ok. So the final window will look like the below.



Thats all. Now you have successfully configured.



Friday, May 9, 2014

Null pointer deference in find bugs

I have faced null pointer deference, when I checked the code standard with tool. It shows NULL pointer deference in severity 1.

Normally it will come when you are not handling null values. For example,

if ( URL != null) {System.out.println( "URL is NOT NULL + URL );}

ok fine. You are checking URL is not null. But in case , URL come as null, what will happen? Null pointer Exception error will thrown.

So this error called Null Pointer Dereference error. ok, How to handle this. See below for solution.

if ( URL != null) {System.out.println( "URL is NOT NULL + URL );}
        else {
        System.out.println( "URL is NULL " );
         throw new IllegalArgumentException();
}

Thats all.

Thursday, May 8, 2014

Appears to call the same method on the same object redundantly in FindBugs

Issue Here below:

try {
----------
----------
}
catch (final SQLException e) {
LOG.error("Error Message Test: " + e.getMessage());
            throw new YourException(e.getMessage(), e);
}


Solution for that:

try {
--------------
--------------
 } catch (final SQLException e) {
        String errorMessage = e.getMessage();
            LOG.error("Error Message Test: " + errorMessage);
            throw new YourException(errorMessage, e);

Wednesday, May 7, 2014

Why toString() method in bean class?

Why toString() method in bean class?

We can use it to print object values directly, if overriding toString() method. If you will try to print object without overriding toString() method, it will print some hexadecimal values.

Generally we will use toString() in bean class, to check the bean property values.

For example,

public class TestBean implements Serializable {

private string name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name= name;
}

public int getAge() {
return age;
}

public void setAge(String age) {
this.age= age;
}

@Override
public String toString() {

return "TestBean [name=" +  name  + ", age=" + age + "]";
}
}


Now you should set values to this bean property. Now somewhere you can create object for this bean class like below.

TestBean test = new TestBean();

try to print the test object ,
System.out.println(test);

It will display the value like below.

=TestBean[name=test, age=19 ]

So you can use this to debug or log etc.,



Saturday, May 3, 2014

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

You may get this error while doing build with Maven. Here the solution,

Just follow the images and provide the information based on that,



After that, follow the next screen , remember select execution environments inside Installed JREs.