Tuesday, 7 January 2014

exceptions in selenium and code to resolve it

Name 5 different exceptions you had in selenium web driver and mention .What instance you got it and how do you resolve it?
·           WebDriverException
·           NoAlertPresentException
·           NoSuchWindowException
·           NoSuchElementException
·           TimeoutException
 • WebDriverException
 WebDriver Exception comes when we try to perform any action on the non-existing
driver.
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.close();
driver.quit();

• NoAlertPresentException
When we try to perform an action i.e., either accept() or dismiss() which is not required
at a required place; gives us this exception.
try{
driver.switchTo().alert().accept();
}
catch (NoAlertPresentException E){
E.printStackTrace();
}
• NoSuchWindowException
 When we try to switch to an window which is not present gives us this exception:
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.switchTo().window("Yup_Fail");
driver.close();
In the above snippet, line 3 throws us an exception, as we are trying to switch to an
window that is not present.• NoSuchFrameException
• Similar to Window exception, Frame exception mainly comes during switching between
the frames.
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.switchTo().frame("F_fail");
driver.close();
In the above snippet, line 3 throws us an exception, as we are trying to switch to an
frame that is not present.
• NoSuchElementException
 This exception is thrown when we WebDriver doesn’t find the web-element in the DOM.
WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.findElement(By.name("fake")).click();
• TimeoutException
 Thrown when a command does not complete in enough time.
All the above exceptions were handled using try catch exceptions.

  

How to ZIP files in Selenium




// Sample Function to make zip of reports
public static void zip(String filepath){
try
{
File inputFolder=new File('Mention file path her");
File outputFolder=new File("Reports.zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFolder)));
BufferedInputStream in = null;
byte[] data  = new byte[1000];
String files[] = inputFolder.list();
for (int j=0; j<files.length; i++)
{
in = new BufferedInputStream(new FileInputStream
(inputFolder.getPath() + "/" + files[j]), 1000);
out.putNextEntry(new ZipEntry(files[j]));
int totalcount;
while((totalcount= in.read(data,0,1000)) != -1)
{
out.write(data, 0, totalcount);
}
out.closeEntry();
  }
  out.flush();
  out.close();
}
  catch(Exception e)
  {
 e.printStackTrace();
           return "Fail - " + e.getMessage();
  }
 }


Back to Selenium Interview questions and Answers

set the execution order for TestNg Testcases.

In TestNG I have some test's  Test1-Test2-
Test3-Test4-Test5I want to run my execution 
order is Test5-Test1-Test3-Test2-Test4.How 
do you set the execution order can you explain 
for that?

package programs;import org.testng.annotations.Test;public class testngexecution { @Test(priority=2)public void test1(){System.out.print("Inside Test1"); }@Test(priority=4)public void test2(){System.out.print("Inside Test2"); }@Test(priority=3)public void test3(){System.out.print("Inside Test3"); }@Test(priority=5)public void test4(){System.out.print("Inside Test4"); }@Test(priority=1)public void test5(){System.out.print("Inside Test5"); }


Back to Selenium Interview Questions and Answers

How to take the screen shots in seelnium 2.0



// store screenshots
public static void captureScreenShot(String filePath) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
   try {
FileUtils.copyFile(scrFile, new File(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
   }

Back to Interview Questions and Answers

Difference between find element () and findelements ()

Difference between find element () and find elements ()?
findElement() :
Find the first element within the current page using the given "locating mechanism".
Returns a single WebElement.
Syntax: WebElement findElement(By by)
Ex:
driver.get("http://ruchi-myseleniumblog.blogspot.in/"); 
WebElement widget = driver.findElement(By .xpath(".//*[@id='BlogArchive1_ArchiveList']"));
widget.click();
findElements() :
Find all elements within the current page using the given "locating mechanism".
Returns List of WebElements.
Syntax: 
        WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();

System.out.println(post);
}

Sample Program:

package programs;

import java.util.List;
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.Test;

public class Sample {
WebDriver driver;

@Test
public void test() {

driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in/");
WebElement widget = driver.findElement(By
.xpath(".//*[@id='BlogArchive1_ArchiveList']"));
WebElement hierarchy = widget.findElement(By.className("hierarchy"));
System.out.println("Year is "
+ hierarchy.findElement(By.className("post-count-link"))
.getText());
System.out.println("Year-Posts size  is "
+ hierarchy.findElement(By.className("post-count")).getText());
WebElement sublist = hierarchy.findElement(By.className("hierarchy"));
System.out.println("Month is "
+ sublist.findElement(By.className("post-count-link"))
.getText());
System.out.println("Month-Posts size  is "
+ sublist.findElement(By.className("post-count")).getText());
WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();

System.out.println(post);
}
driver.close();
}


}

What is actions class in web driver

71. What is actions class in web driver?
Actions class with web Driver help is Sliding element, Resizing an Element, Drag & Drop
Hovering a mouse, especially in a case when dealing with mouse over menus.

Dragging & Dropping an Element:
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 testDragandDrop {
  public static void main(String[] args) throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
  driver.get("http://jqueryui.com/resources/demos/droppable/default.html"); 
  WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
  WebElement droppable = driver.findElement(By.xpath("//*[@id='droppable']"));   
  Actions action = new Actions(driver); 
action.dragAndDrop(draggable, droppable).perform();
  }
}
Sliding an Element:
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 testSlider {
  /**
  * @param args
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
  driver.get("http://jqueryui.com/resources/demos/slider/default.html"); 
  WebElement slider = driver.findElement(By.xpath("//*[@id='slider']/a")); 
  Actions action = new Actions(driver);
  Thread.sleep(3000);
  action.dragAndDropBy(slider, 90, 0).perform();
  }
}
Re-sizing an Element:
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 testResizable {
  public static void main(String[] args) throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
  driver.get("http://jqueryui.com/resources/demos/resizable/default.html");
    WebElement resize = driver.findElement(By.xpath("//*[@id='resizable']/div[3]")); 
  Actions action = new Actions(driver);
  action.dragAndDropBy(resize, 400, 200).perform();
    }
}



How to switch between frames

70. How to switch between frames?

WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:
·         A number.
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index "0", the second at index "1" and the third at index "2". Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.
·         A name or ID.
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Select a frame using its previously located WebElement.
Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.