2012-11-19 29 views
27

executeAsyncScript और executeScript के बीच क्या अंतर है? मैं window.onload जैसे ईवेंट का उपयोग कैसे कर सकता हूं? मैं इसWebDriver executeAsyncScript बनाम executeScript

((JavascriptExecutor) driver).executeAsyncScript("window.onload = function() {alert('Hello')}"); 

की तरह कुछ करने की कोशिश की लेकिन निश्चित रूप से यह काम नहीं किया ... तो अगर किसी को काम करता है कि यह कैसे एक उदाहरण

+6

उन के बीच मुख्य अंतर async के साथ निष्पादित स्क्रिप्ट स्पष्ट रूप से संकेत करना चाहिए कि वे प्रदान की कॉलबैक लागू तक समाप्त हो जाता है। यह कॉलबैक हमेशा निष्पादित कार्य में अंतिम तर्क के रूप में इंजेक्शन दिया जाता है। – sphair

+0

आपकी प्रतिक्रिया के लिए धन्यवाद! – vispart

+0

@ एसफेयर आपको एक उत्तर सबमिट करना चाहिए था, इसके संक्षिप्त और सही, नीचे दिए गए कुछ कचरे के माध्यम से निकलना मुश्किल था। – Pykler

उत्तर

21

मैं executeScript का उपयोग लिखें। उदाहरण प्रदान की है: अलर्ट वहाँ मुद्दे जाना जाता है पर

String cssSelector="...blablabla..."; 
JavascriptExecutor js = (JavascriptExecutor) driver; 
StringBuilder stringBuilder = new StringBuilder(); 
stringBuilder.append("document.getElementById(\'"+cssSelector +"\').click();"); 
js.executeScript(stringBuilder.toString()); 

के संबंध में विवरण। आपको जानकारी प्राप्त कर सकते हैं here

प्रलेखन अंतर के अनुसार है:

executeScript

public java.lang.Object executeScript(java.lang.String script, 
          java.lang.Object... args) 

Description copied from interface: JavascriptExecutor Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function. Within the script, use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist. If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:

  • For an HTML element, this method returns a WebElement
  • For a decimal, a Double is returned
  • For a non-decimal number, a Long is returned
  • For a boolean, a Boolean is returned
  • For all other cases, a String is returned.
  • For an array, return a List with each object following the rules above. We support nested lists.
  • Unless the value is null or there is no return value, in which null is returned

Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"

Specified by: executeScript in interface JavascriptExecutor Parameters: script - The JavaScript to execute args - The arguments to the script. May be empty Returns: One of Boolean, Long, String, List or WebElement. Or null.

executeAsyncScript

public java.lang.Object executeAsyncScript(java.lang.String script, 
            java.lang.Object... args) 

Description copied from interface: JavascriptExecutor Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument. The first argument passed to the callback function will be used as the script's result. This value will be handled in the same way as the synchronous case.

Example #1: Performing a sleep in the browser under test.

long start = System.currentTimeMillis(); 
    ((JavascriptExecutor) driver).executeAsyncScript(
     "window.setTimeout(arguments[arguments.length - 1], 500);"); 
    System.out.println(
     "Elapsed time: " + (System.currentTimeMillis() - start)); 

Example #2: Synchronizing a test with an AJAX application:

WebElement composeButton = driver.findElement(By.id("compose-button")); 
    composeButton.click(); 
    ((JavascriptExecutor) driver).executeAsyncScript(
     "var callback = arguments[arguments.length - 1];" + 
     "mailClient.getComposeWindowWidget().onload(callback);"); 
    driver.switchTo().frame("composeWidget"); 
    driver.findElement(By.id("to")).sendKeys("[email protected]"); 

Example #3: Injecting a XMLHttpRequest and waiting for the result:

Object response = ((JavascriptExecutor) driver).executeAsyncScript(
     "var callback = arguments[arguments.length - 1];" + 
     "var xhr = new XMLHttpRequest();" + 
     "xhr.open('GET', '/resource/data.json', true);" + 
     "xhr.onreadystatechange = function() {" + 
     " if (xhr.readyState == 4) {" + 
     " callback(xhr.responseText);" + 
     " }" + 
     "}" + 
     "xhr.send();"); 
    JSONObject json = new JSONObject((String) response); 
    assertEquals("cheese", json.getString("food")); 

Script arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" variable.

Specified by: executeAsyncScript in interface JavascriptExecutor Parameters: script - The JavaScript to execute. args - The arguments to the script. May be empty. Returns: One of Boolean, Long, String, List, WebElement, or null.

विस्तृत प्रलेखन here

+5

उपरोक्त "विवरण" (इंटरफ़ेस दस्तावेज से कॉपी किया गया) और लिंक किए गए दस्तावेज़ पुराने हैं। "वस्तुओं" की एक सूची सूची <मानचित्र <स्ट्रिंग, ऑब्जेक्ट >> के रूप में वापस कर दी जाएगी। कहां, प्रत्येक कुंजी वस्तु की एक संपत्ति है। मुझे नहीं पता कि इसका कोई विवरण कैसे प्राप्त करें, लेकिन मुझे इसे पढ़ने के बाद जला दिया गया, इसलिए मैंने सोचा कि मैं योगदान दूंगा। – dmansfield

+0

@ eugene.polschikov मैं आपके उदाहरण का जिक्र कर रहा हूं 3 और जब मैं कंसोल में लाइन निष्पादित करता हूं तो 'अनकॉट रेफरेंस एरर: : 1: 16' पर तर्कों को परिभाषित नहीं किया गया है। जेएस के लिए पूरी तरह से नया हूँ और आपकी मदद की सराहना की है। –

2
((JavascriptExecutor) driver).executeScript("alert('Hello');"); 

चेतावनी दिखाएगा:

((JavascriptExecutor) driver).executeAsyncScript() is used when the JS takes time to execute e.g.in a Web Service call.

window.onload सुनिश्चित करती है जेएस निष्पादित किया गया है जब पृष्ठ पूरी तरह से लोड हो जाता है।

5

उन के बीच मुख्य अंतर async के साथ निष्पादित स्क्रिप्ट स्पष्ट रूप से संकेत करना चाहिए कि वे प्रदान की कॉलबैक लागू तक समाप्त हो जाता है। यह कॉलबैक हमेशा निष्पादित कार्य में अंतिम तर्क के रूप में इंजेक्शन दिया जाता है।

समारोह executeAsyncScript के साथ आमंत्रण को अंतिम तर्क के रूप में एक 'किया कॉलबैक' लेता है:

+0

गरीब दस्तावेज ऐसा क्यों नहीं कहते हैं? दस्तावेज़ीकरण केवल तभी कहता है: "वर्तमान में चयनित फ्रेम या विंडो के संदर्भ में जावास्क्रिप्ट को असंकालिक रूप से निष्पादित करता है।" – Elmue

13

(। यह सरल, और सही रखते हुए)

execteScript और executeAsyncScript के बीच प्रासंगिक अंतर यह है है, जो संकेत देने के लिए कि स्क्रिप्ट पर कार्य किया जाता है बुलाया जाना चाहिए।

यह कोड के साथ उपयोग करने की अनुमति देता है जो कॉलबैक का उपयोग होने पर केवल 'समाप्त' होता है - उदाहरण के लिए। सेटटाइमआउट या एसिंक्रोनस एक्सएचआर। यदि टाइमआउट सीमा के भीतर 'किया गया कॉलबैक' नहीं कहा जाता है तो लौटाया गया वादा खारिज कर दिया जाएगा।

Unlike executing synchronous JavaScript with #executeScript, scripts executed with [#executeAsyncScript] must explicitly signal they are finished by invoking the provided callback. This callback will always be injected into the executed function as the last argument..

है, दोनों कार्यों WebDriver प्रवाह नियंत्रण जब तक वे पूरा ब्लॉक - या तो executeScript के लिए कोड के अंत चल रहे हैं या जब executeAsyncScript साथ 'किया कॉलबैक' बुला: में "async" नाम सिग्नल तंत्र का उपयोग करता है और इसका मतलब यह नहीं है कि जावास्क्रिप्ट कोड वास्तव में WebDriver के संबंध में असीमित रूप से निष्पादित किया जाता है।

1

मैंने इस समारोह को करने के लिए बहुत समय लगाया और अंततः मुझे यह मिला। follwing कोड बहुत मदद मिलेगी:

/** 
* executeAsyncScript document mentioned callback is a browser intrinsic function for returning deferred value (e.g 123 in example) from 
* js environment to Java environment 
* 
*/ 
@Test 
public void testAsyncScript() { 
    webDriver.manage().timeouts().setScriptTimeout(1, TimeUnit.SECONDS); 
    Integer a = 23; 
    TestUtil.elapse("first",() -> { 
     Object value = getJsExecutor().executeAsyncScript("window.setTimeout(arguments[arguments.length - 1](123), 500);", a); 
     // following code should be executed after 500ms timeout 
     System.out.println("a is " + a); // a has nothing to do with the documented "callback" 
     assertEquals(123, value); 
    }); 

} 
संबंधित मुद्दे