During the daily Web UI Automation testing process, get or navigate to the specified page, if the element or the specified range is not in the browser display area, then through the screenshot will not be able to obtain the corresponding information, but wasted the fearless image server resources, of course, The most important is not to achieve their own expectations, is not very internal injuries ah. At this point, we have to do the same as normal user operation, by scrolling the page to the appropriate area, then how to scroll, this article gives an answer.
This article implements the page scrolling, is through the JS operation realization, please the small master to see. If there are deficiencies, please the great god, thank you very much!
Directly on the yards ...
1 /**2 * Scroll screen by JavaScript3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.sele.core Seleniumcore.java scrollscreen, 2015-7-26 10:58:14 Exp $6 * 7 * @paramTop:top Distance8 * @paramLeft:left Distance9 */Ten Public voidScrollscreen (intLeftinttop) { One //Note Declare DOCTYPE AString Js_body = "Document.body.scrollTop =" + Top + "; Document.body.scrollLeft = "+Left ; - //Declare DOCTYPE -String JS_DOCU = "Document.documentElement.scrollTop =" + Top + "; Document.documentElement.scrollLeft = "+Left ; the - //decide which the script would be an execute by browser type - Switch( This. Getbrowsertype ()) { - Case"Chrome": + This. EXECJS (js_body); - Break; + Case"Firefox": A Case"Internetexplorerdriver": at Case"Safari": - This. EXECJS (JS_DOCU); - Break; - default: - This. EXECJS (JS_DOCU); - Break; in } -}
For a method of obtaining a browser type in the above code Getbrowsertype, please refer to the previous post WebUI automated combat instance -017-get browser type
The method for executing JavaScript that is involved in the above code is EXECJS as follows:
1 /**2 * Execute JScript3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.sele.core Seleniumcore.java execjavascript, 2015-6-17 00:25:38 Exp $6 * 7 * @paramJScript8 */9 Public voidexecjs (String JScript) {Ten((Javascriptexecutor) This. Webdriver). Executescript (JScript); One}
At this point, theWebUI automation function test script section 025-javascript in the Selenium Automation application example of three (page scrolling, simulation mouse drag scroll bar) successfully completed, I hope this article can give beginners Selenium A reference for you.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
SELENIUM2 Learning -027-webui Automation Practical Case -025-javascript Application example in Selenium Automation (page scrolling, simulation mouse drag scroll bar)