Selenium and JavaScript
SeleniumLevel 3
By Shen Dongliang
Http://blog.csdn.net/shendl/
An all-round explanation of selenium -- one of the simple explanations of Selenium
Selenium Chinese Reference Manual -- selenium II
《SeleniumDeep Dive into the bookPDFFor the full text of the format, seeHttp://code.google.com/p/shendl/downloads/list
Selenium and ext
N ext uses js to dynamically generate HTML and CSS code.
N if an ID is not specified for the ext element, the auto-increment ID is used. The id value cannot be determined.
N if the interface element of ext changes, the ID also changes.
The N selenium recording tool cannot locate the web interface generated by Ext.
The root cause is that selenium needs to locate HTML elements, while many HTML elements of ext are generated by Ext. The generated element IDs are automatically created by Ext.
Solution:
N ext assigns an ID for each component
N, but some HTML elements cannot specify the ID.
N selenium recording tool assists with manual Writing of test code
N selenium can use JS Code to operate ext objects on the user interface.
You can use the geteval method of the selenium interface to execute js code in the browser.
Selenium Interface
java.lang.String getEval(java.lang.String script)
Method to dynamically execute JavaScript code.
Storeeval (
Script, variablename)
Arguments:
·
Script-the Javascript snippet
To run
·
Variablename-the name of a Variable
In which the result is to be stored.
Returns:
The results
Evaluating the snippet
The result is stored in the JS variable name.
Notes for dynamic JavaScript Code Execution
By calling the geteval () method of the selenium interface, you can directly send the javascrip code in Java to the browser for execution.
However, what is actually executed here is not the standard Javascript eval () method. In the geteval () method, this is the selenium object in Javascript, not the window object.
If you need to call the method of the window object on the current page, you need to directly point out the window object, such as: Too Many Rows Doc ument. getelementbyid ('foo ');".
In the geteval () method, this is the selenium object in JavaScript. Note that this is not the selenium interface in Java, but the selenium object in JavaScript. The selenium object in Javascript is used to execute a series of commands on behalf of the selenium interface in Java, allowing the browser to execute.
In addition, the selectframe and selectwindow methods of the selenium interface can modify the target window of the current selenium command. That is to say, the current window object will change.
Example of selenium executing Ext:
Protected selenium browser;
......
This. Browser. geteval ("window. Frames ['20140901']. Window. Ext. getcmp ('testid-09'). setvalue ('should be strictly enforced! ');");
Note:
Window is the target window of the current selenium command. The current window can be modified through the selectframe and selectwindow methods of the selenium interface!
Example of dynamic JavaScript Execution by Selenium:
You can also use this method to inject Javascript code to the page in Java, such:
Rownumstr =This. Browser
. Geteval ("(function (){"
+ "Var
Tablelength = 0 ;"
+ "Var
Tables?##frames='iframe_0030020.00000000003'0000.00000000doc ument. getelementsbytagname ('table ');
"
+ "For (VAR
J = 0; j <tables. length; j ++ ){"
+"
If (tables [J]. classname = 'x-grid3-row-table '){"
+ "Tablelength ++ ;"
+"
If (tables [J]. childnodes [0]. childnodes [0]. childnodes [1]. childnodes [0]. innertext = '"
+
Complainidvalue + "'){"
+ "Return tablelength;" +"
} "+"
}"
+ "}" + "Return-1;" + "})();");
SeleniumThis is the end of the article. Readers should be able to fully control selenium through this articleThis unique webThe Interface Test tool is ready.