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;
}
Here are the useful trick for GNU screen: 1. Give access to other users on the screen:
- Let say screen "test" is running on host 15.25.125.148 with user "xyz"
- run command "Ctrl a :"
- type "multiuser on" to enable multiuser mode
- type "acladd <unix username>" (to whom you want to give access)
- type "aclchg <args>" to change permissions on the screen for that user
- Now the user will login on that host via command "ssh user@15.25.125.148"
- attach to screen with command "screen -x xyz/test"
2. Changing session name of the running screen
The default session name created by the screen command is constructed from the tty and host names, which isn't very intuitive. To change this session name, run this command on your screen session
Ctrl a :sessionname newSessionName
3. Send a command to a window in a running screen session from the commandline
screen -x <screen session name> -p <window number or name> -X stuff '<command>\012'
4. Create screen session with commands running
- First, create .screenrc_test with following contents where you specify your tabbed sessions and commands
startup_message off
vbell off
caption always "%{= bb}%{+b w}%n %h %=%t %c"
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"
activity "Activity in %t(%n)"
deflogin on
shell -/bin/bash
screen -t TAB1
stuff "cd $HOME && ls -1rt | tail -1 | xargs tail -f \012"
screen -t TAB2
stuff "cd $HOME && top \012"
In web applications, buttons which are rendered by GWT (google-web-toolkit) cannot be clicked. Selenium RC doesn't recognize them as HTML button. so following will not work for those type of buttons/element
selenium.click(elementLocator)
After some googling, I found out that we can click on these elements by native key events with follwoing code.
public static void clickOnGWTPopElement(String elementLocator) {
try{
//normal click doesn't work on GWT popup elements so we need to perform native mouse operations
selenium.mouseOver(elementLocator);
selenium.mouseDown(elementLocator);
selenium.mouseUp(elementLocator);
}catch (Exception e) {
e.printStackTrace();
}
}
IN my previous post, I posted code for reading excel file using Apache POI library. In this post I'll cover, how can we write data into excel. Here is the code:
package com.qa.test;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class SpreadSheetWriter {
/** This method writes data to new excel file **/
public static void writeDataToExcelFile(List> data, String fileName)
{
try{
HSSFWorkbook myWorkBook = new HSSFWorkbook();
HSSFSheet mySheet = myWorkBook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
//Create header row
createHeaderRow(mySheet);
int rowNum = 1;
Iterator> iter = data.iterator();
while(iter.hasNext())
{
myRow = mySheet.createRow(rowNum++);
int cellNum = 0;
List key = iter.next();
for(String values: key)
{
myCell = myRow.createCell(cellNum++);
myCell.setCellValue(values);
}
}
FileOutputStream out = new FileOutputStream(fileName);
myWorkBook.write(out);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
If you want to read Excel file in Java, Apache POI is the library which supports to read/write microsoft documents. Here is the code to read an Excel file.
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
public class ReadExcel {
public static void main(String[] args){
try{
InputStream myxls = new FileInputStream("C:\\exceltest.xls");
HSSFWorkbook wb = new HSSFWorkbook(myxls);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
System.out.println("Total Rows :: " + sheet.getLastRowNum());
//Iterate over each row
while (rows.hasNext())
{
HSSFRow row = (HSSFRow) rows.next();
if(checkIfRowEmpty(row))
continue;
int minColIndex = row.getFirstCellNum();
int maxColIndex = row.getLastCellNum();
for(int colIndex = minColIndex; colIndex <= maxColIndex; colIndex++)
{
HSSFCell cell = row.getCell(colIndex);
// POI can't recognize empty cells of Excel, so we should create new cells with a blank value
if(cell == null)
{
cell = row.createCell(colIndex);
cell.setCellValue(" ");
}
String value = "";
switch(cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
value = Double.toString(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = Boolean.toString(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
value = "";
break;
default:
value = cell.getStringCellValue();
break;
}
System.out.print(value + "|");
}
System.out.println();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static boolean checkIfRowEmpty(HSSFRow row)
{
int minColumIndex = row.getFirstCellNum();
int maxColumnIndex = row.getLastCellNum();
boolean isRowEmpty = true;
for(int columIndex = minColumIndex; columIndex <= maxColumnIndex; columIndex++)
{
HSSFCell cell = row.getCell(columIndex);
if(cell == null || cell.toString().trim().isEmpty() || cell.toString().length() == 0){
}
else{
isRowEmpty = false;
return isRowEmpty;
}
}
return isRowEmpty;
}
}
If you want to run multiple versions of firefox on same machine, follow below steps:
1. Close your all running instances of firefox
2. Download older versions of firefox from http://www.oldversion.com/Mozilla-Firefox.html and do a custom install, install them on a specific version directory. For example I installed them as below: C:\Program Files\Mozilla Firefox_6.0.2 C:\Program Files\Mozilla Firefox_7.0.1
3. Create profiles for every version by following steps: A. Goto Run command (or press WINDOWS+R) B. C:\Program Files\Mozilla Firefox_6.0.2 -P C. It will open a profile window, Create a profile name with 'Firefox6.0.2'
4. Repeat step 3 for all firefox versions, create unique profile for each version
5. To run a specific version of firefox use below command: <path to firefox executable> -no-remote -P <profile name>
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();
}
}
}
}