2015-09-17 31 views
7

XCTest FrameworkからXcode 7 GMまでの新しいXcode UI Testingを使用しています。私は簡単なのUIWebViewでアプリを持っている(これは、Webビューとボタンでちょうどナビゲーションコントローラ+ビューコントローラです)、私は次のシナリオを確認したい:Xcode UIテストを使用したUIWebViewのテスト

  1. のWebビューのロードページをwww.example.com
  2. ユーザーがボタンをタップします
  3. Web表示は、URLでいくつかのページをロードします:www.example2.com

私はボタンを押した後のUIWebViewにロードされたページをチェックしたいです。これはUIテストで可能ですか?

は、実は私はこのようなウェブビューを取得しています:

let app:XCUIApplication = XCUIApplication() 
let webViewQury:XCUIElementQuery = app.descendantsMatchingType(.WebView) 
let webView = webViewQury.elementAtIndex(0) 

答えて

6

あなたが表示されている実際のURLのように、ロードされるページを伝えることができなくなります。ただし、アサーションの内容が画面に表示されていることを確認することはできます。 UIテストではlinks that works great with both UIWebView and WKWebViewXCUIElementQueryが提供されています。

ページが同期して読み込まれないので、wait for the actual elements to appearにする必要があります。

let app = XCUIApplication() 
app.launch() 

app.buttons["Go to Google.com"].tap() 

let about = self.app.staticTexts["About"] 
let exists = NSPredicate(format: "exists == 1") 
expectationForPredicate(exists, evaluatedWithObject: about, handler: nil) 

waitForExpectationsWithTimeout(5, handler: nil) 
XCTAssert(about.exists) 

XCTAssert(app.staticTexts["Google Search"].exists) 
app.links["I'm Feeling Lukcy"].tap() 

working test hostは、あなたがコードに掘るしたい場合は二つのリンクと一緒に行くこともあります。

3

ページのタイトルが異なる場合は、ウェブページのタイトルを確認できます。

let app = XCUIApplication() 
app.launch() 
//Load www.example.com 

//Tap on some button  
app.links["Your button"].tap() 

//Wait for www.example2.com to load 
let webPageTitle = app.otherElements["Example2"] 
let exists = NSPredicate(format: "exists == 1") 
expectationForPredicate(exists, evaluatedWithObject: webPageTitle, handler: nil) 
waitForExpectationsWithTimeout(5, handler: nil) 
+0

残念ながら、私は1つのタイトルを持っています。しかし、多分誰かが別の状況になるでしょう。ありがとう! – Apan

関連する問題