Tuesday, 9 February 2016

Find the output for the following program

Find the output for the following program:
==================================
package blog;

public class IfElseDemo {

public static void main(String[] args) {

int a=10,b=20;

if (a=b) 
{
System.out.println("a is bigger");
System.out.println("value of a is : " +a);
System.out.println("value of b is : " +b);
}
else
{
System.out.println("b is smaller");
}

}

}

1.   Compile Time Error
2.   Null PointerException
3.   NoSuchElementException
4.   None of the above


ANSWER : Compile time error.

Identify the following web elements

Q)Identify the following element by using cssselector:
==========================================

<input id='1' class='abc' type='value'/>

1.using class
   webDriver.findElement(By.cssSelector(".abc"));

2.using id
   webDriver.findElement(By.cssSelector("#1"));

3.cssselector
   webDriver.findElement(By.cssSelector("input[id='1']"));


Q)Identify the following element

<div - -  >
  <td id=1>selenium</td>
  <td>selenium</td>
  <td>selenium</td>--------->identify this element
  <td>selenium</td>

xpath:
------------
//td[@id='1']/following-sibling[2]

Q) <div id="identifier-shown">
     <input >
     <input >------>identify this element

xpath:
----------
//div[@id='identifier-shown']/input[2]

How to identify a webelement if that doesnt have any attributes

How to identify a web element if that doesn't have any attributes:
==================================================

Even though if the web element doesn't have any attributes still we can identify the web element.

Let us consider a sample example


Q) <div id="identifier-shown">
     <input >
     <input >------>identify this element

xpath:
---------

//div[@id='id="identifier-shown']/input[2]

How to set browser width and height in Selenium Webdriver

How to set browser width and height in Selenium Webdriver:
===============================================
When ever based on your requirement if we want we can change the width and height of the browser in selenium.

Selenium provided one predefined class called Dimension,by using class we can set the width and height of the browser

Syntax:
----------
Dimension dimension = new Dimension(int width, int height);
Dimension dimension = new Dimension(500, 800);

//eaxmple on how to set width and height of the browser in selenium
------------------------------------------------------------------------------------------
package blog;

import java.util.concurrent.TimeUnit;

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

public class SetWidthDemo {

public static void main(String[] args) {

WebDriver webDriver = new FirefoxDriver();

webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

webDriver.manage().deleteAllCookies();

webDriver.navigate().to("http://google.co.in");

webDriver.manage().window().getSize();

System.out.println(webDriver.manage().window().getSize());

Dimension dimension = new Dimension(500,800);

webDriver.manage().window().setSize(dimension);

webDriver.quit();

}

}

Q)Selenium : Find the output for the following program

Q)Selenium : Find the output for the following program:
============================================
package blog;
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;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class DoubleClickDemo {

WebDriver driver;
Actions actions;
String expectedTitle = "jQuery UI";

@BeforeSuite
public void openBrowser() {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@AfterSuite
public void closeBrowser() {
if (driver!=null) {
driver.quit();
}

}
@Test
public void testScript() {
driver.get("https://www.jqueryui.com");

String actualTitle = driver.getTitle();
if (actualTitle.equalsIgnoreCase(expectedTitle))
{

WebElement element = driver.findElement(By.linkText("Autocomplete"));
Actions actions = new Actions(driver);
actions.doubleClick(element).click().build().perform();
}

}

}

OUTPUT:
--------------
1.   Compile Time Error
2.   Null PointerException
3.   NoSuchElementException
4.   None of the above


ANSWER :  Null PointerException



How to perform double click operation in selenium webdriver

How to perform double click operation in selenium webdriver:
==============================================
By using Actions class we can perform double click operations.
This actions class is having one non static method called doubleClick() and this method always ask you tthe webelement on which you want to perform the double click operation.

public Actions doubleClick(WebElement element);
-----------------------------------------------------------------

//sample example on how to perform doubleClick
-------------------------------------------------------------
package blog;

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;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class DoubleClickDemo {

WebDriver driver;
Actions actions;

String expectedTitle = "jQuery UI";

@BeforeSuite
public void openBrowser() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@AfterSuite
public void closeBrowser() {
if (driver!=null) {
driver.quit();
}

}
@Test
public void testScript() {
driver.get("https://www.jqueryui.com");

String actualTitle = driver.getTitle();
if (actualTitle.equalsIgnoreCase(expectedTitle))
{

WebElement element = driver.findElement(By.linkText("Autocomplete"));
actions = new Actions(driver);

actions.doubleClick(element).click().build().perform();


}

}

}

How to find iframes and their attributes with out seeing html code

How to find iframes and their attributes with out seeing html code:
===================================================

package blog;

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.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class FrameAttributesDemo {

WebDriver driver;

String expectedTitle = "jQuery UI";

@BeforeSuite
public void openBrowser() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@AfterSuite
public void closeBrowser() {
if (driver!=null) {
driver.quit();
}

}
@Test
public void testScript() {
driver.get("https://www.jqueryui.com");

String actualTitle = driver.getTitle();
if (actualTitle.equalsIgnoreCase(expectedTitle))
{

driver.findElement(By.linkText("Autocomplete")).click();
}

java.util.List<WebElement> iframes = driver.findElements(By.tagName("iframe"));

for (WebElement webElement : iframes) {

String name   =  webElement.getAttribute("name");
String id     =  webElement.getAttribute("id");
String clas   =  webElement.getAttribute("class");

System.out.println("iframe name is : " +name);
System.out.println("iframe id is : " +id);
System.out.println("iframe clas is : " +clas);

}
}
}