|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.sahi.client.BrowserElements
net.sf.sahi.client.Browser
public class Browser
Browser is the main driver class for Sahi.
It performs various actions on the browser and is also a factory for ElementStubs which are a representation
of various elements on the browser DOM.
A Browser instance is associated with a specific session on the Sahi proxy.
Eg.
String sahiBasePath = "D:\\path\\to\\sahi_dir";
Sting userDataDirectory = "D:\\path\\to\\userdata_dir"; // userdata_dir is in sahiBasePath/userdata by default
net.sf.sahi.config.Configuration.initJava(sahiBasePath, userDataDirectory);
Browser browser = new Browser()
String browserPath = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
String browserProcessName = "iexplore.exe";
String browserOption = "-noframemerging -extoff"; // Set to "-noframemerging -extoff" for IE 8, "" for IE 7
browser = new Browser(browserPath, browserProcessName, browserOption);
browser.open();
browser.navigateTo("http://www.google.com");
browser.textbox("q").setValue("sahi forums");
browser.submit("Google Search").click();
browser.link("Sahi - Web Automation and Test Tool").click();
browser.link("Login").click();
assertTrue(browser.textbox("req_username").exists());
browser.close();
| Constructor Summary | |
|---|---|
Browser()
|
|
Browser(java.lang.String browserName)
Constructs a Browser object and associates it with a session on Sahi Proxy |
|
Browser(java.lang.String browserName,
java.lang.String host,
int port)
|
|
Browser(java.lang.String browserPath,
java.lang.String browserProcessName,
java.lang.String browserOption)
Constructs a Browser object and associates it with a session on Sahi Proxy |
|
Browser(java.lang.String browserPath,
java.lang.String browserProcessName,
java.lang.String browserOption,
java.lang.String host,
int port)
Constructs a Browser object and associates it with a session on Sahi Proxy |
|
| Method Summary | |
|---|---|
void |
addURLMock(java.lang.String urlPattern)
Force Sahi to return a canned response for specific URL patterns The response is found in sahi/htdocs/spr/simpleMock.htm and can be modified |
void |
addURLMock(java.lang.String urlPattern,
java.lang.String responseClass_method)
Force Sahi to return a dynamic response returned by responseClass.methodName |
void |
check(ElementStub element)
Checks the given checkbox or radio only if it is unchecked. |
boolean |
checked(ElementStub el)
Returns true if the element is checked. |
void |
choose(ElementStub elementStub,
java.lang.String[] values,
boolean append)
Chooses the given options in a multi select box (<select> tag). |
void |
choose(ElementStub elementStub,
java.lang.String value,
boolean append)
Chooses the given option in a select box (<select> tag). |
void |
clearLastAlert()
Clears the lastAlert message |
void |
clearLastConfirm()
Clears the lastConfirm message |
void |
clearLastDownloadedFileName()
Resets the lastDownloadedFileName to null |
void |
clearLastPrompt()
Clears the lastPrompt message |
void |
click(ElementStub element)
Clicks the given element |
void |
close()
Closes the browser instance. |
boolean |
containsHTML(ElementStub el,
java.lang.String html)
Returns true if the element's innerHTML contains the input html |
boolean |
containsText(ElementStub el,
java.lang.String text)
Returns true if the element contains the input text |
Browser |
domain(java.lang.String domainName)
Represents a portion of the page which is from a different domain. |
void |
doubleClick(ElementStub element)
Double clicks the given element |
void |
dragDrop(ElementStub dragElement,
ElementStub dropElement)
Simulates a drag and drop event |
void |
execute(java.lang.String step)
Executes any javascript on the browser. |
void |
executeStep(java.lang.String step)
|
boolean |
exists(ElementStub el)
Returns true if the element exists on the browser Retries a few times if the return value is false. |
boolean |
exists(ElementStub el,
boolean optimistic)
Returns true if the element exists on the browser Retries a few times if optimistic is false. |
void |
expectConfirm(java.lang.String message,
boolean input)
Sets the input value of a confirm dialog with given message. This needs to be set before a confirm is expected. Prompts are generated in javascript via window.confirm(message) |
void |
expectPrompt(java.lang.String message,
java.lang.String input)
Sets the input value of a prompt dialog with given message. This needs to be set before a prompt is expected. Prompts are generated in javascript via window.prompt(message) |
java.lang.String |
fetch(ElementStub el)
Fetches the string value of an element stub by performing an eval on the browser |
java.lang.String |
fetch(java.lang.String expression)
Fetches the value of any DOM property |
void |
focus(ElementStub element)
Brings focus on the element. |
java.lang.String[] |
getRecordedSteps()
Gets the recorded steps from the last time getRecordedSteps() was called. |
java.lang.String |
getSelectedText(ElementStub el)
Returns the selected text visible in a select box (<select> tag) |
java.lang.String |
getText(ElementStub el)
Returns the inner text of given element from the browser |
java.lang.String |
getValue(ElementStub el)
Returns the value of given form element from the browser |
boolean |
isChrome()
Checks for Google Chrome browser |
boolean |
isFF()
Checks for Firefox browser |
boolean |
isFirefox()
Checks for Firefox browser |
boolean |
isIE()
Checks for Internet Explorer browser |
boolean |
isOpera()
Checks for Opera browser |
boolean |
isSafari()
Checks for Safari browser |
boolean |
isVisible(ElementStub el)
Returns true if the element is visible on the browser |
boolean |
isVisible(ElementStub el,
boolean optimistic)
Returns true if the element is visible on the browser Retries a few times if optimistic is false. |
void |
kill()
Kills the browser instance |
java.lang.String |
lastAlert()
Returns the last alert message from browser Alert messages are generated via window.alert(message) in javascript. |
java.lang.String |
lastConfirm()
Returns the last confirm message from browser Confirm messages are generated via window.confirm(message) in javascript. |
java.lang.String |
lastDownloadedFileName()
Sahi automatically downloads files into sahi/userdata/temp/download directory. If a file was downloaded as a result of a click, its fileName will be accessible for assertion |
java.lang.String |
lastPrompt()
Returns the last prompt message from browser Alert messages are generated via window.prompt(message) in javascript. |
void |
mouseOver(ElementStub link)
Simulates a mouse over on the given element |
void |
navigateTo(java.lang.String url)
Navigates to the given URL |
void |
navigateTo(java.lang.String url,
boolean forceReload)
Navigates to the given URL |
void |
open()
Opens a browser instance and associates with a session on Sahi proxy. |
Browser |
popup(java.lang.String popupName)
Represents a popup window. |
void |
removeFocus(ElementStub element)
Removes focus from the element. |
void |
removeURLMock(java.lang.String urlPattern)
Removes any mocks associated with given pattern |
void |
restartPlayback()
Re-initializes proxy for playback if proxy is restarted. Should be invoked if proxy is started as a different process and needs a restart. |
void |
rightClick(ElementStub element)
Right clicks the given element |
void |
rteWrite(ElementStub rte,
java.lang.String value)
Sets the value in a Rich Text Editor (RTE) |
void |
saveDownloadedAs(java.lang.String newFilePath)
Saves the last downloaded file to required location. Can be used to save file to some location and then verify contents by reading it. |
java.lang.String |
sessionId()
|
void |
setBrowserJS(java.lang.String browserJS)
Allows given javascript to be injected into the browser. Custom functions or stubbed functions can be passed through this The given javascript will be available to every web page in this session Equivalent to adding <browser> blocks in Sahi Script Eg.
browser.setBrowserJS("function myFinderFn(i){return document.links[i];}"); |
void |
setFile(ElementStub textbox,
java.lang.String value)
Sets the file to be posted to the server via a file input field. |
void |
setSessionId(java.lang.String sessionId)
|
void |
setSpeed(int interval)
Sets the speed of playback. Some applications do not trigger AJAX requests on response to events, but use a small delay before execution. It is useful to tweak this parameter (eg. |
void |
setValue(ElementStub textbox,
java.lang.String value)
Sets the value in a form element |
void |
startRecording()
Starts recording. |
void |
stopRecording()
Stops recording. |
java.lang.String |
style(ElementStub el,
java.lang.String attribute)
Returns the computed css style |
java.lang.String |
title()
|
void |
uncheck(ElementStub element)
Unchecks the given checkbox only if it is checked. |
void |
waitFor(BrowserCondition condition,
int timeout)
Waits till the condition is satisfied or till timeout |
void |
waitFor(long timeout)
Waits till timeout milliseconds |
| Methods inherited from class net.sf.sahi.client.BrowserElements |
|---|
accessor, area, blockquote, bold, button, byClassName, byId, bySeleniumLocator, byText, byXPath, cell, checkbox, code, cookie, div, emphasis, file, heading1, heading2, heading3, heading4, heading5, heading6, hidden, iframe, image, imageSubmitButton, italic, label, link, list, listItem, map, option, parentCell, parentNode, parentRow, parentTable, password, position, preformatted, radio, reset, row, rte, select, span, spandiv, strong, submit, table, tableHeader, textarea, textbox, xy |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Browser(java.lang.String browserName)
browserName - - Name of the browser as it is in browser_types.xml
public Browser(java.lang.String browserPath,
java.lang.String browserProcessName,
java.lang.String browserOption)
browserPath - The browser executable pathbrowserProcessName - The process name to look for to run a kill commandbrowserOption - Any browser options. Leave blank if not required.
public Browser(java.lang.String browserPath,
java.lang.String browserProcessName,
java.lang.String browserOption,
java.lang.String host,
int port)
browserPath - The browser executable pathbrowserProcessName - The process name to look for to run a kill commandbrowserOption - Any browser options. Leave blank if not required.
public Browser(java.lang.String browserName,
java.lang.String host,
int port)
public Browser()
| Method Detail |
|---|
public void restartPlayback()
public void navigateTo(java.lang.String url)
throws ExecutionException
url -
ExecutionException
public void navigateTo(java.lang.String url,
boolean forceReload)
throws ExecutionException
url - forceReload - boolean forces reload
ExecutionException
public void execute(java.lang.String step)
throws ExecutionException
step -
ExecutionException
public void executeStep(java.lang.String step)
throws ExecutionException
ExecutionExceptionpublic void open()
public void close()
public void kill()
public void setValue(ElementStub textbox,
java.lang.String value)
throws ExecutionException
textbox - value -
ExecutionException
public void setFile(ElementStub textbox,
java.lang.String value)
throws ExecutionException
elementStub - value -
ExecutionException
public void click(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void doubleClick(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void rightClick(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void check(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void uncheck(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void focus(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void removeFocus(ElementStub element)
throws ExecutionException
element -
ExecutionException
public void mouseOver(ElementStub link)
throws ExecutionException
link -
ExecutionException
public void dragDrop(ElementStub dragElement,
ElementStub dropElement)
throws ExecutionException
dragElement - Element to dragdropElement - Element to drop on
ExecutionException
public java.lang.String fetch(java.lang.String expression)
throws ExecutionException
expression -
ExecutionException
public java.lang.String fetch(ElementStub el)
throws ExecutionException
element -
ExecutionException
public java.lang.String getText(ElementStub el)
throws ExecutionException
el -
ExecutionException
public java.lang.String getValue(ElementStub el)
throws ExecutionException
el -
ExecutionExceptionpublic boolean exists(ElementStub el)
elementStub -
public boolean exists(ElementStub el,
boolean optimistic)
elementStub - optimistic - boolean. If true returns in a single try. If false, retries a few times.
public boolean isVisible(ElementStub el)
throws ExecutionException
elementStub -
ExecutionException
public boolean isVisible(ElementStub el,
boolean optimistic)
throws ExecutionException
elementStub - optimistic - boolean. If true returns in a single try. If false, retries a few times.
ExecutionException
public java.lang.String getSelectedText(ElementStub el)
throws ExecutionException
selectElement -
ExecutionExceptionpublic Browser popup(java.lang.String popupName)
popupName -
public Browser domain(java.lang.String domainName)
domainName -
public void waitFor(long timeout)
timeout - in milliseconds
public void waitFor(BrowserCondition condition,
int timeout)
condition - BrowserConditiontimeout - in milliseconds
BrowserCondition condition = new BrowserCondition(browser); browser.waitFor(condition, 5000);The above code will make the browser wait till the textbox's value becomes "populated". If it does not become "populated", the browser will wait for max 5000 ms before moving to the next step
public void choose(ElementStub elementStub,
java.lang.String value,
boolean append)
throws ExecutionException
selectElement - value - append: - if true, option is selected without unselecting previous option in multi-select box
ExecutionException
public void choose(ElementStub elementStub,
java.lang.String[] values,
boolean append)
throws ExecutionException
selectElement - values - append: - if true, options are selected without unselecting previous options in multi-select box
ExecutionExceptionpublic void startRecording()
public void stopRecording()
public java.lang.String[] getRecordedSteps()
public java.lang.String lastAlert()
throws ExecutionException
ExecutionException
public java.lang.String lastConfirm()
throws ExecutionException
ExecutionException
public void expectPrompt(java.lang.String message,
java.lang.String input)
throws ExecutionException
message - String visible message prompted by the browserinput - String input to enter in the prompt dialog
ExecutionException
public void expectConfirm(java.lang.String message,
boolean input)
throws ExecutionException
message - String visible message prompted by the browserinput - boolean true to click on 'OK', false to click on 'Cancel'
ExecutionException
public java.lang.String lastPrompt()
throws ExecutionException
ExecutionException
public void clearLastAlert()
throws ExecutionException
ExecutionException
public void clearLastPrompt()
throws ExecutionException
ExecutionException
public void clearLastConfirm()
throws ExecutionException
ExecutionException
public java.lang.String title()
throws ExecutionException
ExecutionException
public boolean checked(ElementStub el)
throws ExecutionException
el -
ExecutionExceptionpublic void addURLMock(java.lang.String urlPattern)
urlPattern - a javascript regular expression as a string
public void addURLMock(java.lang.String urlPattern,
java.lang.String responseClass_method)
urlPattern - a javascript regular expression as a stringresponseClass_method - The class which will respond to matching requestspublic void removeURLMock(java.lang.String urlPattern)
urlPattern - public java.lang.String lastDownloadedFileName()
public void clearLastDownloadedFileName()
public void saveDownloadedAs(java.lang.String newFilePath)
newFilePath - public void setSpeed(int interval)
interval - time in milliseconds
public boolean containsText(ElementStub el,
java.lang.String text)
el - text -
public boolean containsHTML(ElementStub el,
java.lang.String html)
el - html -
public java.lang.String style(ElementStub el,
java.lang.String attribute)
el - attribute -
public void setSessionId(java.lang.String sessionId)
public java.lang.String sessionId()
public void rteWrite(ElementStub rte,
java.lang.String value)
rte - value -
ExecutionExceptionpublic void setBrowserJS(java.lang.String browserJS)
browser.setBrowserJS("function myFinderFn(i){return document.links[i];}");
// The above code defines a function myFinderFn, which will be available on each page of the application.
browser.setBrowserJS("function checkFileFieldIsPopulated(){return true}");
// The above code will redefine checkFileFieldIsPopulated function so that it will always return true
browserJS - public boolean isChrome()
public boolean isFirefox()
public boolean isFF()
public boolean isIE()
public boolean isSafari()
public boolean isOpera()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||