|
||||||||||
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); String browserName = "ie"; // default values are "ie", "firefox", "safari", "chrome", "opera" - specified in userdata/config/browser_types.xml Browser browser = new Browser(browserName); 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 |
blur(ElementStub elementStub)
Simulates a blur event on the element |
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 |
java.lang.String |
clearPrintCalled()
Clears the window print statement i.e. |
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 |
int |
count(java.lang.Object... args)
Returns a count of all matching elements Eg. |
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 |
dragDropXY(ElementStub dragElement,
int x,
int y)
Simulates a drag and drop event |
void |
execute(java.lang.String step)
Executes any javascript on the browser. |
void |
executeSahi(java.lang.String step)
Executes any sahi code and javascript on the browser. |
void |
executeStep(java.lang.String step)
|
void |
executeStep(java.lang.String step,
boolean addSahi)
|
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 |
getAttribute(ElementStub el,
java.lang.String attribute)
|
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 |
getSelectionText()
|
java.lang.String |
getSelectionText(ElementStub window)
Getting the selected text |
java.lang.String[] |
getSteps()
Returns the array of steps executed |
java.lang.String |
getText(ElementStub el)
Returns the inner text of given element from the browser |
long |
getTimeBetweenSteps()
Returns the time taken between each step |
java.lang.String |
getValue(ElementStub el)
Returns the value of given form element from the browser |
void |
highlight(ElementStub element)
Highlights the given element |
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 |
keyDown(ElementStub element,
int keyCode,
int charCode)
|
void |
keyDown(ElementStub elementStub,
java.lang.String keySequence)
Simulates a key down event on the given element with a combo value ie. |
void |
keyDown(ElementStub elementStub,
java.lang.String keySequence,
java.lang.String combo)
Simulates a key down event on the given element with a combo value ie. |
void |
keyPress(ElementStub elementStub,
java.lang.String keySequence)
Simulates a key press event on the given element. |
void |
keyPress(ElementStub elementStub,
java.lang.String keySequence,
java.lang.String combo)
Simulates a key press event on the given element with a combo value ie. |
void |
keyUp(ElementStub element,
int keyCode,
int charCode)
|
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 |
mouseDown(ElementStub element)
Simulates a mouse down on the given element |
void |
mouseOver(ElementStub element)
Simulates a mouse over on the given element |
void |
mouseUp(ElementStub element)
Simulates a mouse up 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. |
java.lang.Boolean |
printCalled()
Returns true if window print statement was executed |
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. |
void |
selectRange(ElementStub element,
int rangeStart,
int rangeEnd)
Sahi waits for AJAX readyStates 1,2 and 3. |
void |
selectTextRange(ElementStub element,
java.lang.String searchText)
Selecting text for manipulation (like selecting text in a Rich Text Editor to change font to bold) |
void |
selectTextRange(ElementStub element,
java.lang.String searchText,
java.lang.String position)
Selecting text for manipulation (like selecting text in a Rich Text Editor to change font to bold) |
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 |
setFile(ElementStub textbox,
java.lang.String value,
java.lang.String URL)
Sets the file to be posted to the server via a file input field. |
void |
setFile2(ElementStub textbox,
java.lang.String value)
Sets the file to be posted to the server via a file input field. |
void |
setFile2(ElementStub textbox,
java.lang.String value,
java.lang.String URL)
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 |
setStrictVisibilityCheck(boolean check)
Sets strict visibility check. If true, Sahi APIs will ignore hidden elements. Useful when similar widgets are generated but only one widget is displayed at any time Can set to true globally from sahi.properties/userdata.properties by setting element.visibility_check.strict=true |
void |
setTranslationMode(boolean b)
Sets the translation mode as true or false |
void |
setValue(ElementStub textbox,
java.lang.String value)
Sets the value in a form element |
void |
setXHRReadyStatesToWaitFor(java.lang.String s)
|
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 |
---|
abbr, accessor, activeElement, area, blockquote, bold, button, byClassName, byId, bySeleniumLocator, byText, byXPath, cell, checkbox, code, cookie, datebox, datetimebox, datetimelocalbox, dDesc, div, dList, dTerm, emailbox, emphasis, file, heading1, heading2, heading3, heading4, heading5, heading6, hidden, iframe, image, imageSubmitButton, italic, label, link, list, listItem, map, monthbox, numberbox, option, paragraph, parentCell, parentNode, parentRow, parentTable, password, position, preformatted, radio, rangebox, reset, row, rte, searchbox, select, span, spandiv, strong, submit, table, tableHeader, telbox, textarea, textbox, timebox, urlbox, weekbox, 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.xmlpublic 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 selectRange(ElementStub element, int rangeStart, int rangeEnd) throws ExecutionException
s
-
ExecutionException
public java.lang.String getSelectionText(ElementStub window)
window
- Eg. browser.selectRange(browser.div("s4Id"), 1, 4);public java.lang.String getSelectionText()
public void selectTextRange(ElementStub element, java.lang.String searchText, java.lang.String position) throws ExecutionException
element
- searchText
- position
- Eg. browser.selectTextRange(_rte(1), "red apple", "before");
ExecutionException
public void selectTextRange(ElementStub element, java.lang.String searchText) throws ExecutionException
element
- searchText
- Eg. browser.selectTextRange(_rte(1), "red apple");
ExecutionException
public void setXHRReadyStatesToWaitFor(java.lang.String s)
public void execute(java.lang.String step) throws ExecutionException
step
-
ExecutionException
public void executeSahi(java.lang.String step) throws ExecutionException
step
-
ExecutionException
public void executeStep(java.lang.String step) throws ExecutionException
ExecutionException
public void executeStep(java.lang.String step, boolean addSahi) throws ExecutionException
ExecutionException
public long getTimeBetweenSteps()
public java.lang.String[] getSteps()
public void setTranslationMode(boolean b)
public 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 setFile(ElementStub textbox, java.lang.String value, java.lang.String URL) throws ExecutionException
elementStub
- value
- url
- String The form "action" url pattern to which this upload request is submitted
ExecutionException
public void setFile2(ElementStub textbox, java.lang.String value, java.lang.String URL) throws ExecutionException
elementStub
- value
- url
- String The form "action" url pattern to which this upload request is submitted
ExecutionException
public void setFile2(ElementStub textbox, java.lang.String value) throws ExecutionException
elementStub
- value
-
ExecutionException
public void keyDown(ElementStub elementStub, java.lang.String keySequence, java.lang.String combo) throws ExecutionException
element
- keysequence
- Key to be pressed downcombo
- String can be "ALT", "META", "SHIFT", "CTRL" or a combination of these with | as the separator
eg. "ALT|SHIFT" or "CTRL|ALT|SHIFT"
ExecutionException
public void keyDown(ElementStub elementStub, java.lang.String keySequence) throws ExecutionException
element
- keysequence
- Key to be pressed downcombo
- String can be "ALT", "META", "SHIFT", "CTRL" or a combination of these with | as the separator
eg. "ALT|SHIFT" or "CTRL|ALT|SHIFT"
ExecutionException
public void highlight(ElementStub element) throws ExecutionException
element
-
ExecutionException
public void keyPress(ElementStub elementStub, java.lang.String keySequence, java.lang.String combo) throws ExecutionException
element
- keysequence
- Key to be pressedcombo
- String can be "ALT", "META", "SHIFT", "CTRL" or a combination of these with | as the separator
eg. "ALT|SHIFT" or "CTRL|ALT|SHIFT"
ExecutionException
public void keyPress(ElementStub elementStub, java.lang.String keySequence) throws ExecutionException
element
- keysequence
- Key to be pressed
ExecutionException
public void click(ElementStub element) throws ExecutionException
element
-
ExecutionException
public void blur(ElementStub elementStub)
elementStub
- public java.lang.String getAttribute(ElementStub el, java.lang.String attribute) throws ExecutionException
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 element) throws ExecutionException
element
-
ExecutionException
public void mouseDown(ElementStub element) throws ExecutionException
element
-
ExecutionException
public void mouseUp(ElementStub element) throws ExecutionException
element
-
ExecutionException
public void dragDrop(ElementStub dragElement, ElementStub dropElement) throws ExecutionException
dragElement
- Element to dragdropElement
- Element to drop on
ExecutionException
public void dragDropXY(ElementStub dragElement, int x, int y) throws ExecutionException
dragElement
- Element to dragx
- X coordinatey
- Y coordinate
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
-
ExecutionException
public 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
-
ExecutionException
public Browser popup(java.lang.String popupName)
popupName
-
public Browser domain(java.lang.String domainName)
domainName
-
public void waitFor(long timeout)
timeout
- in millisecondspublic 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
ExecutionException
public 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
-
ExecutionException
public void addURLMock(java.lang.String urlPattern)
urlPattern
- a javascript regular expression as a stringpublic 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 millisecondspublic void setStrictVisibilityCheck(boolean check)
boolean
- 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
-
ExecutionException
public 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()
public int count(java.lang.Object... args)
browser.count("div", "css-class-name")
args
-
public void keyDown(ElementStub element, int keyCode, int charCode)
public void keyUp(ElementStub element, int keyCode, int charCode)
public java.lang.String clearPrintCalled()
public java.lang.Boolean printCalled()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |