0% found this document useful (0 votes)
738 views5 pages

Selenium Code Examples

This document provides instructions on how to maximize and focus the Firefox window size in Selenium, set timeout values for open commands, and get all checkbox IDs on a page. It includes code snippets in Java showing how to call the selenium.windowMaximize(), selenium.windowFocus(), and selenium.setTimeout() methods to configure the browser window and timeouts. It also includes a method to retrieve all checkbox IDs using JavaScript and storing them in an array.

Uploaded by

Mike Hussey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
0% found this document useful (0 votes)
738 views5 pages

Selenium Code Examples

This document provides instructions on how to maximize and focus the Firefox window size in Selenium, set timeout values for open commands, and get all checkbox IDs on a page. It includes code snippets in Java showing how to call the selenium.windowMaximize(), selenium.windowFocus(), and selenium.setTimeout() methods to configure the browser window and timeouts. It also includes a method to retrieve all checkbox IDs using JavaScript and storing them in an array.

Uploaded by

Mike Hussey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1/ 5

How to Maximize and focus firefox Window Size : :::

public void setUp() throws Exception


{
selenium = new DefaultSelenium("Localhost", 4444, "*firefox",
"http://www.google.com/");
selenium.start();
selenium.windowMaximize();
selenium.windowFocus();
}

How to Set timeout for OPEN Command :::::

public void setUp() throws Exception


{
selenium = new DefaultSelenium("Localhost", 4444, "*firefox",
"http://www.google.com/");
selenium.start();
selenium.setTimeout("900000");
selenium.windowMaximize();
selenium.windowFocus();
}

How to Get all Check box ids :::::

@Test public static String[] getAllCheckboxIds (String[] x2)


{
String script = "var inputId = new Array();";// Create array in
java script.
script += "var cnt = 0;"; // Counter for check box idss.
script += "var inputFields = new Array();"; // Create array in java
script.
script += "inputFields =
window.document.getElementsByTagName('input');"; // Collect input elements.
script += "for(var i=0; i<inputFields.length; i++) {"; // Loop
through the collected elements.
script += "if(inputFields[i].id !=null " +
"&& inputFields[i].id !='undefined' " +
"&& inputFields[i].getAttribute('type') == 'checkbox') {"; // If
input field is of type check box and input id is not null.
script += "inputId[cnt]=inputFields[i].id ;" + // Save check box id
to inputId array.
"cnt++;" + // increment the counter.
"}" + // end of if.
"}"; // end of for.
script += "inputId.toString();" ;// Convert array in to string.
String[] checkboxIds = selenium.getEval(script).split(","); // Split
the string.
return checkboxIds;
}
1) What does SIDE stand for?
Ans: Selenium Integrated Development Environment.

2) What is the difference between an assert and a verify with Selenium commands?
Ans: Assert: Will fail and abort the current test execution.Verify: Will fail and continue to run the
test execution.

3) What Selenese commands can be used to help debug a regexp?


Ans:

4) What is one big difference between SilkTest and Selenium, excluding the price?
Ans: I am not sure about Silk Test. But general difference what i found from other Automation
Testing tools is that, selenium does not work on Object Repository concept for identifying the
objects in the application

5) Which browsers can Selenium IDE be run in?


Ans: FireFox

6) If a Selenium function requires a script argument, what would that argument look like in
general terms?
Ans: StoreEval(script, variable) and storeExpression(expression, variableName)

7) If a Selenium function requires a pattern argument, what five prefixes might that argument
have?
Ans: glob, regexp, exact, regexpi

8)    What is the regular expression sequence that loosely translates to "anything or nothing?"
Ans:     glob: Real* or regexp:Real.*

9)    What is the globbing sequence that loosely translates to "anything or nothing?
Ans:    *

10)    What does a character class for all alphabetic characters and digits look like in regular 
expressions?
Ans:

POSIX Perl ASCII Description


[:alnum:] [A-Za-z0-9] Alphanumeric characters
[:word:] \w [A-Za-z0-9_] Alphanumeric characters plus "_"
[:alpha:] [A-Za-z] Alphabetic characters
[:digit:] \d [0-9] Digits
[:lower:] [a-z] Lowercase letters
[:upper:] [A-Z] Uppercase letters

11)    What does a character class for all alphabetic characters and digits look like in
globbing?
Ans:     [0-9] matches any digit
    [a-zA-Z0-9] matches any alphanumeric character
    [a-zA-Z] matches any alphabet character

12)    What must one set within SIDE in order to run a test from the beginning to a certain
point within the test?
Ans:     Breakpoint (Keyword: b OR in SIDE, Right click and select “Toggle Breakpoint”)

13)    What does a right-pointing green triangle at the beginning of a command in SIDE
indicate?
Ans:    Play entire Test suite

14)    How does one get rid of the right-pointing green triangle?
Ans:

15)    How can one add vertical white space between sections of a single test?
Ans:

16)    What Selenium functionality uses wildcards?


Ans:    

17)    Which wildcards does SIDE support?


Ans:     *, [ ]

NOTE: PLEASE  correct me if i am wrong

18)    What are the four types of regular expression quantifiers which we've studied?
Ans:

19)    What regular expression special character(s) means "any character?"


Ans:    “.*”

20)    What distinguishes between an absolute and relative URL in SIDE?


Ans:    Absolute URL: Its is base url and this represent domain address.
           Relative URL: (Absolute URL + Page Path).
          Open command uses Base URL (Absolute URL) to navigate web page.

21)    How would one access a Selenium variable named "count" from within a JavaScript
snippet?
Ans:    ${count}

22)    What Selenese command can be used to display the value of a variable in the log file,
which can  be very valuable for debugging?
Ans:    store | Chidambaram | var
          echo  | ${var}

23)    If one wanted to display the value of a variable named answer in the log file, what would
the first argument to the previous command look like?
Ans:    echo | ${answer}

24)    Where did the name "Selenium" come from?


Ans:   

25)    Which Selenium command(s) simulates selecting a link?


Ans:    click, clickandWait, ClickAt, ClickAtandWait, DoubleClick, DoubleClickandWait,
doubleClickAt, doubleClickAtandWait

26)    Which two commands can be used to check that an alert with a particular message
popped up?
Ans:    The following commands are available within Selenium for processing Alerts:
•    getAlert()
•    assertAlert()
•    assertAlertNotPresent()
•    assertAlertPresent()
•    storeAlert()
•    storeAlertPresent()
•    verifyAlert()
•    verifyAlertNotPresent()
•    verifyAlertPresent()
•    waitForAlert()
•    waitForAlertNotPresent()
•    waitForAlertPresent()
The …AlertPresent() and …AlertNotPresent() functions check for the existence or not of an alert –
regardless of it’s content.
The …Alert() functions allow the caller to specify a pattern which should be matched.
The getAlert() method also exists in Selenium RC, and returns the text from the previous Alert
displayed.

27)    What does a comment look like in Column view?


Ans:     Pink color (Appears only in command column)

28)    What does a comment look like in Source view?


Ans:     Normal 0 false false false MicrosoftInternetExplorer4
Command Target Value
store 10 hits
storeXpathCount //blockquote blockquotes
storeEval storedVars[‘hits’]-storedVars[‘blockquotes’] paragraphs

This next example illustrates how a JavaScript snippet can include calls to methods, in this case the
JavaScript String object’s toUpperCase method and toLowerCase method.
Command Target Value
store Edith Wharton name
storeEval storedVars[‘name’].toUpperCase() uc
storeEval storedVars[‘name’].toLowerCase() lc

JavaScript Usage with Non-Script Parameters


JavaScript can also be used to help generate values for parameters, even when the parameter is not
specified to be of type script. However, in this case, special syntax is required–the JavaScript
snippet must be enclosed inside curly braces and preceded by the label javascript, as in javascript
{*yourCodeHere*}. Below is an example in which the type command’s second parameter value is
generated via JavaScript code using this special syntax:
Command Target Value
store league of nations searchString
type q javascript{storedVars[‘searchString’].toUpperCase()}
    Ans is StoredVars[]

43)    How would one access the value of a SIDE variable named name from within a
JavaScript snippet used as the argument to a Selenese command? 
Ans:    ${name}

44)    What is the name of the type of JavaScript entity represented by the last answer?
Ans:   

45)    What string(s) does this regular expression match? regexp:August|April 5, 1908
Ans:    August 5,1980 or April 5, 1980

46)    What Selenium regular expression pattern can be used instead of the glob below to
produce the same results? verifyTextPresent | glob:9512?
Ans:    Glob uses two class pattern : * and []. I am not sure whether question is valid or not.

47)    What Selenium globbing pattern can be used instead of the regexp below to produce the
same results? verifyTextPresent | regexp:Hush.*Charlotte
Ans:     glob: Hush*Charlotte

48)    I am using the selenium ide for testing my web application i recorded some script and
played back while playing the recorded script the “alert msg is not getting closed”.
Ans:    If the alert message is in different browser you have to handle it after recording. You have to
select that popup window and close it.Then transfer the control back to main window.
                 waitForPopUp | winId | 30000
                 selectWindow | winId
                 …
                 close
                 selectWindow

String-match Patterns

Various Pattern syntaxes are available for matching string values:


• glob:pattern:Match a string against a "glob" (aka "wildmat") pattern. "Glob" is akind of
limited regular-expression syntax typically used in command-lineshells. In a glob pattern,
"*" represents any sequence of characters, and "?"represents any single character. Glob
patterns match against the entirestring.
• regexp:regexp:Match a string using a regular-expression. The full power of
JavaScriptregular-expressions is available.
• regexpi:regexpi:Match a string using a case-insensitive regular-expression.
• exact:string:Match a string exactly, verbatim, without any of that fancy wildcardstuff.
If no pattern prefix is specified, Selenium assumes that it's a "glob"pattern.
For commands that return multiple values (such as verifySelectOptions),the string being matched is
a comma-separated list of the return values,where both commas and backslashes in the values are
backslash-escaped.When providing a pattern, the optional matching syntax (i.e. glob,regexp, etc.) is
specified once, as usual, at the beginning of thepattern.

You might also like