Sunday 28 February 2016

Angular JS Protractor Installation process - Tutorial Part 1


                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed specifically for Angular JS web applications. It was introduced during Angular JS 1.2 as a replacement of the existing E2E testing framework. The Protractor automation tool is also recommended by Angular JS for scenario testing

 Features of the Protractor:
  1. Built on the top of Web driver JS and Selenium server
  2. Introduced new simple syntax to write tests
  3. Allows running tests targeting remote addresses
  4. Can take advantage of Selenium grid to run multiple browsers at once
  5. Can use Jasmine or Mocha to write test suites
 Protractor is a wrapper (built on the top) around Selenium Web Driver, so it contains every feature that is available in the Selenium Web Driver. Additionally, Protractor provides some new locator strategies and functions which are very helpful to automate the Angular JS application. Examples include things like: waitForAngular, By.binding, By.repeater, By.textarea, By.model, WebElement.all, WebElement.evaluate, etc.


Prerequisites

1. Node Js :

 Protractor is a Node.js program. To run Protractor, you will need to have Node.js installed
 https://nodejs.org/en/

 Verify Installation
To verify your installation, please type in the command
  npm --version

2. Installing Protractor :

For this project, the Protractor framework is being used and configured on a Windows environment. Below are the steps for installation:
  
Open the command prompt and type in the following command to install protractor globally.
       
npm install –g protractor
Install Protractor Locally
You can install protractor locally in your project directory. Go to your project directory and type in the following command in the command prompt:
   npm install protractor
Verify Installation
To verify your installation, please type in the command
    Protractor --version
If Protractor is installed successfully then the system will display the installed version. Otherwise you will have to recheck the installation.

3. Installing JDK :

 To run Protractor, you will need to have Node.js installed
http://www.oracle.com/technetwork/java/javase/downloads/

Verify Installation
To verify your installation, please type in the command
 java -version 

4. Setting Up the Selenium Server :

The webdriver-manager is a helper tool to easily get an instance of a Selenium Server running. Use it to download the necessary binaries with:
webdriver-manager update
Now start up a server with:  
webdriver-manager start 
This will start up a Selenium Server and will output a bunch of info logs. Your Protractor test will send requests to this server to control
a local browser. You can see information about the status of the server at
http://localhost:4444/wd/hub.
       
Now By default your node should be installed in your user directory and open up "Cmd"

Example :
C:\Users\BrahmiP\AppData\Roaming\npm\node_modules\protractor\example>protractor
 conf.js




Tuesday 4 August 2015

What is the alternate way to send text in textbox of webpage with out using sendKeys() method ?

Note- Use Javascript to send text.
syntax-

((JavascriptExecutor)driver).executeScript("document.getElementById('attribute value of id').value='text which you want to pass'");

ex-
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WithoutSendKeys {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/ServiceLogin?sacu=1&scc=1...");
((JavascriptExecutor)driver).executeScript("document.getElementById('Email').value='sanjay'");
}
}

How to check whether the selectbox is singleListbox or multipleListBox.


Script Code-

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

public class MultipleListBox {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("C:\\selenium\\Selenium_web.html"); 
/*here the url is my personal page, you can try for any url which has such scenario or you can develop your own html page having the above html code.*/
        System.out.println(driver.findElement(By.xpath("//select[@id='mdd']")).getAttribute("multiple")); // this will print true, which means it is multipleListBox
        System.out.println(driver.findElement(By.xpath("//select[@id='sdd']")).getAttribute("multiple"));//

// this will print null, which means it is singleListBox       
    }
}

How do I identify and click on hidden element present on web page?

 HTML :
------------------
 
<input id="reporting" class="main" type="checkbox" title="reporting" onclick="enableRadioButtons('reporting', 'reportingHidden', 'reportingCheck')"/>Reporting</td>


 SELENIUM CODE:

----------------------
JavaScriptExecutor js= javaScriptExecutor(driver);
js.executescript("onclick="enableRadioButtons('reporting', 'reportingHidden', 'reportingCheck')");

Print the name of friends with the status like one is online, busy, idle or offline in gmail chat.

Note- Please give the gmail id and password while runtime. (after pressing ctrl+f11).

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GmailOnlinePeople {

public static void main(String[] args) {
    WebDriver driver;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the gmail id: ");
    String emailId = in.next();
    System.out.println("Enter the pass: ");
    String pass = in.next();
 
    driver = new FirefoxDriver(); //open firefox browser
 
    //login to gmail
    driver.get("http://www.gmail.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
    driver.findElement(By.name("Email")).sendKeys(emailId);
    driver.findElement(By.name("Passwd")).sendKeys(pass);
    driver.findElement(By.name("signIn")).click();
    String name="";
    //friends with available status
    try {
        List<WebElement> available = driver.findElements(By.xpath("//tr[td[img[contains(@alt,'Available')]]]//td[2]/span[1]"));
        System.out.println("number of friends with available status in the gmail chat: "+available.size());
        if(available.size()!=0){
            System.out.println("Name of the friends with Available status: ");
        }
        for (int i=0; i <available.size(); i++)
        {
            name = available.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is there with available status.");
    }
   
  //friends with busy status in the gmail chat
    try {
        List<WebElement> busy = driver.findElements(By.xpath("//tr[td[img[@alt='Busy']]]//td[2]/span[1]"));
        System.out.println("number of friends with busy status in the gmail chat: "+busy.size());
        if(busy.size()!=0){
            System.out.println("Name of the friends with busy status: ");
        }
        for (int i=0; i <busy.size(); i++)
        {
            name = busy.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is with busy status.");
    }
   
  //friends with idle status
    try {
        List<WebElement> idle = driver.findElements(By.xpath("//tr[td[img[@alt='Idle']]]//td[2]/span[1]"));
        System.out.println("number of friends with idle status in the gmail chat: "+idle.size());
        if(idle.size()!=0){
            System.out.println("Name of the friends with idle status: ");
        }
        for (int i=0; i <idle.size(); i++)
        {
            name = idle.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is with idle status.");
    }
   
  //friends with offline status
    try {
        List<WebElement> offline = driver.findElements(By.xpath("//tr[td[img[@alt='Offline']]]//td[2]/span[1]"));
        System.out.println("number of friends offline in the gmail chat: "+offline.size());
        if(offline.size()!=0){
            System.out.println("Name of the friends offline: ");
        }
        for (int i=0; i <offline.size(); i++)
        {
            name = offline.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is offline.");
    }
    driver.close();
}
}

Wednesday 22 July 2015

How to make the web driver to wait for page to refresh before executing another test

code:
 
public void waitForPageLoaded(WebDriver driver) {

     ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
          return ((JavascriptExecutor)driver).executeScript
            ("return document.readyState").equals("complete");
        }
      };

     Wait<WebDriver> wait = new WebDriverWait(driver,30);
      try {
              wait.until(expectation);
      } catch(Throwable error) {
              assertFalse("Timeout waiting for
       Page Load Request to complete.",true);
      }
 } 

Friday 19 June 2015

Difference Between get() and navigate() in Selenium

Get v/s Navigate :-
"navigate().to()" and "get()" do exactly the same thing. Only thing is that incase of "get" selenium would wait for the page to fully load before executing the next line of code.

Also "navigate" interface further exposes the ability to move backwards and forwards in your browser's history.

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...