Tuesday 29 October 2013

TARGET ELEMENTS USING CSS SELECTORS

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.




<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 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 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


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

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]


<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]


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>



Saturday 26 October 2013

Write Your First Functional Selenium Test-selenium online 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

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 navnaz 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("/ navnazWeb/") commands Selenium RC to tell IE to open http://localhost:8080/ navnazWeb/.

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.