1.How to set the browser to Proxy Injection Mode ?
*pi(browsername);
2.How to avoid using waitForpageload() ,wait(),pause(),thread.sleep()?
Add selenium.setSpeed(Constants.DEFAULT_TIMEOUT); at the start of the script.
3.How to check for dynamic ids in the object string using xpath
for example if dynamic ids have the format text-12345
where 12345 is a dynamic number you could use the following
XPath: //input[starts-with(@id, 'text-')]
4.How to Capture selenium server side logs if you don't start the server through code ?
java -jar selenium-server.jar -log selenium.log
5.How to Capture Browserside logs?
java -jar selenium-server.jar -log browserSideLog
6.How to use contains function in xpath ?
"//a[contains(text(),'Add New')]"
If there are multiple Add New on page its better to nail down to the one we are looking to click with index subscript.
//a[contains(text(),'Posts')]/following::a[contains(text(),'Add New')][1]
So we are using xpath axis which is following & an index[1]
Axis are used to work with node sets
This blog comprises of learning to get started with Open source automation tools. Mentions best practices of Agile,Xtreme Programming Selenium In nutshell.
Favourite Quote
Wednesday, November 17, 2010
How to Capture Network Traffic using Selenium
CapturingNetwork Traffic - It could be used to know details such as the browser information.
public class Epc extends SeleneseTestCase
{
public static SeleniumServer selServer;
public static DefaultSelenium selenium;
final String URL = "http://10.10.7.48:38080/";
int port = Constants.PORT;
String browser = Constants.BROWSER;
public void setUp() throws Exception
{
if(selServer == null)
{
try
{
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.trustAllSSLCertificates();
selServer = new SeleniumServer(rc);
selServer.start();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public void testWebservices() throws Exception
{
selenium = new DefaultSelenium("localhost",Constants.PORT,Constants.BROWSER,URL);
selenium.start("captureNetworkTraffic=true");
selenium.open("/epc-client");
String trafficOutput = selenium.captureNetworkTraffic("plain");
System.out.println("\nNetwork Traffic captured in plain format !!!!"+trafficOutput);
pause(Constants.MAX_WAIT_TIME_MS);
selenium.windowFocus();
selenium.windowMaximize();
selenium.useXpathLibrary("javascript-xpath");
pause(Constants.MAX_WAIT_TIME_MS);
selenium.click("link=Search UPRN by Address Web Service");
}
}
public void tearDown()
{
selenium.stop();
selServer.stop();
}
}
public class Epc extends SeleneseTestCase
{
public static SeleniumServer selServer;
public static DefaultSelenium selenium;
final String URL = "http://10.10.7.48:38080/";
int port = Constants.PORT;
String browser = Constants.BROWSER;
public void setUp() throws Exception
{
if(selServer == null)
{
try
{
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.trustAllSSLCertificates();
selServer = new SeleniumServer(rc);
selServer.start();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
public void testWebservices() throws Exception
{
selenium = new DefaultSelenium("localhost",Constants.PORT,Constants.BROWSER,URL);
selenium.start("captureNetworkTraffic=true");
selenium.open("/epc-client");
String trafficOutput = selenium.captureNetworkTraffic("plain");
System.out.println("\nNetwork Traffic captured in plain format !!!!"+trafficOutput);
pause(Constants.MAX_WAIT_TIME_MS);
selenium.windowFocus();
selenium.windowMaximize();
selenium.useXpathLibrary("javascript-xpath");
pause(Constants.MAX_WAIT_TIME_MS);
selenium.click("link=Search UPRN by Address Web Service");
}
}
public void tearDown()
{
selenium.stop();
selServer.stop();
}
}
Wednesday, November 10, 2010
DataDriven Testing using Selenium - java
I am using excel for data driven testing.
public void login()
{
String uName = username;
String pWord = password;
try
{
FileInputStream fi = new FileInputStream ("C:\\Selenium\\datainput\\webservices.xls");
Workbook wbk = Workbook.getWorkbook(fi);
Sheet sht = wbk.getSheet("Credentials");
for(int rowCount=1;rowCount<=sht.getRows();rowCount++)
{
uName = sht.getCell(1,rowCount).getContents();
pWord = sht.getCell(2, rowCount).getContents();
objSelenium.type("xpath=//input[@id='j_username']", uName);
selenium.wait(Constants.MAX_WAIT_TIME_MS);
objSelenium.type("xpath=//input[@id='j_password']", pWord);
selenium.wait(Constants.MAX_WAIT_TIME_MS);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
public void login()
{
String uName = username;
String pWord = password;
try
{
FileInputStream fi = new FileInputStream ("C:\\Selenium\\datainput\\webservices.xls");
Workbook wbk = Workbook.getWorkbook(fi);
Sheet sht = wbk.getSheet("Credentials");
for(int rowCount=1;rowCount<=sht.getRows();rowCount++)
{
uName = sht.getCell(1,rowCount).getContents();
pWord = sht.getCell(2, rowCount).getContents();
objSelenium.type("xpath=//input[@id='j_username']", uName);
selenium.wait(Constants.MAX_WAIT_TIME_MS);
objSelenium.type("xpath=//input[@id='j_password']", pWord);
selenium.wait(Constants.MAX_WAIT_TIME_MS);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
Generating HTML reports at granularity
Generating detailed reports with logs & screenshots.
Using the LoggingSelenium Interface.
1.Must have Selenium-client.jar for java
2.Junit4.jar
3.Commons-lang.jar
4.loggingselenium.jar
Include all the above jars in your classpath.
package com.est.org;
// import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertEquals;
// import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.selenium.HttpCommandProcessor;
import com.unitedinternet.portal.selenium.utils.logging.HtmlResultFormatter;
import com.unitedinternet.portal.selenium.utils.logging.LoggingCommandProcessor;
import com.unitedinternet.portal.selenium.utils.logging.LoggingDefaultSelenium;
import com.unitedinternet.portal.selenium.utils.logging.LoggingResultsFormatter;
import com.unitedinternet.portal.selenium.utils.logging.LoggingSelenium;
import com.unitedinternet.portal.selenium.utils.logging.LoggingUtils;
import org.openqa.selenium.server.*;
public class Loggingselenium
{
protected LoggingSelenium selenium;
private BufferedWriter loggingWriter;
private static final String RESULT_FILE_ENCODING = "UTF-8";
private static final String DEFAULT_TIMEOUT = "30000";
private static final String OPENQA_URL = "http://192.168.9.104:8080/";
private static final String SCREENSHOT_PATH = "screenshots";
private final String RESULTS_BASE_PATH = "C:/" + File.separator+ "loggingResults" + System.currentTimeMillis();
private String resultsPath = new File(RESULTS_BASE_PATH).getAbsolutePath();
private String screenshotsResultsPath = new File(RESULTS_BASE_PATH+ File.separator + SCREENSHOT_PATH).getAbsolutePath();
private SeleniumServer selServer;
@Before
public void setUp() {
if (!new File(screenshotsResultsPath).exists()) {
new File(screenshotsResultsPath).mkdirs();
}
final String resultHtmlFileName = resultsPath + File.separator+ "sampleResultSuccess.html";
loggingWriter = LoggingUtils.createWriter(resultHtmlFileName,Loggingselenium.RESULT_FILE_ENCODING, true);
LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter,Loggingselenium.RESULT_FILE_ENCODING);
htmlFormatter.setScreenShotBaseUri(Loggingselenium.SCREENSHOT_PATH+ "/");
htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
if(selServer == null)
{
try
{
SeleniumServer selServer = new SeleniumServer();
selServer.start();
}catch(Exception e){}
}
LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*firefox",OPENQA_URL), htmlFormatter);
myProcessor.setExcludedCommands(new String[]{});
selenium = new LoggingDefaultSelenium(myProcessor);
selenium.start();
}
@After
public void tearDown() {
selenium.stop();
try {
if (null != loggingWriter) {
loggingWriter.close();
}
} catch (IOException e) {}
}
@Test
public void loggingSelenium() throws InterruptedException,ParseException
{
selenium.setContext("loggingSelenium()");
selenium.open("/epc-client");
selenium.windowFocus();
selenium.windowMaximize();
selenium.captureScreenshot(screenshotsResultsPath + File.separator+ "openQaHomePage_" + LoggingUtils.timeStampForFileName()+ ".png");
try {
selenium.click("link=Search UPRN by Address Web Service");
selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
selenium.click("//input[@type='radio'][6]");
FileInputStream fi = new FileInputStream("C:\\Selenium\\datainput\\webservices.xls");
Workbook wBk = Workbook.getWorkbook(fi);
Sheet sht = wBk.getSheet("webService");
selenium.type("request",sht.getCell(3,0).getContents());
selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
selenium.wait(30000);
selenium.click("//input[@name='Submit' and @value='submit']");
selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
String textArea = selenium.getValue("//textarea[@name='header']");
if(textArea.contains("null"))
{
System.out.println("\n Invalid request XML submitted!!!!");
}
} catch (Exception e) {}
}
}
Using the LoggingSelenium Interface.
1.Must have Selenium-client.jar for java
2.Junit4.jar
3.Commons-lang.jar
4.loggingselenium.jar
Include all the above jars in your classpath.
package com.est.org;
// import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertEquals;
// import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.thoughtworks.selenium.HttpCommandProcessor;
import com.unitedinternet.portal.selenium.utils.logging.HtmlResultFormatter;
import com.unitedinternet.portal.selenium.utils.logging.LoggingCommandProcessor;
import com.unitedinternet.portal.selenium.utils.logging.LoggingDefaultSelenium;
import com.unitedinternet.portal.selenium.utils.logging.LoggingResultsFormatter;
import com.unitedinternet.portal.selenium.utils.logging.LoggingSelenium;
import com.unitedinternet.portal.selenium.utils.logging.LoggingUtils;
import org.openqa.selenium.server.*;
public class Loggingselenium
{
protected LoggingSelenium selenium;
private BufferedWriter loggingWriter;
private static final String RESULT_FILE_ENCODING = "UTF-8";
private static final String DEFAULT_TIMEOUT = "30000";
private static final String OPENQA_URL = "http://192.168.9.104:8080/";
private static final String SCREENSHOT_PATH = "screenshots";
private final String RESULTS_BASE_PATH = "C:/" + File.separator+ "loggingResults" + System.currentTimeMillis();
private String resultsPath = new File(RESULTS_BASE_PATH).getAbsolutePath();
private String screenshotsResultsPath = new File(RESULTS_BASE_PATH+ File.separator + SCREENSHOT_PATH).getAbsolutePath();
private SeleniumServer selServer;
@Before
public void setUp() {
if (!new File(screenshotsResultsPath).exists()) {
new File(screenshotsResultsPath).mkdirs();
}
final String resultHtmlFileName = resultsPath + File.separator+ "sampleResultSuccess.html";
loggingWriter = LoggingUtils.createWriter(resultHtmlFileName,Loggingselenium.RESULT_FILE_ENCODING, true);
LoggingResultsFormatter htmlFormatter = new HtmlResultFormatter(loggingWriter,Loggingselenium.RESULT_FILE_ENCODING);
htmlFormatter.setScreenShotBaseUri(Loggingselenium.SCREENSHOT_PATH+ "/");
htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
if(selServer == null)
{
try
{
SeleniumServer selServer = new SeleniumServer();
selServer.start();
}catch(Exception e){}
}
LoggingCommandProcessor myProcessor = new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*firefox",OPENQA_URL), htmlFormatter);
myProcessor.setExcludedCommands(new String[]{});
selenium = new LoggingDefaultSelenium(myProcessor);
selenium.start();
}
@After
public void tearDown() {
selenium.stop();
try {
if (null != loggingWriter) {
loggingWriter.close();
}
} catch (IOException e) {}
}
@Test
public void loggingSelenium() throws InterruptedException,ParseException
{
selenium.setContext("loggingSelenium()");
selenium.open("/epc-client");
selenium.windowFocus();
selenium.windowMaximize();
selenium.captureScreenshot(screenshotsResultsPath + File.separator+ "openQaHomePage_" + LoggingUtils.timeStampForFileName()+ ".png");
try {
selenium.click("link=Search UPRN by Address Web Service");
selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
selenium.click("//input[@type='radio'][6]");
FileInputStream fi = new FileInputStream("C:\\Selenium\\datainput\\webservices.xls");
Workbook wBk = Workbook.getWorkbook(fi);
Sheet sht = wBk.getSheet("webService");
selenium.type("request",sht.getCell(3,0).getContents());
selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
selenium.wait(30000);
selenium.click("//input[@name='Submit' and @value='submit']");
selenium.waitForPageToLoad(DEFAULT_TIMEOUT);
String textArea = selenium.getValue("//textarea[@name='header']");
if(textArea.contains("null"))
{
System.out.println("\n Invalid request XML submitted!!!!");
}
} catch (Exception e) {}
}
}
Subscribe to:
Posts (Atom)