Commit 525bd4bb authored by eugenebut's avatar eugenebut Committed by Commit bot

[ios] Implemented -[ShellEarlGrey loadURL:].

Unlike web::shell_test_util::LoadUrl this new method waits until the
load is completed.

BUG=646544

Review-Url: https://codereview.chromium.org/2340533004
Cr-Commit-Position: refs/heads/master@{#418604}
parent 169fcec5
......@@ -337,6 +337,8 @@ source_set("earl_grey_test_support") {
]
sources = [
"public/test/earl_grey/js_test_util.h",
"public/test/earl_grey/js_test_util.mm",
"public/test/earl_grey/web_view_actions.h",
"public/test/earl_grey/web_view_actions.mm",
"public/test/earl_grey/web_view_matchers.h",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_TEST_EARL_GREY_JS_TEST_UTIL_H_
#define IOS_WEB_PUBLIC_TEST_EARL_GREY_JS_TEST_UTIL_H_
#import <Foundation/Foundation.h>
#import "ios/web/public/web_state/web_state.h"
namespace web {
// Waits until the Window ID has been injected and the page is thus ready to
// respond to JavaScript injection. Fails with a GREYAssert on timeout or if
// unrecoverable error (such as no web view) occurs.
void WaitUntilWindowIdInjected(WebState* web_state);
// Executes |javascript| on the given |web_state|, and waits until execution is
// completed. If |out_error| is not nil, it is set to the error resulting from
// the execution, if one occurs. The return value is the result of the
// JavaScript execution. If the script execution is timed out, then this method
// fails with a GREYAssert.
id ExecuteJavaScript(WebState* web_state,
NSString* javascript,
NSError** out_error);
} // namespace web
#endif // IOS_WEB_PUBLIC_TEST_EARL_GREY_JS_TEST_UTIL_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/test/earl_grey/js_test_util.h"
#import <EarlGrey/EarlGrey.h>
#import <WebKit/WebKit.h>
#include "base/timer/elapsed_timer.h"
#import "ios/testing/earl_grey/wait_util.h"
#import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
namespace web {
void WaitUntilWindowIdInjected(WebState* web_state) {
bool is_window_id_injected = false;
bool is_timeout = false;
bool is_unrecoverable_error = false;
base::ElapsedTimer timer;
base::TimeDelta timeout =
base::TimeDelta::FromSeconds(testing::kWaitForJSCompletionTimeout);
// Keep polling until either the JavaScript execution returns with expected
// value (indicating that Window ID is set), the timeout occurs, or an
// unrecoverable error occurs.
while (!is_window_id_injected && !is_timeout && !is_unrecoverable_error) {
NSError* error = nil;
id result = ExecuteJavaScript(web_state, @"0", &error);
if (error) {
is_unrecoverable_error = ![error.domain isEqual:WKErrorDomain] ||
error.code != WKErrorJavaScriptExceptionOccurred;
} else {
is_window_id_injected = [result isEqual:@0];
}
is_timeout = timeout < timer.Elapsed();
}
GREYAssertFalse(is_timeout, @"windowID injection timed out");
GREYAssertFalse(is_unrecoverable_error, @"script execution error");
}
id ExecuteJavaScript(WebState* web_state,
NSString* javascript,
NSError** out_error) {
__block BOOL did_complete = NO;
__block id result = nil;
CRWJSInjectionReceiver* receiver = web_state->GetJSInjectionReceiver();
[receiver executeJavaScript:javascript
completionHandler:^(id value, NSError* error) {
did_complete = YES;
result = [value copy];
if (out_error)
*out_error = [error copy];
}];
// Wait for completion.
GREYCondition* condition = [GREYCondition
conditionWithName:@"Wait for JavaScript execution to complete."
block:^{
return did_complete;
}];
GREYAssert([condition waitWithTimeout:testing::kWaitForJSCompletionTimeout],
@"Script execution timed out");
if (out_error)
[*out_error autorelease];
return [result autorelease];
}
} // namespace web
......@@ -69,6 +69,8 @@ source_set("earl_grey_test_support") {
"earl_grey/shell_actions.mm",
"earl_grey/shell_base_test_case.h",
"earl_grey/shell_base_test_case.mm",
"earl_grey/shell_earl_grey.h",
"earl_grey/shell_earl_grey.mm",
"earl_grey/shell_matchers.h",
"earl_grey/shell_matchers.mm",
]
......
......@@ -14,7 +14,10 @@ namespace shell_test_util {
// ui::PAGE_TRANSITION_TYPED.
void LoadUrl(const GURL& url);
// Returns true if the current page in the current WebState is loading.
bool IsLoading();
} // namespace shell_test_util
} // namespace web
#endif // IOS_WEB_SHELL_TEST_APP_NAVIGATION_TEST_UTIL_H_
\ No newline at end of file
#endif // IOS_WEB_SHELL_TEST_APP_NAVIGATION_TEST_UTIL_H_
......@@ -14,5 +14,9 @@ void LoadUrl(const GURL& url) {
web::test::LoadUrl(GetCurrentWebState(), url);
}
bool IsLoading() {
return GetCurrentWebState()->IsLoading();
}
} // namespace shell_test_util
} // namespace web
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_H_
#define IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_H_
#import <Foundation/Foundation.h>
#include "url/gurl.h"
// Test methods that perform actions on Web Shell. These methods may read or
// alter Web Shell's internal state programmatically or via the UI, but in both
// cases will properly synchronize the UI for Earl Grey tests.
@interface ShellEarlGrey : NSObject
// Loads |URL| in the current WebState with transition of type
// ui::PAGE_TRANSITION_TYPED, and waits for the page to complete loading, or
// a timeout.
+ (void)loadURL:(const GURL&)URL;
@end
#endif // IOS_WEB_SHELL_TEST_EARL_GREY_SHELL_EARL_GREY_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/shell/test/earl_grey/shell_earl_grey.h"
#import <EarlGrey/EarlGrey.h>
#import "ios/web/public/test/earl_grey/js_test_util.h"
#include "ios/web/shell/test/app/navigation_test_util.h"
#import "ios/web/shell/test/app/web_shell_test_util.h"
namespace {
const NSTimeInterval kWaitForPageLoadTimeout = 10.0;
} // namespace
@implementation ShellEarlGrey
+ (void)loadURL:(const GURL&)URL {
web::shell_test_util::LoadUrl(URL);
// Make sure that the page started loading.
GREYAssert(web::shell_test_util::IsLoading(), @"Page did not start loading.");
GREYCondition* condition =
[GREYCondition conditionWithName:@"Wait for page to complete loading."
block:^BOOL {
return !web::shell_test_util::IsLoading();
}];
GREYAssert([condition waitWithTimeout:kWaitForPageLoadTimeout],
@"Page did not complete loading.");
web::WebState* webState = web::shell_test_util::GetCurrentWebState();
if (webState->ContentIsHTML())
web::WaitUntilWindowIdInjected(webState);
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment