1
0
mirror of synced 2026-07-06 16:40:01 +00:00

chore: extract common routines to base class (#666)

This commit is contained in:
Yury Semikhatsky
2021-10-26 17:53:09 -07:00
committed by GitHub
parent bd6ed7bc88
commit 45b141811b
4 changed files with 116 additions and 148 deletions
@@ -1,36 +0,0 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import org.opentest4j.ValueWrapper;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
class AssertionUtils {
static ValueWrapper formatValue(Object value) {
if (value == null || !value.getClass().isArray()) {
return ValueWrapper.create(value);
}
Collection<String> values = asList((Object[]) value).stream().map(e -> e.toString()).collect(Collectors.toList());
String stringRepresentation = "[" + String.join(", ", values) + "]";
return ValueWrapper.create(value, stringRepresentation);
}
}
@@ -0,0 +1,73 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.ValueWrapper;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
class AssertionsBase {
final LocatorImpl actualLocator;
final boolean isNot;
AssertionsBase(LocatorImpl actual, boolean isNot) {
this.actualLocator = actual;
this.isNot = isNot;
}
void expectImpl(String expression, ExpectedTextValue textValue, Object expected, String message, FrameExpectOptions options) {
expectImpl(expression, asList(textValue), expected, message, options);
}
void expectImpl(String expression, List<ExpectedTextValue> expectedText, Object expected, String message, FrameExpectOptions options) {
if (options == null) {
options = new FrameExpectOptions();
}
options.expectedText = expectedText;
options.isNot = isNot;
expectImpl(expression, options, expected, message);
}
void expectImpl(String expression, FrameExpectOptions expectOptions, Object expected, String message) {
FrameExpectResult result = actualLocator.expect(expression, expectOptions);
if (result.matches == isNot) {
Object actual = result.received == null ? null : Serialization.deserialize(result.received);
String log = String.join("\n", result.log);
if (!log.isEmpty()) {
log = "\nCall log:\n" + log;
}
if (expected == null) {
throw new AssertionFailedError(message + log);
}
throw new AssertionFailedError(message + log, formatValue(expected), formatValue(actual));
}
}
private static ValueWrapper formatValue(Object value) {
if (value == null || !value.getClass().isArray()) {
return ValueWrapper.create(value);
}
Collection<String> values = asList((Object[]) value).stream().map(e -> e.toString()).collect(Collectors.toList());
String stringRepresentation = "[" + String.join(", ", values) + "]";
return ValueWrapper.create(value, stringRepresentation);
}
}
@@ -18,36 +18,21 @@ package com.microsoft.playwright.impl;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.assertions.LocatorAssertions;
import org.opentest4j.AssertionFailedError;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import static com.microsoft.playwright.impl.AssertionUtils.formatValue;
import static com.microsoft.playwright.impl.Serialization.serializeArgument;
import static com.microsoft.playwright.impl.Utils.convertViaJson;
import static java.util.Arrays.asList;
public class LocatorAssertionsImpl implements LocatorAssertions {
private final LocatorImpl actual;
private final boolean isNot;
static class HasTextCommonOptions {
public Object expressionArg;
public SerializedArgument expectedValue;
public Double timeout;
public Boolean useInnerText;
public Integer expectedNumber;
}
public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAssertions {
public LocatorAssertionsImpl(Locator locator) {
this(locator, false);
}
private LocatorAssertionsImpl(Locator locator, boolean isNot) {
this.actual = (LocatorImpl) locator;
this.isNot = isNot;
super((LocatorImpl) locator, isNot);
}
@Override
@@ -56,7 +41,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.string = text;
expected.matchSubstring = true;
expected.normalizeWhiteSpace = true;
expectImpl("to.have.text", expected, text, "Locator expected to contain text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.text", expected, text, "Locator expected to contain text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -66,7 +51,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
// expected.regexFlags =
expected.matchSubstring = true;
expected.normalizeWhiteSpace = true;
expectImpl("to.have.text", expected, pattern, "Locator expected to contain text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.text", expected, pattern, "Locator expected to contain text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -79,7 +64,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.normalizeWhiteSpace = true;
list.add(expected);
}
expectImpl("to.contain.text.array", list, strings, "Locator expected to contain text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.contain.text.array", list, strings, "Locator expected to contain text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -92,7 +77,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.normalizeWhiteSpace = true;
list.add(expected);
}
expectImpl("to.contain.text.array", list, patterns, "Locator expected to contain text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.contain.text.array", list, patterns, "Locator expected to contain text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -113,7 +98,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
if (options == null) {
options = new HasAttributeOptions();
}
HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class);
FrameExpectOptions commonOptions = convertViaJson(options, FrameExpectOptions.class);
commonOptions.expressionArg = name;
expectImpl("to.have.attribute", expectedText, expectedValue, "Locator expected to have attribute", commonOptions);
}
@@ -122,14 +107,14 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
public void hasClass(String text, HasClassOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
expected.string = text;
expectImpl("to.have.class", expected, text, "Locator expected to have class", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.class", expected, text, "Locator expected to have class", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void hasClass(Pattern pattern, HasClassOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
expected.regexSource = pattern.pattern();
expectImpl("to.have.class", expected, pattern, "Locator expected to have class", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.class", expected, pattern, "Locator expected to have class", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -140,7 +125,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.string = text;
list.add(expected);
}
expectImpl("to.have.class.array", list, strings, "Locator expected to have class", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.class.array", list, strings, "Locator expected to have class", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -151,7 +136,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.regexSource = pattern.pattern();
list.add(expected);
}
expectImpl("to.have.class.array", list, patterns, "Locator expected to have class", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.class.array", list, patterns, "Locator expected to have class", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -159,7 +144,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
if (options == null) {
options = new HasCountOptions();
}
HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class);
FrameExpectOptions commonOptions = convertViaJson(options, FrameExpectOptions.class);
commonOptions.expectedNumber = count;
List<ExpectedTextValue> expectedText = null;
expectImpl("to.have.count", expectedText, count, "Locator expected to have count", commonOptions);
@@ -183,7 +168,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
if (options == null) {
options = new HasCSSOptions();
}
HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class);
FrameExpectOptions commonOptions = convertViaJson(options, FrameExpectOptions.class);
commonOptions.expressionArg = name;
expectImpl("to.have.css", expectedText, expectedValue, "Locator expected to have CSS", commonOptions);
}
@@ -192,7 +177,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
public void hasId(String id, HasIdOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
expected.string = id;
expectImpl("to.have.id", expected, id, "Locator expected to have ID", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.id", expected, id, "Locator expected to have ID", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -200,7 +185,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
if (options == null) {
options = new HasJSPropertyOptions();
}
HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class);
FrameExpectOptions commonOptions = convertViaJson(options, FrameExpectOptions.class);
commonOptions.expressionArg = name;
commonOptions.expectedValue = serializeArgument(value);
List<ExpectedTextValue> list = null;
@@ -213,7 +198,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.string = text;
expected.matchSubstring = false;
expected.normalizeWhiteSpace = true;
expectImpl("to.have.text", expected, text, "Locator expected to have text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.text", expected, text, "Locator expected to have text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -223,7 +208,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
// Just match substring, same as containsText.
expected.matchSubstring = true;
expected.normalizeWhiteSpace = true;
expectImpl("to.have.text", expected, pattern, "Locator expected to have text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.text", expected, pattern, "Locator expected to have text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -236,7 +221,7 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.normalizeWhiteSpace = true;
list.add(expected);
}
expectImpl("to.have.text.array", list, strings, "Locator expected to have text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.text.array", list, strings, "Locator expected to have text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -249,105 +234,71 @@ public class LocatorAssertionsImpl implements LocatorAssertions {
expected.normalizeWhiteSpace = true;
list.add(expected);
}
expectImpl("to.have.text.array", list, patterns, "Locator expected to have text", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.text.array", list, patterns, "Locator expected to have text", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void hasValue(String value, HasValueOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
expected.string = value;
expectImpl("to.have.value", expected, value, "Locator expected to have value", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.value", expected, value, "Locator expected to have value", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void hasValue(Pattern pattern, HasValueOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
expected.regexSource = pattern.pattern();
expectImpl("to.have.value", expected, pattern, "Locator expected to have value", convertViaJson(options, HasTextCommonOptions.class));
expectImpl("to.have.value", expected, pattern, "Locator expected to have value", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isChecked(IsCheckedOptions options) {
expectTrue("to.be.checked", "Locator expected to be checked", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.checked", "Locator expected to be checked", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isDisabled(IsDisabledOptions options) {
expectTrue("to.be.disabled", "Locator expected to be disabled", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.disabled", "Locator expected to be disabled", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isEditable(IsEditableOptions options) {
expectTrue("to.be.editable", "Locator expected to be editable", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.editable", "Locator expected to be editable", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isEmpty(IsEmptyOptions options) {
expectTrue("to.be.empty", "Locator expected to be empty", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.empty", "Locator expected to be empty", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isEnabled(IsEnabledOptions options) {
expectTrue("to.be.enabled", "Locator expected to be enabled", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.enabled", "Locator expected to be enabled", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isFocused(IsFocusedOptions options) {
expectTrue("to.be.focused", "Locator expected to be focused", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.focused", "Locator expected to be focused", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isHidden(IsHiddenOptions options) {
expectTrue("to.be.hidden", "Locator expected to be hidden", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.hidden", "Locator expected to be hidden", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void isVisible(IsVisibleOptions options) {
expectTrue("to.be.visible", "Locator expected to be visible", convertViaJson(options, HasTextCommonOptions.class));
expectTrue("to.be.visible", "Locator expected to be visible", convertViaJson(options, FrameExpectOptions.class));
}
private void expectImpl(String expression, ExpectedTextValue textValue, Object expected, String message, HasTextCommonOptions options) {
expectImpl(expression, asList(textValue), expected, message, options);
}
private void expectTrue(String expression, String message, HasTextCommonOptions options) {
private void expectTrue(String expression, String message, FrameExpectOptions options) {
List<ExpectedTextValue> expectedText = null;
expectImpl(expression, expectedText, null, message, options);
}
private void expectImpl(String expression, List<ExpectedTextValue> expectedText, Object expected, String message, HasTextCommonOptions options) {
if (options == null) {
options = new HasTextCommonOptions();
}
FrameExpectOptions expectOptions = new FrameExpectOptions();
expectOptions.expressionArg = options.expressionArg;
expectOptions.expectedNumber = options.expectedNumber;
expectOptions.expectedValue = options.expectedValue;
expectOptions.expectedText = expectedText;
expectOptions.isNot = isNot;
expectOptions.timeout = options.timeout;
expectOptions.useInnerText = options.useInnerText;
expectImpl(expression, expectOptions, expected, message);
}
private void expectImpl(String expression, FrameExpectOptions expectOptions, Object expected, String message) {
FrameExpectResult result = actual.expect(expression, expectOptions);
if (result.matches == isNot) {
Object actual = result.received == null ? null : Serialization.deserialize(result.received);
String log = String.join("\n", result.log);
if (!log.isEmpty()) {
log = "\nCall log:\n" + log;
}
if (expected == null) {
throw new AssertionFailedError(message + log);
}
throw new AssertionFailedError(message + log, formatValue(expected), formatValue(actual));
}
}
@Override
public LocatorAssertions not() {
return new LocatorAssertionsImpl(actual, !isNot);
return new LocatorAssertionsImpl(actualLocator, !isNot);
}
}
@@ -18,31 +18,30 @@ package com.microsoft.playwright.impl;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.assertions.PageAssertions;
import org.opentest4j.AssertionFailedError;
import java.util.regex.Pattern;
import static com.microsoft.playwright.impl.UrlMatcher.resolveUrl;
import static com.microsoft.playwright.impl.Utils.convertViaJson;
import static java.util.Arrays.asList;
public class PageAssertionsImpl implements PageAssertions {
private final PageImpl actual;
private final boolean isNot;
public class PageAssertionsImpl extends AssertionsBase implements PageAssertions {
private final PageImpl actualPage;
public PageAssertionsImpl(Page page) {
this(page, false);
}
private PageAssertionsImpl(Page page, boolean isNot) {
this.actual = (PageImpl) page;
this.isNot = isNot;
super((LocatorImpl) page.locator(":root"), isNot);
this.actualPage = (PageImpl) page;
}
@Override
public void hasTitle(String title, HasTitleOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
expected.string = title;
expectImpl("to.have.title", expected, title, "Page title expected to be", options == null ? null : options.timeout);
expectImpl("to.have.title", expected, title, "Page title expected to be", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -51,17 +50,17 @@ public class PageAssertionsImpl implements PageAssertions {
ExpectedTextValue expected = new ExpectedTextValue();
expected.regexSource = pattern.pattern();
// expected.regexFlags =
expectImpl("to.have.title", expected, pattern, "Page title expected to match regex", options == null ? null : options.timeout);
expectImpl("to.have.title", expected, pattern, "Page title expected to match regex", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public void hasURL(String url, HasURLOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
if (actual.context().baseUrl != null) {
url = resolveUrl(actual.context().baseUrl, url);
if (actualPage.context().baseUrl != null) {
url = resolveUrl(actualPage.context().baseUrl, url);
}
expected.string = url;
expectImpl("to.have.url", expected, url, "Page URL expected to be", options == null ? null : options.timeout);
expectImpl("to.have.url", expected, url, "Page URL expected to be", convertViaJson(options, FrameExpectOptions.class));
}
@Override
@@ -70,31 +69,12 @@ public class PageAssertionsImpl implements PageAssertions {
ExpectedTextValue expected = new ExpectedTextValue();
expected.regexSource = pattern.pattern();
// expected.regexFlags =
expectImpl("to.have.url", expected, pattern, "Page URL expected to match regex", options == null ? null : options.timeout);
}
private void expectImpl(String expression, ExpectedTextValue textValue, Object expected, String message, Double timeout) {
LocatorImpl locator = actual.locator(":root");
FrameExpectOptions expectOptions = new FrameExpectOptions();
expectOptions.expectedText = asList(textValue);
expectOptions.isNot = isNot;
expectOptions.timeout = timeout;
FrameExpectResult result = locator.expect(expression, expectOptions);
if (result.matches == isNot) {
Object actual = result.received == null ? null : Serialization.deserialize(result.received);
String log = String.join("\n", result.log);
if (!log.isEmpty()) {
log = "\nCall log:\n" + log;
}
throw new AssertionFailedError(message + log, expected, actual);
}
expectImpl("to.have.url", expected, pattern, "Page URL expected to match regex", convertViaJson(options, FrameExpectOptions.class));
}
@Override
public PageAssertions not() {
return new PageAssertionsImpl(actual, !isNot);
return new PageAssertionsImpl(actualPage, !isNot);
}
}