March 26, 2011

GoBack Simulation

How to Simulate the user clicking the "back" button on their browser??

Here is the code for that..

package newone;




import org.junit.*;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;


public class Goback extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@Before
public void setUp() throws Exception {

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

}

@Test
public void testGoback() throws Exception
{
selenium.open("http://www.google.co.in");
selenium.windowMaximize();
assertEquals("Google", selenium.getTitle());
Thread.sleep(1000);
selenium.type("q", "testing");
Thread.sleep(1000);
//Simulates a user pressing and releasing a key (13 if for Enter button).
selenium.keyPress("btnG", "\\13");
Thread.sleep(1000);
/*goBackAndWait()
Generated from goBack()
   Simulates the user clicking the "back" button on their browser*/
selenium.goBack();
Thread.sleep(1000);
assertEquals("Google", selenium.getTitle());
Thread.sleep(1000);
}
@After
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}

}


Hope it will help you in simulating back button on browser....

No comments:

Post a Comment