Commit 7e659c27 authored by Eric Noyau's avatar Eric Noyau Committed by Commit Bot

Introducing an element selector for tests.

There are a number of ways for selecting an element in a tested html page, and those involve
injecting javascript. This class is intended to be used for abstracting this js generation
to diminish the proliferation of ad-hoc code.

Bug: None
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: Ifbe1ba8be509dfe3ab02ff8b7ddca6cefc3761ff
Reviewed-on: https://chromium-review.googlesource.com/1118160
Commit-Queue: Eric Noyau <noyau@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#571069}
parent eac113a7
...@@ -28,6 +28,8 @@ source_set("test") { ...@@ -28,6 +28,8 @@ source_set("test") {
allow_circular_includes_from = [ "//ios/web/test:test_support" ] allow_circular_includes_from = [ "//ios/web/test:test_support" ]
sources = [ sources = [
"element_selector.h",
"element_selector.mm",
"error_test_util.h", "error_test_util.h",
"error_test_util.mm", "error_test_util.mm",
"js_test_util.h", "js_test_util.h",
......
// Copyright 2018 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_ELEMENT_SELECTOR_H_
#define IOS_WEB_PUBLIC_TEST_ELEMENT_SELECTOR_H_
#include <string>
namespace web {
namespace test {
// An ElementSelector is used to generate the proper javascript to retrieve an
// element on a web page. It encapsulates the various means of finding an
// element and is intended to be passed around.
class ElementSelector {
public:
// Returns an ElementSelector to retrieve an element by ID.
static const ElementSelector ElementSelectorId(const std::string element_id);
// Returns an ElementSelector to retrieve an element by a CSS selector.
static const ElementSelector ElementSelectorCss(
const std::string css_selector);
// Returns an ElementSelector to retrieve an element by a xpath query.
static const ElementSelector ElementSelectorXPath(
const std::string xpath_selector);
ElementSelector(const ElementSelector& that) = default;
ElementSelector(ElementSelector&& that) = default;
~ElementSelector() = default;
// Returns the javascript to invoke on a page to retrieve the element.
const std::string GetSelectorScript() const;
// Return a human readable description of the query.
const std::string GetSelectorDescription() const;
private:
ElementSelector(const std::string&& script, const std::string&& description);
const std::string script_;
const std::string description_;
};
} // namespace test
} // namespace web
#endif // IOS_WEB_PUBLIC_TEST_ELEMENT_SELECTOR_H_
// Copyright 2018 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.
#include "ios/web/public/test/element_selector.h"
#include "base/strings/stringprintf.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace web {
namespace test {
// Static.
const ElementSelector ElementSelector::ElementSelectorId(
const std::string element_id) {
return ElementSelector(
base::StringPrintf("document.getElementById('%s')", element_id.c_str()),
base::StringPrintf("with ID '%s')", element_id.c_str()));
}
// Static.
const ElementSelector ElementSelector::ElementSelectorCss(
const std::string css_selector) {
const std::string script(base::StringPrintf("document.querySelector(\"%s\")",
css_selector.c_str()));
const std::string description(
base::StringPrintf("with CSS selector '%s')", css_selector.c_str()));
return ElementSelector(std::move(script), std::move(description));
}
// Static.
const ElementSelector ElementSelector::ElementSelectorXPath(
const std::string xpath_selector) {
const std::string script(base::StringPrintf(
"document.evaluate(`%s`, document, "
"null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue",
xpath_selector.c_str()));
const std::string description(
base::StringPrintf("with xpath '%s')", xpath_selector.c_str()));
return ElementSelector(std::move(script), std::move(description));
}
ElementSelector::ElementSelector(const std::string&& script,
const std::string&& description)
: script_(script), description_(description) {}
const std::string ElementSelector::GetSelectorScript() const {
return script_;
}
const std::string ElementSelector::GetSelectorDescription() const {
return description_;
}
} // namespace test
} // namespace web
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