Selenium2學習-036-WebUI自動化實戰執行個體-034-JavaScript 在 Selenium 自動化中的應用執行個體之六(擷取 JS 執行結果傳回值)

來源:互聯網
上載者:User

標籤:

Selenium 擷取 JavaScript 傳回值非常簡單,只需要在 js 指令碼中將需要返回的資料 return 就可以,然後通過方法返回 js 的執行結果,方法源碼如下所示:

 1     /** 2      * Get Object of return from js 3      *  4      * @author Aaron.ffp 5      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java execJSR, 2015-8-9 1:39:17 Exp $ 6      *  7      * @param js : return_js 8      *  9      * @return Object10      */11     public Object execJSR(String js){12         return ((JavascriptExecutor)this.webdriver).executeScript(js);13     }

測試步驟概要如下所示:

  1. 啟動 Chrome 瀏覽器
  2. 開啟易迅網首頁
  3. 執行 JavaScript 指令碼,並擷取傳回值
  4. 關閉頁面
  5. 退出 WebDriver

方法的測試源碼如下所示:

 1 /** 2  * Aaron.ffp Inc. 3  * Copyright (c) 2004-2015 All Rights Reserved. 4  */ 5 package main.aaron.demo.javascript; 6  7 import main.aaron.sele.core.SeleniumCore; 8  9 import org.openqa.selenium.chrome.ChromeDriver;10 import org.testng.annotations.AfterClass;11 import org.testng.annotations.BeforeClass;12 import org.testng.annotations.Test;13 14 /**15  * 16  * @author Aaron.ffp17  * @version V1.0.0: autoSeleniumDemo main.aaron.demo.javascript JQuery.java, 2015-7-27 13:31:31 Exp $18  */19 public class JQuery extends SeleniumCore{20     String jq = "webelement = $(‘.btn-cor-1‘)[0]; " + 21                 "return webelement.offsetTop + ‘;‘ + webelement.offsetLeft + ‘;‘ + " + 22                 "       webelement.offsetHeight + ‘;‘ + webelement.offsetWidth";23     String baseUrl = "http://www.yixun.com/";24     25     @BeforeClass26     public void beforeClass() throws InterruptedException{27         this.webdriver = new ChromeDriver();28         this.webdriver.manage().window().maximize();29         this.webdriver.get(baseUrl);30         Thread.sleep(5000);31     }32     33     @AfterClass34     public void afterClass(){35         this.webdriver.close();36         this.webdriver.quit();37     }38     39     /**40      * Get return of execute JavaScript41      * 42      * @author Aaron.ffp43      * @version V1.0.0: autoSeleniumDemo main.aaron.demo.javascript JQuery.java test_jsReturn, 2015-8-8 15:59:51 Exp $44      *45      */46     @Test47     public void test_jsReturn(){48         System.out.println("\nStart test_jsReturn ...");49         System.out.println(this.execJSR(jq));50     }51 }

執行結果如下所示:

Start test_jsReturn ...717;1396;30;100

 

至此,Web使用者介面自動化功能測試指令碼第 034-JavaScript 在 Selenium 自動化中的應用執行個體之六(擷取 JS 執行結果傳回值) 順利完結,希望此文能夠給初學 Selenium 的您一份參考。

最後,非常感謝親的駐足,希望此文能對親有所協助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^

 

Selenium2學習-036-WebUI自動化實戰執行個體-034-JavaScript 在 Selenium 自動化中的應用執行個體之六(擷取 JS 執行結果傳回值)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.