Wednesday, 20 July 2016

How to get hash code of the object:

How to get hash code of the object:
===========================
In java every object contains some unique number called hash code and it allocated to object by JVM.
In order to get hash code of the object ,Object (java. Lang package) class is having one method called hash Code().
Public int hashCode();
Whenever call hash Code() method we will get hash code of the particular object.

//Example on how to get hash code of the object
public class HashCode {

       @Test
       public void script() throws InterruptedException {
             
              WebDriver driver = new FirefoxDriver();
              driver.get("http://rameshselenium.blogspot.in/2016/02/fillo-api.html");
             
              //getting hashcode of the driver object
              int hashCode = driver.hashCode();
              System.out.println("hash code of the object is : " +hashCode());
              driver.quit();      
       }
}


Tuesday, 12 July 2016

Browser refresh in different ways using selenium webdriver

Browser refresh in different ways using selenium web driver:
==============================================
1 1.  Refresh command: Most commonly used and simple command for refreshing a webpage.

Public class RefreshGetCurrentUrl {

       Public static void main(String[] args) throws Interrupted Exception 
{
              WebDriver driver = new FirefoxDriver ();
              driver.manage ().window ().maximize ();
              driver.manage ().deleteAllCookies ();
              driver.manage ().timeouts ().implicitly Wait (30, TimeUnit.SECONDS);
              driver.get ("http://rameshselenium.blogspot.in/");
              Thread. Sleep (2000);
              driver. Navigate ().refresh ();
              Thread. Sleep (2000);
              driver. Quit ();

       }
}

2.SendKeys command: Second most commonly used command for refreshing a webpage. As it is using a send keys method, we must use this on any Text box on a webpage.

public class RefreshSendKeys {
       public static void main(String[] args) throws InterruptedException {
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
              driver.manage().deleteAllCookies();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://rameshselenium.blogspot.in/");
              Thread.sleep(2000);
              driver.findElement(By.id("ramesh")).sendKeys(Keys.F5);
               Thread.sleep(2000);
              driver.quit();

       }
}

3.    To command: This command is again using the same above concept. navigate( ).to( ) is feeding with a page url and an argument.

public class RefreshGetCurrentUrl {
       public static void main(String[] args) throws InterruptedException {
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
              driver.manage().deleteAllCookies();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://rameshselenium.blogspot.in/");
              Thread.sleep(2000);
       driver.navigate().to(driver.getCurrentUrl());
              Thread.sleep(2000);
              driver.quit();

       }


4.  Get command: This is a tricky one, as it is using another command as an argument to it. If you look carefully, it is just feeding get command with a page url.

public class RefreshGetCurrentUrl {
       public static void main(String[] args) throws InterruptedException {
              WebDriver driver = new FirefoxDriver();
              driver.manage().window().maximize();
              driver.manage().deleteAllCookies();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://rameshselenium.blogspot.in/");
              Thread.sleep(2000);
              driver.get(driver.getCurrentUrl());
              Thread.sleep(2000);
              driver.quit();

       }
}

Friday, 8 July 2016

How to get list of elements from drop down

How to get list of elements from drop down:
==================================


package com.rameshsoft.draganddrops;;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
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;
import org.openqa.selenium.support.ui.Select;

public class TotalElementsWebList {
public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.facebook.com");
WebElement element_day = driver.findElement(By.id("day"));
Select select = new Select(element_day);
// getting all elements from droip down
List<WebElement> totalWebListElements = select.getOptions();
// removing duplicate elements from list if any
HashSet<WebElement> hashSet = new HashSet<WebElement>(totalWebListElements);
// iterating the elements from collection object
Iterator<WebElement> iterator = totalWebListElements.iterator();
while (iterator.hasNext()) {
WebElement webElement = (WebElement) iterator.next();
if (webElement.getText().equalsIgnoreCase("15")) {
webElement.click();

}
}

}
}

Multiple ways of performing Drag and drop operations

1.Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
import java.util.List;
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;
import org.openqa.selenium.interactions.Actions;

public class DragDropOne {
public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver);
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();  
        driver.switchTo().frame(0);      
        WebElement source = driver.findElement(By.id("draggable"));      
        WebElement target = driver.findElement(By.id("droppable"));        
        actions.dragAndDrop(source, target).build().perform();    
        driver.quit();
     
}
}

2.Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
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;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropTwo {

public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver); 
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();       
        driver.switchTo().frame(0);
        WebElement source = driver.findElement(By.id("draggable"));       
        WebElement target = driver.findElement(By.id("droppable"));
        Actions builder = new Actions(driver);
        Action dragAndDrop = builder.clickAndHold(source).moveToElement(target).release(target).build();
        dragAndDrop.perform();      
        Thread.sleep(5000);
        driver.quit();
        
}
}

3.Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
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;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropThree {

public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver); 
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();       
        driver.switchTo().frame(0);
        WebElement source = driver.findElement(By.id("draggable"));       
        WebElement target = driver.findElement(By.id("droppable"));        
        Actions builder = new Actions(driver);
        Action dragAndDrop =  builder.clickAndHold(source).moveToElement(target, 2, 2).release(target).build();
        dragAndDrop.perform();      
        Thread.sleep(5000);
        driver.quit();
        
}
}

4. Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
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;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropFour {

public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver); 
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();       
        driver.switchTo().frame(0);
        WebElement source = driver.findElement(By.id("draggable"));       
        WebElement target = driver.findElement(By.id("droppable"));      
        Actions builder = new Actions(driver);
        builder.clickAndHold(source).moveToElement(target).perform();
        Thread.sleep(2000);
        builder.release(target).build().perform();      
        Thread.sleep(5000);
        driver.quit();
}
}

Wednesday, 6 July 2016

What is javascript executor?



Javascript Executor:
=============
javascriptexecutor is an interface. and which is available in org.openqa.selenium.JavascriptExecutor;
which provides mechanism to execute Javascript through selenium driver. It provides “executescript” & "executeAsyncScript" methods, to run JavaScript in the context of the currently selected frame or window. 
We can use Javascript to perform actions Using selenium Webdriver. By using ‘JavascriptExecutor’ we can perform these actions


Why we use it?
==========
To enhance the capabilities of the existing scripts by performing javascript injection into our application under test.


In simple words  “Javascript can be executed within the browser with the help of JavaScript Executor.”

Mousehover:
=========
WebElement element = driver.findElement(By.xpath("//a"));
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].onmouseover()", element);

Click():
=====
WebElement element = driver.findElement(By.id(“Email”));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(“arguments[0].click();”, element);

WebElement element = driver.findElement(By.className(“”));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(“arguments[0].click();”, element);


OR:
===
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.document.getElementById(‘Email).click()”);


How to refresh the browser window using Javascript:
===================================
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");

Get Title:
=======
driver.get("https://www.google.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript(“return document.title”);
System.out.println(“title : ” + title);
OUTPUT | title : Google

or
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText =  js.executeScript("return document.title;").toString();
Scroll bar operations:
  JavascriptExecutor js = (JavascriptExecutor)driver;
  //Vertical scroll - down by 50  pixels
  js.executeScript("window.scrollBy(0,50)");

Get Domain name:
============
driver.get(“https://www.google.com);
JavascriptExecutor js = (JavascriptExecutor) driver;
String domain = (String) js.executeScript(“return document.domain”);
System.out.println(“domain : ” + domain);
OUTPUT | domain :
http://www.google.co.in

Get URL:
======
driver.get(“http://google.co.in/&#8221;);
JavascriptExecutor js = (JavascriptExecutor) driver;
String URL = (String) js.executeScript(“return document.URL”);
System.out.println(“Full URL : ” + URL);
OUTPUT | Full URL :
https://www.google.co.in/

Get Attribute | Text:
==============
driver.get(“http://google.co.in/&#8221;);
Object exampleDiv = ((JavascriptExecutor) driver).executeScript(“return document.getElementById(‘main’);”);
String name = ((WebElement) exampleDiv).getAttribute(“class”);
System.out.println(name);
OUTPUT | content


Last Modified:
=========
driver.get(“http://google.co.in/&#8221;);
JavascriptExecutor js = (JavascriptExecutor) driver;
String lastModified = (String) js.executeScript(“return document.lastModified”);
System.out.println(“lastModified : ” + lastModified);
OUTPUT | lastModified : 01/29/2014 14:56:46





Tuesday, 15 March 2016

How to take screenshots@remote in selenium

How to take screenshots@remote in selenium :
===================================
package com.rameshselenium.remotescreenshot
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Testing {
   
    public void myTest() throws Exception {
        WebDriver driver = new RemoteWebDriver(
                                new URL("http://localhost:4444/wd/hub"),
                                DesiredCapabilities.firefox());
       
        driver.get("http://rameshselenium.blogspot.in/");
       
        // RemoteWebDriver does not implement the TakesScreenshot class
        // if the driver does have the Capabilities to take a screenshot
        // then Augmenter will add the TakesScreenshot methods to the instance
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File screenshot = ((TakesScreenshot)augmentedDriver).
                            getScreenshotAs(OutputType.FILE);
    }
}

How to take screenshots on failure in selenium webdriver

How to take screenshots on failure in selenium webdriver:
============================================
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import org.apache.commons.io.FileUtils;

import org.testng.annotations.Test;         

public class webDriverTakeScreenshot {
   public WebDriver driver;               

   @Test
   public void runDummyTest() throws Exception {    
      driver = new FirefoxDriver();
      driver.get("http://rameshselenium.blogspot.in/");

      try {               
         driver.findElement(By.id("")).click();
      }
      catch(Exception e) {    
         System.out.println(&quot;Element Not Found&quot;);   
         takeScreenshot();     
      }     
   }

   public void takeScreenshot() throws Exception {             
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(scrFile, new File(C:\\test\\failed-test.png));     
   }
}