I had a requirement where I had to take screenshots of different URLs on different browsers. Instead of taking it manually everytime, thought of taking it programatically using Selenium. Here is the program which takes the screenshot of a webpage using selenium:
ScreeshotTest.java
SeleniumServerControl.java
ScreeshotTest.java
import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.Selenium; import junit.framework.TestCase; public class ScreeshotTest extends TestCase{ private Selenium browser; private static int seleniumServerPort = 2001; private static SeleniumServerControl seleniumServerControl = null; private final static String URL = "http://www.vishnuagrawal.blogspot.com"; public void setUp() { seleniumServerControl = SeleniumServerControl.getInstance(); seleniumServerControl.startSeleniumServer(seleniumServerPort); browser = new DefaultSelenium("localhost", seleniumServerPort, "*firefox", URL); } public void tearDown() { browser.stop(); seleniumServerControl.stopSeleniumServer(); } public void testFlashApp() throws Exception{ browser.start(); browser.setTimeout("120000"); browser.open(URL); browser.captureEntirePageScreenshot("D:\\ss.png", ""); } }
SeleniumServerControl.java
import org.openqa.selenium.server.RemoteControlConfiguration; import org.openqa.selenium.server.SeleniumServer; public class SeleniumServerControl { private static final SeleniumServerControl instance = new SeleniumServerControl(); private SeleniumServer server = null; public static SeleniumServerControl getInstance() { return instance; } protected SeleniumServerControl() { } public void startSeleniumServer() { startSeleniumServer(RemoteControlConfiguration.DEFAULT_PORT); } public void startSeleniumServer(int port) { if (server == null) { try { RemoteControlConfiguration settings = new RemoteControlConfiguration(); //File f = new File("/home/user/.mozilla/firefox/default"); //settings.setFirefoxProfileTemplate(f); //settings.setReuseBrowserSessions(true); settings.setSingleWindow(true); settings.setPort(port); server = new SeleniumServer(settings); System.out.println(" selenium server " + server.toString()); } catch (Exception e) { System.err.println("Could not create Selenium Server because of: " + e.getMessage()); e.printStackTrace(); } } try { server.start(); } catch (Exception e) { System.err.println("Could not start Selenium Server because of: " + e.getMessage()); e.printStackTrace(); } } public void stopSeleniumServer() { if (server != null) { try { server.stop(); server = null; } catch (Exception e) { System.err.println("Could not stop Selenium Server because of: " + e.getMessage()); e.printStackTrace(); } } } }
No comments:
Post a Comment