2016-12-31 1 views
0

テストランナーとしてSeleniumとSelenium API用にwebdriver.io javascriptライブラリーとの統合テストを実行します。 私のテストは次のようになります: htmlページを読み込んでボタンをクリックします。私はGet REST呼び出しが呼び出されたかどうか確認したい。SeleniumでREST呼び出しをキャプチャします

webdriver.ioのプラグインが私の要件に合うようになっているwebdriverajaxと呼ばれていましたが、うまくいきません。

どのようにレストコールをキャプチャするのですか?

答えて

0

これは、セレンコードから外れているカスタムHttpClientクラスを使用することで実現できます。セレンはこの機能をサポートしていません。

URLがアクセス可能であるか、あなたの場合はあなたが決めることができnot.Thenどうかを確認するために、カスタムコードを使用することができますelement.ThenあなたはそれがRESTサービスを呼び出しますボタンをクリックしたときに、URLがHTMLDOMからグラブすることができたとテストは、status codeまたはその他のメカニズムに基づいて合格または不合格です。

FileDownloader.java(サンプルコードスニペット)

private String downloader(WebElement element, String attribute) throws IOException, NullPointerException, URISyntaxException { 
     String fileToDownloadLocation = element.getAttribute(attribute); 
     if (fileToDownloadLocation.trim().equals("")) throw new NullPointerException("The element you have specified does not link to anything!"); 

     URL fileToDownload = new URL(fileToDownloadLocation); 
     File downloadedFile = new File(this.localDownloadPath + fileToDownload.getFile().replaceFirst("/|\\\\", "")); 
     if (downloadedFile.canWrite() == false) downloadedFile.setWritable(true); 

     HttpClient client = new DefaultHttpClient(); 
     BasicHttpContext localContext = new BasicHttpContext(); 

     LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); 
     if (this.mimicWebDriverCookieState) { 
      localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(this.driver.manage().getCookies())); 
     } 

     HttpGet httpget = new HttpGet(fileToDownload.toURI()); 
     HttpParams httpRequestParameters = httpget.getParams(); 
     httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, this.followRedirects); 
     httpget.setParams(httpRequestParameters); 

     LOG.info("Sending GET request for: " + httpget.getURI()); 
     HttpResponse response = client.execute(httpget, localContext); 
     this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); 
     LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); 
     LOG.info("Downloading file: " + downloadedFile.getName()); 
     FileUtils.copyInputStreamToFile(response.getEntity().getContent(), downloadedFile); 
     response.getEntity().getContent().close(); 

     String downloadedFileAbsolutePath = downloadedFile.getAbsolutePath(); 
     LOG.info("File downloaded to '" + downloadedFileAbsolutePath + "'"); 

     return downloadedFileAbsolutePath; 
    } 

TestClass.java

@Test 
public void downloadAFile() throws Exception { 


     FileDownloader downloadTestFile = new FileDownloader(driver); 
     driver.get("http://www.localhost.com/downloadTest.html"); 
     WebElement downloadLink = driver.findElement(By.id("fileToDownload")); 
     String downloadedFileAbsoluteLocation = downloadTestFile.downloadFile(downloadLink); 

     assertThat(new File(downloadedFileAbsoluteLocation).exists(), is(equalTo(true))); 
     assertThat(downloadTestFile.getHTTPStatusOfLastDownloadAttempt(), is(equalTo(200))); 
// you can use status code to valid the REST URL 
    } 

Hereが基準です。

注:これはあなたの要件には厳密には適合しないかもしれませんが、あなたの要件に合うように考えて修正することができます。

BrowserMob Proxyも参照してください。これを使用しても、あなたが望むものを達成することができます。

0

問題はwebdriver.ioバージョンでした。どうやら、webdriverajaxはwebdriver.io v3.xだけでうまく動作しますが、v4.xではうまく動作しません。私はv4.5.2を使用しています。

次のように私は、プラグインを使用しないことを決定し、window.XMLHttpRequest オープンのためのモックを実装し、メソッドを送信します。

proxyXHR() { 
    this.browser.execute(() => { 
    const namespace = '__scriptTests'; 
    window[namespace] = { open: [], send: [] }; 
    const originalOpen = window.XMLHttpRequest.prototype.open; 
    window.XMLHttpRequest.prototype.open = function (...args) { 
     window[namespace].open.push({ 
     method: args[0], 
     url: args[1], 
     async: args[2], 
     user: args[3], 
     password: args[4] 
     }); 
     originalOpen.apply(this, [].slice.call(args)); 
    }; 
    window.XMLHttpRequest.prototype.send = function (...args) { 
     window[namespace].send.push(JSON.parse(args[0])); 
    }; 
    }); 
} 

getXHRsInfo() { 
    const result = this.browser.execute(() => { 
    const namespace = '__scriptTests'; 
    return window[namespace]; 
    }); 
    return result.value; 
} 
関連する問題