Like us on Facebook

LightBlog

April 29, 2011

Capture screenshot on Failure

April 29, 2011 2
Capture screenshot....
How to capture screenshot?
How to capture screenshot on Failure?

These are the frequent questions.................It will be good if we capture screenshot if our test fails...
We need to use try and catch block..if try block catches the exception..it will execute the capture screenshot command which is written in catch block..


Below is the sample code how to capture screenshot on failure...........

--------------------------------------------------------------------------
import org.junit.*;


import org.openqa.selenium.server.*;

import com.thoughtworks.selenium.*;

public class Screenshot extends SeleneseTestCase{

public Selenium selenium;

public SeleniumServer seleniumserver;


@BeforeClass
public void setUp() throws Exception {
seleniumserver = new SeleniumServer();
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();
}
@Test
public void testScreenshot() throws Exception
{

try {
selenium.open("http://in.yahoo.com//?p=us");
selenium.windowMaximize();
selenium.click("//li[@id='pa-u_14782488-bd']/a/span[2]");
selenium.waitForPageToLoad("30000");
selenium.type("username", "test");
Thread.sleep(1000);
//the element name for password field is "passwd" But here i changed it to "passd" which is wrong.
//Selenium will capture screen shot after the error
selenium.type("passd", "123");
selenium.click(".save");
selenium.waitForPageToLoad("30000");
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
selenium.captureScreenshot("C:\\test\\erro.png");
}

}


@AfterClass
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}
}

----------------------------------------------------------

Hope this post will be helpful ...................


The screenshot will look like this...



Screenshot....



April 28, 2011

Enter data while script is running --Good example FOR loop and IF condition

April 28, 2011 2
Enter data while script is running --Good example FOR loop and IF condition
Below script is good example for

1. How to enter data from key board while script is running..
2. Good example how to use FOR loop
3. How to use IF condition..


package test;

import javax.swing.JOptionPane;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.*;



public class Bidtest {
public Selenium selenium;
public SeleniumServer seleniumserver;

@Before
public void setUp() throws Exception {

seleniumserver = new SeleniumServer();
selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testBidtest() throws Exception {

selenium.open("http://www.jeetle.in/");
selenium.windowMaximize();
selenium.click("link=Login");
selenium.waitForPageToLoad("30000");
selenium.type("edit-name", "Jyotish Varma Vegesna");
selenium.type("edit-pass", "jyotish44");
selenium.click("op");
selenium.waitForPageToLoad("30000");
selenium.click("//div[8]/div[1]/a/img");
selenium.waitForPageToLoad("30000");
// a text box will appear For entering data
String bidnum=JOptionPane.showInputDialog("Enter number of time u want to click Bid more");

int bidnum1=Integer.parseInt(bidnum);

for (int i = 1; i <= bidnum1; i++)
{
selenium.click("//img[@onclick='javascript:addMore1(1000)']");
}

// a text box will appear For entering data
String number=JOptionPane.showInputDialog("Enter number of rows you want to enter");

int num1=Integer.parseInt(number);
int  amount1 =0,amount2=0,amount3 =0;
for(int i = 1; i < num1; i++) {
amount3++;
if(amount3==10) {
amount2++;amount3=0;
if(amount2==10){
amount1++;amount2=0;
}
}
selenium.type("bid_amount1["+i+"]", ""+amount1);
selenium.type("bid_amount2["+i+"]", ""+amount2+amount3);
}

Thread.sleep(5000);
selenium.click("link=Logout");
selenium.waitForPageToLoad("30000");
}

@After
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}