Selenium to get the JavaScript return value is very simple, just need to return the data in the JS script can be returned, and then through the method to return JS execution results, the method source code is as follows:
1 /**2 * Get Object of return from JS3 * 4 * @authorAARON.FFP5 * @versionV1.0.0:autoseleniumdemo main.aaron.sele.core Seleniumcore.java execjsr, 2015-8-9 1:39:17 Exp $6 * 7 * @paramJs:return_js8 * 9 * @returnObjectTen */ One PublicObject EXECJSR (String js) { A return((Javascriptexecutor) This. Webdriver). Executescript (JS); -}
The test steps are summarized as follows:
- Start the Chrome browser
- Open the homepage of Yi Xun net
- Executes a JavaScript script and gets the return value
- Close page
- Exit Webdriver
The test source for the method is as follows:
1 /**2 * AARON.FFP Inc.3 * Copyright (c) 2004-2015 All rights Reserved.4 */5 PackageMain.aaron.demo.javascript;6 7 ImportMain.aaron.sele.core.SeleniumCore;8 9 ImportOrg.openqa.selenium.chrome.ChromeDriver;Ten ImportOrg.testng.annotations.AfterClass; One ImportOrg.testng.annotations.BeforeClass; A Importorg.testng.annotations.Test; - - /** the * - * @authorAARON.FFP - * @versionV1.0.0:autoseleniumdemo main.aaron.demo.javascript Jquery.java, 2015-7-27 13:31:31 Exp $ - */ + Public classJqueryextendsseleniumcore{ -String JQ = "Webelement = $ ('. Btn-cor-1 ') [0];" + +"Return webelement.offsettop + '; ' + Webelement.offsetleft + '; ' +" + A"Webelement.offsetheight + '; ' + webelement.offsetwidth"; atString baseUrl = "http://www.yixun.com/"; - - @BeforeClass - Public voidBeforeclass ()throwsinterruptedexception{ - This. Webdriver =Newchromedriver (); - This. Webdriver.manage (). window (). Maximize (); in This. Webdriver.get (BASEURL); -Thread.Sleep (5000); to } + - @AfterClass the Public voidAfterclass () { * This. Webdriver.close (); $ This. Webdriver.quit ();Panax Notoginseng } - the /** + * Get Return of Execute JavaScript A * the * @authorAARON.FFP + * @versionV1.0.0:autoseleniumdemo main.aaron.demo.javascript Jquery.java test_jsreturn, 2015-8-8 15:59:51 Exp $ - * $ */ $ @Test - Public voidTest_jsreturn () { -System.out.println ("\nstart test_jsreturn ..."); theSystem.out.println ( This. EXECJSR (JQ)); - }Wuyi}
The execution results are as follows:
Start Test_jsreturn ... 717;1396;30;100
At this point, theWebUI automation function test script section 034-javascript in the Selenium Automation application Example six (get the JS execution result return value) 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 -036-webui Automation Practical instance -034-javascript in Selenium Automation application example of six (get JS execution result return value)