Tuesday, 29 October 2013

Conditional and concatenation operator


The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

|| Conditional-OR
The following program, ConditionalDemo1, tests these operators:

class ConditionalDemo1 {
    public static void main(String[] args){
        int value1 = 1;
        int value2 = 2;
        if((value1 == 1) && (value2 == 2))
            System.out.println("value1 is 1 AND value2 is 2");
        if((value1 == 1) || (value2 == 1))
            System.out.println("value1 is 1 OR value2 is 1");
    }
}
Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

The following program, ConditionalDemo2, tests the ?: operator:
class ConditionalDemo2 {
    public static void main(String[] args){
        int value1 = 1;
        int value2 = 2;
        int result;
        boolean someCondition = true;
        result = someCondition ? value1 : value2;

        System.out.println(result);
    }
}
Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).
<c:set> for this.

<c:set var="promoPrice" value="4.67" />
<c:set var="promoPriceString" value="ONLY $${promoPrice}" />
<p>${not empty promoPrice ? promoPriceString : 'FREE'}</p>
In this particular case, another way is to split the expression in two parts:

<c:set var="promoPrice" value="4.67" />
<p>${not empty promoPrice ? 'ONLY $' : 'FREE'}${promoPrice}</p>
If ${promoPrice} is null or empty, it won't be printed anyway.

In the upcoming EL 3.0 (JSR-341, part of Java EE 7), a new string concatenation operator & will be available. See also EL 3.0 spec - Operators. Your specific use case can then be solved as follows:


<p>${(promoPrice != null) ? ("ONLY" & $${promoPrice}") : "FREE"}</p>

Saturday, 26 October 2013

Write Your First Functional Selenium Test-selenium training



Write Your First Functional Selenium Test
if you are going to write your Selenium tests in a high-level language like Java, Ruby, Python, C#, and PHP. If  your coding skills need the help of a record/playback tool to write test scripts.
Writing test scripts in a high level language is easy. I recommend you learn the concepts behind Selenium PageObjects first. PageObjects is an object oriented library for building easily maintainable tests. It separates test code into a Model, View, Controller pattern. Read a blog and watch a screencast on PageObjects. Other object oriented test scripting solutions include: GEB, Fitness.

A very simple Selenium test looks like this in Java:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class temp script extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://localhost:8080/", "*iexplore");
    }
    public void testTemp script() throws Exception {
        selenium.open("/BrewBizWeb/");
        selenium.click("link=Start The BrewBiz Example");
        selenium.waitForPageToLoad("30000");
        selenium.type("name=id", "bert");
        selenium.type("name=Password", "biz");
        selenium.click("name=dologin");
        selenium.waitForPageToLoad("30000");
    }
}

The super constructor setUp tells the Selenium client library to connect to the Selenium RC service to use Microsoft Internet Explorer (*iexplore) and the base URL of http://localhost:8080/. All subsequent Selenium commands will be relative to the base URL. For example, selenium.open("/BrewBizWeb/") commands Selenium RC to tell IE to open http://localhost:8080/BrewBizWeb/.

Once you write this Java class, write an Ant script to compile the code and package it into a Java Archive Resource (JAR) file. Most Selenium users will run their Selenium tests from the Ant script itself. That makes it easy to integrate the test with a Continuous Integration environment as described
There are great tutorials on the SeleniumHQ.org documentation site to explain Selenium test script authoring in more depth.

Selenium has many pitfalls. For example, many Selenium tutorials offer instruction to use XPath expressions to locate elements in a Web page. Do not use XPath if you can avoid it. The Microsoft Internet Explorer browsers do not feature a native XPath expression evaluator. One of my Selenium tests takes 3 minutes to run in Firefox and takes 30 minutes to run in IE depending on the XPath expressions in the Selenium script. The Selenium Load Testing screencast explains the problem and solution.

Learn the Selenium language basics, contructors, element locators, and event handling by watching the Selenium Basics screencast.

Thursday, 24 October 2013

TARGET ELEMENTS USING CSS SELECTORS-Selenium Training


I strongly recommend CSS selectors as the locating strategy of choice.

CSS selectors are considerably faster than XPath and can find the most complicated objects in any HTML document.

Additional documentation on CSS selectors is available on the link below

There are several CSS selectors, but I am going to introduce you to a few of them as we gradually build our case to more complex examples.

(a) Element Selectors

(b) ID Selectors

(c) Class Selectors

<html>
    <head>
        <title>CSS Selectors</title>
    </head>
    <body>
        <h3 id="moduleHeader">Page Module Header</h3>
        <div class="primaryContainerModule">

            <h4 id="pageSub">Subtitle</h4>
        </div>
        <form id="pageForm" method="post" action="process.php">
            <input type="text" name="username" value="" />
            <input type="password" name="password" value="" />
            <input type="submit" name="submit button" value="Submit Form" />
        </form>
    </body>
</html>

Element Selectors
Element selectors use the name of the element to locate the element.

In the above document, we can target the subtitle by using the following CSS selector

css=h4

We can also target the page module by using the following css selector

css=h3

ID Selectors (CONTAINS HASH OR POUND SIGN)

ID selectors use place a hash or pound sign in front of the id value of that element

In the above document, we can target the subtitle by using the following CSS selector

css=#pageSub

We can also target the page module by using the following css selector

css=#moduleHeader

Class Selectors (CONTAINS DOTS)

Class selectors place a dot in front of the class attribute value of that element

In the above document, we can target the primary container by using the following CSS selector

css=.primaryContainerModule

Class Selectors Multiple (CONTAINS DOTS)


<input type="checkbox" class="europe asia africa" value="Continent" />

If the element has multiple classes, you can target it by specifying the classes concatenated using dots

css=.europe.asia.africa

COMBINING SELECTORS

Sometimes when a particular selector matches more than one element, you may need to combine selectors to clarify things

Here is some of that syntax

css=div#party

In the above example, we are combining the div element selector with the party id selector

css=div.party

In the above example, we are combining the div element selector with the party class selector

SELECTING ELEMENTS WITH CERTAIN ATTRIBUTES
We can also select certain elements that have certain attribute values.

In the form above, we can target the submit button by using the following syntax.

css=input[type=submit]

SELECTING ELEMENTS USING MULTIPLE ATTRIBUTES

<input type="text" class="europe asia africa" value="Continent" />
<input type="text" class="europe asia africa" value="Company" />
<input type="checkbox" class="europe asia africa" value="Continent" />
<input type="checkbox" class="europe asia africa" value="Company" />
<input type="hidden" class="europe asia africa" value="Continent" />

In the above example, if we wanted to target the checkbox with value Continent, we can use the following selector:

css=input[type=checkbox][value=Continent]

DESCENDANT SELECTORS

This type of selectors are using to access elements located with other elements when the locator can hit multiple matches

A space is needed between each selector leading to the target from the ancestor to the descendant going from left to right

In the HTML source below, we can use the descendant selectors to distinguish between the Page Module Header and the Subtitle for and automation engineer.

If we want to target the h3 element for the subtitle for an automation engineer, we cannot use css=h3 because selenium will stop the search once it comes across the Page Module Header h3 element.

Selenium will always stop the search as soon as it finds the first match. So we need to be more specific by using descendant selectors.

Using the following CSS selector will allow us to distinguish between the Page Module Header and the Subtitle for an Automation Engineer.

css=div.primaryModuleContainer div.automationEngineer h3

<html>
    <head>
        <title>CSS Selectors</title>
    </head>
    <body>

        <div>
            <h3>Page Module Header</h3>
        </div>

        <div class="primaryContainerModule">

            <video width="320" height="240" controls="controls"></video>

            <div class="softwareEngineer">
                <h3>Subtitle</h3>
            </div>

            <div class="qualityAssuranceEngineer">
                <h3>Subtitle</h3>
            </div>

            <div class="automationEngineer">
                <h3>Subtitle</h3>
            </div>

            <div>
                <video width="320" height="240" controls="controls"></video>
            <div>

        </div>
    </body>
</html>




Tuesday, 22 October 2013

Installing Eclipse-Selenium


we will use the Eclipse software for writing, compiling, and testing programming assignments. Although you can use Eclipse at school (see Launching Eclipse on Solaris) we highly recommend you install the Eclipse environment on your computer. Accessing Eclipse remotely through the campus network is theoretically possible, but extremely slow.

The Eclipse installation process has four main steps:

Install the Java 5.0 software.
Install the Eclipse 3.1 software (It does not includes Java 5.0).
Launch Eclipse.
Install a set of plugins designed especifically for courses taught in the Dept of Computer Science.
The files you will need to download are quite large so it is necessary you have a fast internet connection. For people that have slow connections (e.g., telephone modem), we can provide a limited number of CD's with the required software or you could copy the files to a CD from one of the wam labs on campus.   Please contact your instructor or teaching assistant for more details.

Next we describe the installation process for Windows and for Mac OS X.  If you have a system different from Windows or Mac OS X (e.g., linux) see the "Software Source" section below.  Feel free to skip the section that does not apply to your system.

Installation Process for Windows

Install Java 5.0
Download the file jdk-1_5_0_04-windows-i586-p.exe.
Double-click on the previously downloaded file and following the installation instructions.
Install Eclipse 3.1 software (It does not includes Java 5.0)
Download the file eclipse-SDK-3.1-win32.zip.
Once you have unzipped the previously downloaded file, you will see a folder named eclipse.  In that folder you will find the eclipse application (a big blue dot).  We recommend you create a shortcut on the desktop to simplify the launching of eclipse.  Notice that unlike Java, Eclipse does not have an installation process.  Once you have unzipped the file you are done.
Launch Eclipse
Follow the steps in launching Eclipse.
Installing the plugins
Launch eclipse if you have done so.
Follow the instructions provided on the web site http://www.cs.umd.edu/~pugh/eclipse.  After installing the new plugins, you will need to relaunch Eclipse.  Keep in mind that you will not be able to submit projects if you don't complete the installation of these plugins.
Installation Process for Mac OS X

Install Java 5.0
If you have a Mac and are planning to use Java 5.0 you must update your OS to Tiger.  As a student of the University you can obtain a copy of this software for $20.00.  Visit the web site Mac OS X v.10.4 for further information.
Follow the instructions on the web page (J2SE) 5.0 Release 1 for Tiger
Install Eclipse 3.1 software (It does not includes Java 5.0)
Download the file eclipse-SDK-3.1-macosx-carbon.tar
After double-clicking on the previously downloaded file, you will see a folder named eclipse.  In that folder you will find the eclipse application (a big blue dot).  We recommend you create a shortcut on the desktop to simplify the launching of eclipse.  Notice that unlike Java, Eclipse does not have an installation process.  Once you have untarred the file you are done.
Launch Eclipse
Follow the steps in launching Eclipse.
Installing the plugins
Launch eclipse if you have done so.
After installing the new plugins, you will need to relaunch Eclipse.  Keep in mind that you will not be able to submit projects if you don't complete the installation of these plugins.