Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts

Friday, June 8, 2012

Selenium: Testing auto complete fields

Selenium's "type" command does not trigger auto complete feature to give suggestion. We can use "typeKeys" method to simulate auto complete. Here is the sample code for this:
public static void inputTextInAutocompleteField(String testField, String testValue) 
{
        try {
                selenium.type(testField, testValue);
                selenium.typeKeys(testField," \b") ;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Here we are first entering value in the text field, then using selenium's 'typeKeys' method to press 'space' character and then 'backspace' (\b) character.

Selenium: How to accept SSL certificates

When you run your selenium tests on any HTTPS url, things go wrong. We get certificate exception and test doesn't proceed. In firefox, the workaround for this problem is to create your own Firefox profile, with the specific certificate added on it manually and then start Selenium server with that firefox profile, but for Internet Explorer there is no such workaround.

However, we can resolve this issue by installing CyberVillains Certificate on Windows. We need to follow below steps to work Selenium with SSL.

1. Start selenium server with "-trustAllSSLCertificates" command
2. Use "*firefoxproxy" instead of "firefox" OR "*iexploreproxy" instead of "iexplore"
3. Install CyberVillains Certificate (selenium bundles a special certification authority (CA) certificate that is used to trust all the other SSL certificates.)

Installing CyberVillains Certificate
---------------------------------------
1. Extract selenium jar into a folder
2. Goto sslSupport diretory
3. Double click on cybervillainsCA.cer




Thursday, April 19, 2012

Selenium: Check if Selenium Server is running

If you want to check whether selenium server is running or not, hit the url "http://host:port/selenium-server/driver/?cmd=testComplete", If response is returned that means, server is running on the given host:port. Here is the Java code for this:


import java.net.HttpURLConnection;
import java.net.URL;

public static boolean isSeleniumServerRunning(String host, int port) 
  {
   try {
    String baseUrl = "http://" + host + ":" + port;
    System.out.println("Checking selenium server status [" + baseUrl + "]");
    URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
    if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
     return true;
  } catch (Exception e) {
   System.err.println("Could not check selenium server status: " + e.getMessage());
   e.printStackTrace();
  }   
  return false;
  }

Wednesday, December 14, 2011

Selenium: Taking screenshot of a webpage

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

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();
      }
    }
  }
}