Pages

6/01/2016

selenium javascript alert box

This is a easy task to capture javascript based alertbox ,

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package selenium_alert_box_example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 *
 * @author pradeek
 */

public class Selenium_Alert_Box_Example {

    public static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        driver = new FirefoxDriver();
        driver.get("http://www.samle");
        driver.findElement(By.xpath(".//*[@id='contentcolumn']/form/p/input")).click();
        Thread.sleep(2000);
        driver.switchTo().alert().dismiss();
    }
 }

Test step is like this , navigate to website, then click a link that will show javascript alert box,

driver.get will navigate to webpage, driver.click will click the link,

driver.switch will switch focus to alertbox, then alert() will identify the alertbox,
accept() and dismiss() is the action on this alertbox.

you can verify the alertbox by comparing the text in the alertbox and text we give. If the our text matches with our expectation , test will pass other wise it will fail.

package selenium_alert_box_example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
 *
 * @author pradeek
 */
public class Selenium_Alert_Box_Example {

    public static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        driver = new FirefoxDriver();
  driver.get("http://www.samplealert.com");
  driver.findElement(By.xpath(".//*[@id='contentcolumn']/form/p/input")).click();
  Thread.sleep(2000);
                //Assert.assertEquals("My name is George. Welcome!", driver.switchTo().alert().getText());
                if("My name is George. Welcome!".equals(driver.switchTo().alert().getText()))
                {
                    System.out.println("Test passed");
                }
                else
                {
                    System.out.println("Test failed");
                }
  driver.switchTo().alert().accept();  
    }    
}

for asserting you can either use the testNg or junit library code Assert.assertEquals or just use java if else conditions.

5/22/2016

installing perl dancer in ubuntu

For installing perl dancer you need curl utility for installing curl ,
 sudo apt-get install curl

Now we can install Perl-Dancer by this command, type this command in terminal 

curl -L http://cpanmin.us | perl - --sudo Dancer2 
 
 

creating first go program

for creating go program open your editor and paste this code, and save it as filename.go for example helloworld.go

package main

import "fmt"

func main() {
    fmt.Println("Hello, 世界")
}

I will explain these codes in details for now , we will look how to run these code.

to run type go run helloworld.go  in terminal , we get


you can try golang online as well , golang

installing go in ubuntu

Installing gccgo-go package on Ubuntu 14.04 (Trusty Tahr) is as easy as running the following command on terminal:

sudo apt-get update
sudo apt-get install gccgo-go

To check the version type go version in terminal .

creating python django project

If you installed and configured python django you can create django project .

for that create a folder any where in you system and open you terminal navigate to that folder and type the django command django-admin startproject projectname  for example django-admin startproject firstdjangoproject     


This command will create a folder firstdjangoproject        , navigate inside to the project folder . You can see file structure of django project

I will explain each of this files in detail for now lets run the application

for running the application need to type this command in your terminal

python manage.py runserver  

Ignore the migration exception , we can see the application running at localhost port 8000, 8000 is the default port . Now open your web browser and type address http://127.0.0.1:8000 and you can see

if you see this your django application is up and running and you are good to go.
press ctrl+c keys to stop the server.

python django installation on ubuntu

If you wish to install Django using the Ubuntu repositories, the process is very straight forward.

First, update your local package index with apt, and then install the python-django package:
sudo apt-get update
sudo apt-get install python-django
You can test that the installation was successful by typing:
django-admin --version 
 
If you wish to install the latest version of Django globally, a better option is to use pip, the Python package manager. First, we need to install the pip package manager. Refresh your apt package index:
sudo apt-get update
Now you can install pip. If you plan on using Python version 2, install using the following commands:
sudo apt-get install python-pip
Now that you have pip, we can easily install Django. If you are using Python 2, you can type:
sudo pip install django

You can verify that the installation was successful by typing:
django-admin --version 
 

5/19/2016

xpath

It is very easy if you understood the funta behind XPATH . xpath is XML path , XPath is used to navigate through elements and attributes in an XML document . As i said earlier both HTML and XML follows DOM structure , so we can use xpath to navigate elements and attributes in an HTML.

<html>
 <body>
   <div class="1">
     <div class="2"></div>
        <div class="1"></div>
        <div class="2">
           <div class="1">
              <a id="1">HI</a>
              <a id="2">
                 <h1>HI<h2>
              </a>
           </div>
        </div>
        <div class="3"></div>
     <div class="2"></div>
   </div>
 </body>
</html>

we can visualize the above HTML code in DOM structure we get

In Document object Model (DOM) , document is the parent object , then comes html,body,child and siblings , if we want to navigate to element h1, we can start from html inside the document , document is the outer cover of all elements.

if we want to reach body we can write its as
navigate so far (xpath) : /html/body        

Inside body  tag there is only one child which is a div element so we can write it as
navigate so far (xpath) : /html/body/div          

Inside the div element there are three div elements , so we can count it as 1,2,3 other wise returns 3 element array of div elements and we want to navigate to second div element , we can write it as

navigate so far (xpath) : /html/body/div/div[2]

Inside div[2] there is only one child which is a div element, so we can write it as 
navigate so far (xpath) : /html/body/div/div[2]/div 

Inside the div element we can see  two anchor elements and we can count it as 1,2 other wise which will return array of two anchor elements . Then we want to navigate to the second anchor element which we can write it as


navigate so far (xpath) : /html/body/div/div[2]/div/a[2]

Inside a[2] there is only one child that is the h1 , thats our required element and we can write it as 

 navigate so far (xpath) : /htm/body/div/div[2]/div/a[2]/h1

That is how xpath is take. There are two types of xpath 

  1. Absolute xpath 
  2.  Relative xpath
The above xpath we took earlier is called absolute path which contains the whole path . But there is problem in taking the absolute path because if there any change in dom will effect the navigation for example

In the above xpath /htm/body/div/div[2]/div/a[2]/h1   div[2] changes to div[1] or deleted the navigation will not work, this is where relative xpath come in to play

if we want to take the relative path of  the above absolute path , we can write it as .//a[@id='2']/h1      or .//*[@id='2']/h1     we will break this expressions in to parts 

[@id=2] , which means take any element in the document who has the id=2, in xpath attributes are represented by @id which has a value of 2 which is enclosed by two square brackets if you remember JS syntax.

so we can write it as [@id=2] and if we write a[@id='2']     this expression will return anchor tag <a> who has the attribute id=2 . 

if we write a[@id='2']/h1   this will returns the element who has path match with this expression which has h1 after a[@id='2']

if we write  //a[@id='2']/h1  the two forwarded slash means ,Selects nodes in the document from the current node that match the selection no matter where they are.

if we write .//a[@id='2']/h1   the dot in front of this means , Select the current node.


if we write *[@id='2']/h1  , There is an Asterisk comes in front of the squares means , There can be any elements comes , we wont mind the element , since id is unique and it is related with the <a> tag, it always returns the anchor element. thus we get .//*[@id='2']/h1


But when you write the expression we need to begin from left.

Now we can write path expressions using predicate which is supported by xpath.
for example [@id='2', predicates always enclosed in a square brackets . There are many other predicate which supports path expression .

for example div[2]  2 is a predicate , we can use last(),position(),@ for selecting attributes which i already said.

if we want to use predicate last() and position() we can use like this 

.//a[last()]/h1 this means , select the last anchor element in this path that is a[2] 

by predicate position we can write it as .//a[position()>1]/h1  which is a[2]
you can generate any kind of meaningful expression by mix and match this predicates.

5/18/2016

locating webelements using selenium


Record and playback is a Novice routine and I am not interested in it.

 Selenium provides several options to locate Web Elements such as (h1,p,div...) .

The attributes id,name,classname are used for identifying particular element inside html for example

<html>
 <body>
    <p id="para1">Hi</p>
    <input type=textarea name="text" >
    <p class="para2">Hello</p>
    <p class="para2">World</p>
    <a id="linkone">link1</a>
 </body>
<html>

#para1 {
  color: blue;
}
.para2 {
 color: green;
}

we are giving id to <p id="para1">  for identify the element in order to change the property of that element. if you are using css or javascript id cannot be repeated unlike class="para2" , class name can be repeated and name is the attribute of input . In selenium there are classes and methods to get these elements by passing this id,name and classname 

In selenium we can write it as :

for id : WebDriver.findElement(By.id("para1"))    , this will return a particular element.

for class : WebDriver.findElement(By.className("para2"))   , this will return all the elements which has the classname which would be a array of elements.

for nameWebDriver.findElement(By.name("text"))  , this will return the particular element .

for css : WebDriver.findElement(By.cssSelector("#para1 or . para2"))      ,  if you are giving #para1 it will return single element as like id , if you are giving .para2 this will return 2 element which is an array of elements.

for link : WebDriver.findElement(By.linkText(""))   , this will return the link which contains the text "link1"

for tagname : WebDriver.findElement(By.tagName("p"))   , this will return all the paragraph tag elements.

for patialLink : WebDriver.findElement(By.partialLinkText("link1"))  , this is same as the link but the difference is text contains a partial link.
   
for  xpath : WebDriver.findElement(By.xpath("/html/body/p"))    , this will return a element or array of elements . i will explain xpath in more in coming post.
 

how to create a sample selenium program

creating a sample selenium program is very simple you need to create a new project and add a class to new project and save. Now add the selenium library to your IDE Build path.

For example I am creating a sample programs for navigating to a webpage .

For Navigating to Web Page we need a method called "get" which lies inside a class WebDriver , This class is provided by the Selenium library .

We wont talk about TestNG because we don't need it now.

The test step would be

1. You need to fireup the web browser
2. navigate to webpage.

so the code will be like this.

package seleniumsampleproject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 *
 * @author pradeek 
 */
public class SeleniumSampleProject {
    public static WebDriver driver;
    public static void main(String[] args) {
        driver = new FirefoxDriver();
        driver.get("http://google.com");
    }    
}


Here we created a instance of WebDriver class as driver and copying the firefox object to driver instance then navigate using get method.

prerequisites for Selenium automation.

For starting selenium you need a IDE , you can choose any IDE you want like Netbeans , Eclipse and Jetbrains intellij but this is pay , evaluation period is there.

Next you need to get the selenium libraries , There is two types of libraries Selenium server and Selenium driver either of them do the same but Selenium driver is faster than Selenium Server. you can download either version.

you need another another library for controlling the test that is TestNG .


How automation frameworks works

As i said in the previous post , Every web automation tools helps you to identify the web-element and make a action on it . If you are familiar with javascript Dom Method it would be more easier to understand . In Js there are libraries for locating elements and make a event the specific elements for example

document.getElementById("demo").innerHTML = "Hello World!";

This is Js code for identifying an element whose id is "demo" and we are inserting an text "Hello world" . This id could be the id of a <p> tag or h1 tag etc . We can make an action on this element by various way like onClick(),onLoad(),onHover() etc. you can use TagName or className for identifying elements too in Js .

In order to understand this you need to understand HTML DOM .HTML GUI is a representation of html code in browser . So HTML is not a programming language but some kind of representation of code for the GUI Part.
Another kind of representation is the HTML DOM. Hierarchy of elements attached to each other is represents as parent node and child node.This can be draw as a tree structure. <HTML> tags comes the top element or parent of all elements and all other's are called child node.

Just like HTML dom element representation we can  represent HTML in XML too.it has also similar structure as like html . xml is also a representation of parent ans child nodes both HTML and XML represents in DOM  .


Selenium see web page as xml representation , so it can easily parse the nodes.
selenium has libraries which are in different languages who can talk with the browser libraries who drives the browser elements .

you can see how selenium works, Selenium

Selenium Web automation

Selenium is a set of libraries which help you to identify and make action on web elements in a  web page . This is the primary task of every web automation tools . A part from this there are so many libraries to handle several cases in web technology . Selenium wont provide you the complete solution for web automation but almost. 
 why we need web automation ?
 QA is the part of every software development cycle , During QA process we need to test each functionality from top bottom that the mandatory thing . Consider we have thousand functionalities , it wont be a problem to test from top to bottom in test cycle for first time . But where feature adding in each day we need to test the feature also make sure that it wont effect stable functionalities so we need to test the rest of the functionalities along with the newer one. This would be tedious task doing the repeated testing on old ones with newer ones. This is where automation come in to play, Software automation applied to on certain criteria .
  1. product should be stable, because you don't want to write and erase code when functionality changes.
  2. Automation is good for regression testing, new feature add.
  3. Situation where testing time is limited .
Advantage of automation Testing
  1. Avoid human errors, Testers could be passive on some functionality where they are confident.
  2.  Reduce Time by running the code.
How to choose a Automation tool ?
 Choosing a automation tool is little difficult , Identify your code developer in test team and identify their programming skills. Choose a language in which all developers are comfort of . Now you make a decision whether you want to use paid tool or use free tools . its really depends on you because its depends on the budget and duration of the test. If you are want to use paid tools , there are so many good tools like IBM RFTUFT by HP etc.

Advantage of Paid tools
  1.  Most of the paid tools provides solution for complete web testing from developing the code to the deployment .
  2. They provide customer service on tool issues.
  3. Developer's need not to worry about the integration of other tools because all option were in built in them. 
Disadvantage of Paid tools
  1. You need to pay for using the service.
  2. Its may not be open source, so you cannot be fully control on the tool.
  3. You always need to depends on the customer service for error fix in the tool.
On the other hand, There are free open  source tools like selenium. They are free to use. Selenium is the most popular open source free web testing tool available in the market . Originally Selenium developed by ThoughtWorks called Selenium standalone server it provides lots of services like grid testing. Later Google developed the same selenium which is called Selenium 2.0 or selenium driver.

Advantage of Selenium
  1. Free and Open source.
  2. It is free use and deliver. 
  3. Its support multiple language to code, You can code Java,C#,python,ruby,php,perl,javascript.
  4. You can modify the tool according to your choice because source code is available.
  5. It is easy to use.
  6. There are lots of lots of open source tool which is compatible with selenium .
 Disadvantage of Selenium
  1. Selenium is developed by community developers in open source, so there wont be individual service for the customer.
  2. Developer should be good programmers to know how to develop test code.
  3. Selenium is limited to certain technologies in web , so developers need find the work around for the specific issue. For example drag and drop in html5.
  4. It is easy to use but difficult to maintain. Selenium only provides to write code web testing , If you want to integrate other tools with selenium you  need to configure yourself.

5/13/2016

Python first program

Python can be executed either in command line or file program.

for command line execution you need to get in to python environment, for that need to open your terminal and type python, then you will get in to python environment.
 
We will print string "Hello World" using python, for that you need to type print "Hello World" . This will print Hello World 



 for exiting from python environment press ctrl+d in pc.

or you can execute through as file program for that , open you text editor and type print "Hello World"   and save it as filename.py . for example helloworld.py , for running you need to type python filename.py      and hit enter.

 

widget

selenium javascript alert box

This is a easy task to capture javascript based alertbox , /* * To change this license header, choose License Headers in Project Properti...