Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel
2 Day 5 Selenium IDE Advanced JavaScript
Loops execute a block of code a specified number of times, or while a specified condition is true In JavaScript, there are two different kind of loops: ◦ for - loops through a block of code a specified number of times ◦ while - loops through a block of code while a specified condition is true 3
Used when you know in advance how many times the script should run. Often used to cycle through lists or arrays Syntax for (var=startvalue;var<=endvalue;var=var+increment) { code to be executed } Example var fruit = "apples,oranges,bananas".split(","); for (i=0;i<fruit.length;i++) { alert("We have many sweet " + fruit[i] + "!"); } 4
The while loop cycles through a block of code while a specified condition is true. Used when exact input is unknown Syntax while (var<=endvalue) { code to be executed } Example var name = prompt("What is your name", "Enter your name here"); while (name != "Done") { alert("Very happy to meet you " + name + "!"); var name = prompt("What is your name", "Enter your name here"); } 5
Used to manipulate a stored piece of text. The Math object includes several mathematical constants and methods. The following example uses the length property of the String object to find the length of a string: var txt="Hello world!"; document.write(txt.length); The following example uses the toUpperCase() method of the String object to convert a string to uppercase letters: var txt="Hello world!"; document.write(txt.toUpperCase()); 6 12 HELLO WORLD!
MethodDescription indexOf() Returns the position of the first found occurrence of a specified value in a string lastIndexOf() Returns the position of the last found occurrence of a specified value in a string match() Searches for a match between a regular expression and a string, and returns the matches replace() Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring search() Searches for a match between a regular expression and a string, and returns the position of the match slice() Extracts a part of a string and returns a new string split() Splits a string into an array of substrings substr() Extracts the characters from a string, beginning at a specified start position, and through the specified number of character substring() Extracts the characters from a string, between two specified indices toLowerCase() Converts a string to lowercase letters toUpperCase() Converts a string to uppercase letters 7
Each HTML document loaded into a browser window becomes a Document (DOM) object. The Document object provides access to all HTML elements in a page, from within a script. JS HTML DOM can be used to retrieve dynamically assigned ids of elements, during test execution Complete DOM reference: 8
getElementById() - Accesses the first element with the specified id getElementById() document.getElementById() getElementsByName() - all elements with a specified name getElementsByName() document.getElementsByName() getElementsByTagName() - Accesses all elements with a specified tagname getElementsByTagName() document.getElementsByTagName() 9
DOM AnchorDOM Input Radio DOM AreaDOM Input Reset DOM BaseDOM Input Submit DOM BodyDOM Input Text DOM ButtonDOM Link DOM FormDOM Meta DOM Frame/IFrameDOM Object DOM FramesetDOM Option DOM ImageDOM Select DOM Input ButtonDOM Style DOM Input CheckboxDOM Table DOM Input FileDOM TableCell DOM Input HiddenDOM TableRow DOM Input PasswordDOM Textarea 10
id - Returns the id of an element id var x = document.getElementsByTagName(“div”); var element_id = x[0].id; nodeType - Returns the type of the element nodeType var x = document.getElementsByTagName(“input”); var element_type = x[0].nodeType; tagName - Returns the tagname of an element as a string (in uppercase) tagName var x = document.getElementsByName(“firstName”); var element_tag = x[0].tagName; 11
hasChildNodes() - Returns whether an element has any child elements hasChildNodes() var x = document.getElementById(“admin”); var isParent = x.hasChildNodes(); toString() - Converts an element to a string toString() var x = document.getElementById(“admin”); var sub_menues = x.toString(); attributes[] - Returns an array of the attributes of an element attributes[] var x = document.getElementById(“admin”); var all_attributes = x.attributes[]; childNodes[] - Returns an array of child nodes for an element childNodes[] var x = document.getElementById(“admin”); var sub_menues = x.childNodes[]; 12