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.

Object Repository in Selenium

Unlike QTP/UFT, Selenium does not offer the default implementation for  object repository. In QTP things are really straightforward, just object spy the controls and add to object repository, and further with the blessings of intellisense feature in its IDE, utilize them easily in writing scripts.

But how we can achieve the same in selenium??..
It can be done by using Properties file feature of Java. Lets begin with some basic explanation.

What is Object Repository?

Object Repository is a centralized location where we can store objects information, it acts as interface between Test script and application in order to identify the objects during the execution.

We always recommend using external file for object repository rather than hard coding the objects and its properties directly into our code. Why this??? As it reduces the maintenance  effort and provides positive ROI, for example say any of the object properties change within our application under test, we can easily change it in external object repository file, rather than searching and doing updates  for that object individually in the code.

Principle:-
A basic object repository can be implemented as a collection of key-value pairs, with the key being a logical name identifying the object and the value containing unique objects properties used to identify the object on a screen. For this, we will use a .properties file in Java which is a basic collection of key-value pairs.

Creating Properties File in Eclipse:-

Step1:-
Right click on the Package in the solution Explorer of Eclipse-> New ->Other


Creating Properties file in Eclipse
Adding New File in Eclipse

Or 
Right click on the Package in the solution Explorer of Eclipse-> New ->File (In this case Step-2 below is not required)


Create new file in eclipse
New ->File in Eclipse

Step2:-
General ->File ->Click Next


New file wizard in Eclipse
New Wizard in Eclipse
Step3:-
Give name to the file with .properties extension (Say OR_Gmail_Login.properties) ->Click Finish
Properties file in Java Eclipse
Naming Properties File in Eclipse


Note:- 
We generally create each properties file for every single page and capture all the UI elements present on the page and use it as per the needs.


Adding Key/value Pairs in Properties File:-

Before adding our objects into object repository, let’s outline a simple scenario that we will be automating in Selenium:-

1. Launch Gmail login page.
2. Fill the Username & Password fields and click on Submit button

After creating our empty properties file, now we need to add our elements, in the form of key and Value pairs.
For Example:-

Gmail.LoginPage.txtPassword -> Key
Passwd->Value

Note: - In Value property we have taken the Locator and its value, which will be used to identify our control. As password field, is getting uniquely identified by ID field so we have taken that value.


Properties file with objects in Selenium
Properties File in Java



Code:-
package OR;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

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

public class PropertiesFile {
  public static void main(String[] args)  {
   //Creating the File Object
   File file = new File("D:\\Automation\\Selenium\\MyCode\\FirstSeleniumCode\\src\\OR\\OR_Gmail_Login.properties");
   //Creating properties object
   Properties prop = new Properties();
  //Creating InputStream object to read data
   FileInputStream objInput = null;
   try {
    objInput = new FileInputStream(file);
    //Reading properties key/values in file
    prop.load(objInput);
    //Closing the InputStream
    objInput.close();
    } catch (FileNotFoundException e) {
     System.out.println(e.getMessage());   
     
    } catch (IOException e) {
   System.out.println(e.getMessage());
  }
   //Creating the driver instance
   WebDriver driver = new FirefoxDriver();
   //Adding wait
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
   //Launching the GMAIL page
   driver.get(prop.getProperty("Gmail.URL"));
   //Entering the UserName
   WebElement element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.txtUserName")));
   element.sendKeys("uftHelp@gmail.com");
   //Entering the Password
   element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.txtPassword")));
   element.sendKeys("uftHelp");
   //Clicking the SignIn button
   element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.btnSignIn")));
   element.click();
   System.out.println("Test Scenario Completed!!");
   //Destroying the driver object
   //driver.close();

  }
}

Thursday 5 March 2015

Resizing a web element using movebyoffs

Genarally when ever we want to change the size of an element we do with the help of mouse manually. Now we will see to resize / change the size of an element using webdriver actions class with moveByOffset which moves the mouse position from its current position by the given offset.
Below are the simple steps thats needs to be followed for the below example:-
Step 1: Open the URL
Step 2: Wait for the element that you want to resize. (Make sure if there are any frames then we need to shift to the frame and then perform operation).
Step 3: We will define a method using which we need to pass web element and the coordinates to the method.

Angular JS Protractor Installation process - Tutorial Part 1

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