How to use Matchers class of org.testingisdocumenting.webtau package

Best Webtau code snippet using org.testingisdocumenting.webtau.Matchers

Source:DbQuery.java Github

copy

Full Screen

1/*2 * Copyright 2020 webtau maintainers3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.testingisdocumenting.webtau.db;17import org.testingisdocumenting.webtau.data.render.DataRenderers;18import org.testingisdocumenting.webtau.data.table.TableData;19import org.testingisdocumenting.webtau.data.table.header.TableDataHeader;20import org.testingisdocumenting.webtau.expectation.ActualPath;21import org.testingisdocumenting.webtau.expectation.ActualPathAndDescriptionAware;22import org.testingisdocumenting.webtau.expectation.ActualValueExpectations;23import org.testingisdocumenting.webtau.reporter.StepReportOptions;24import org.testingisdocumenting.webtau.reporter.TokenizedMessage;25import org.testingisdocumenting.webtau.reporter.WebTauStep;26import java.util.Collections;27import java.util.List;28import java.util.Map;29import java.util.function.Supplier;30import java.util.stream.Collectors;31import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;32import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.*;33import static org.testingisdocumenting.webtau.reporter.WebTauStep.*;34/**35 * <code>DbQuery</code> defines a query to be evaluated at later stage.36 * It is compatible with <code>should</code> and <code>waitTo</code> matchers.37 * <p>38 * To define a query use <code>db.query("select * from table where id=:id", [id: 'my-id'])</code>39 */40public class DbQuery implements ActualValueExpectations, ActualPathAndDescriptionAware {41 private static final ActualPath ACTUAL_PATH = new ActualPath("query result");42 private final Supplier<String> dataSourceLabelSupplier;43 private final Supplier<List<Map<String, Object>>> dataFetcher;44 private final String query;45 private final Map<String, Object> params;46 DbQuery(Supplier<String> dataSourceLabelSupplier, Supplier<List<Map<String, Object>>> dataFetcher, String query, Map<String, Object> params) {47 this.dataSourceLabelSupplier = dataSourceLabelSupplier;48 this.dataFetcher = dataFetcher;49 this.query = query;50 this.params = params;51 }52 public int numberOfRows() {53 return tableData().numberOfRows();54 }55 public TableData tableData() {56 return fetchValueAsStep(this::queryTableDataNoStep);57 }58 public <E> E singleValue() {59 return fetchValueAsStep(this::querySingleValueNoStep);60 }61 @Override62 public ActualPath actualPath() {63 return ACTUAL_PATH;64 }65 @Override66 public TokenizedMessage describe() {67 return appendParamsIfRequired(tokenizedMessage(queryValue(query)));68 }69 @Override70 public StepReportOptions shouldReportOption() {71 return StepReportOptions.REPORT_ALL;72 }73 boolean isSingleValue(TableData result) {74 return result.numberOfRows() == 1 && result.getHeader().size() == 1;75 }76 <E> E getUnderlyingSingleValue(TableData result) {77 return result.row(0).get(0);78 }79 TableData queryTableDataNoStep() {80 return convertToTable(dataFetcher.get());81 }82 <E> E querySingleValueNoStep() {83 TableData table = queryTableDataNoStep();84 if (!isSingleValue(table)) {85 throw new RuntimeException(query + " result is not a single value:\n" + DataRenderers.render(table));86 }87 return getUnderlyingSingleValue(table);88 }89 private <E> E fetchValueAsStep(Supplier<Object> supplier) {90 WebTauStep step = createStep(91 queryMessage("running DB query"),92 () -> queryMessage("ran DB query"),93 supplier);94 return step.execute(StepReportOptions.REPORT_ALL);95 }96 private TokenizedMessage queryMessage(String actionLabel) {97 return appendParamsIfRequired(98 tokenizedMessage(action(actionLabel), stringValue(query), ON, id(dataSourceLabelSupplier.get())));99 }100 private TokenizedMessage appendParamsIfRequired(TokenizedMessage message) {101 if (params.isEmpty()) {102 return message;103 }104 return message.add(WITH, stringValue(params));105 }106 private TableData convertToTable(List<Map<String, Object>> result) {107 if (result.isEmpty()) {108 return new TableData(Collections.emptyList());109 }110 List<String> columns = result.get(0).keySet().stream()111 .map(String::toUpperCase)112 .collect(Collectors.toList());113 TableDataHeader header = new TableDataHeader(columns.stream());114 TableData tableData = new TableData(header);115 result.forEach(row -> tableData.addRow(row.values().stream()));116 return tableData;117 }118}...

Full Screen

Full Screen

Source:GraphQLTestBase.java Github

copy

Full Screen

...27import org.testingisdocumenting.webtau.utils.CollectionUtils;28import org.testingisdocumenting.webtau.utils.UrlUtils;29import java.util.Map;30import java.util.function.Consumer;31import static org.testingisdocumenting.webtau.Matchers.actual;32import static org.testingisdocumenting.webtau.Matchers.equal;33public class GraphQLTestBase implements WebTauHttpConfiguration {34 protected static final GraphQLTestDataServer testServer = new GraphQLTestDataServer();35 protected final static String QUERY = "{ taskById(id: \"a\") { id } }";36 protected final static String MULTI_OP_QUERY = "query task { taskById(id: \"a\") { id } } " +37 "query openTasks { allTasks(uncompletedOnly: true) { id } }";38 protected final static String OP_NAME = "task";39 protected final static String QUERY_WITH_VARS = "query task($id: ID!) { taskById(id: $id) { id } }";40 protected final static Map<String, Object> VARS = CollectionUtils.aMapOf("id", "a");41 protected final static String MULTI_OP_QUERY_WITH_VARS = "query task($id: ID!) { taskById(id: $id) { id } } " +42 "query openTasks { allTasks(uncompletedOnly: true) { id } }";43 protected final static String ERROR_QUERY = "query error($msg: String!) { error(msg: $msg) { msg } }";44 protected final static HttpResponseValidator VALIDATOR = (header, body) -> body.get("data.taskById.id").should(equal("a"));45 protected final static HttpResponseValidatorWithReturn VALIDATOR_WITH_RETURN = (header, body) -> {46 body.get("data.taskById.id").should(equal("a"));...

Full Screen

Full Screen

Source:GraphQLJavaTest.java Github

copy

Full Screen

...17import org.junit.Test;18import java.util.Arrays;19import java.util.List;20import java.util.Map;21import static org.testingisdocumenting.webtau.Matchers.actual;22import static org.testingisdocumenting.webtau.Matchers.equal;23import static org.testingisdocumenting.webtau.graphql.GraphQL.graphql;24import static org.testingisdocumenting.webtau.utils.CollectionUtils.aMapOf;25public class GraphQLJavaTest extends GraphQLTestBase {26 @Test27 public void execute() {28 String query = "query {" +29 " allTasks(uncompletedOnly: false) {" +30 " id" +31 " description" +32 " }" +33 "}";34 List<String> expectedIds = Arrays.asList("a", "b", "c");35 List<String> ids = graphql.execute(query, (header, body) -> {36 body.get("errors").should(equal(null));...

Full Screen

Full Screen

Matchers

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.Ddjt;2import org.testingisdocumenting.webtau.expectation.ActualPathValue;3import org.testingisdocumenting.webtau.expectation.ExpectedPathValue;4import org.testingisdocumenting.webtau.expectation.PathMatcher;5import org.testingisdocumenting.webtau.expectation.PathMatchers;6import org.testingisdocumenting.webtau.expectation.ValueMatcher;7import org.testingisdocumenting.webtau.expectation.ValueMatchers;8import java.util.List;9import java.util.Map;10import static org.testingisdocumenting.webtau.Ddjt.*;11public class 1 extends Ddjt {12 public static void main(String[] args) {13 Ddjt.http.get("/api/books", (header, body) -> {14 body.should(equalJson("{\"books\":[{\"id\":1,\"title\":\"Book1\",\"author\":\"Author1\"},{\"id\":2,\"title\":\"Book2\",\"author\":\"Author2\"},{\"id\":3,\"title\":\"Book3\",\"author\":\"Author3\"}]}"));15 body.should(equalJson("{\"books\":[{\"id\":1,\"title\":\"Book1\",\"author\":\"Author1\"},{\"id\":2,\"title\":\"Book2\",\"author\":\"Author2\"},{\"id\":3,\"title\":\"Book3\",\"author\":\"Author3\"}]}"));16 body.should(equalJson("{\"books\":[{\"id\":1,\"title\":\"Book1\",\"author\":\"Author1\"},{\"id\":2,\"title\":\"Book2\",\"author\":\"Author2\"},{\"id\":3,\"title\":\"Book3\",\"author\":\"Author3\"}]}"));17 body.should(equalJson("{\"books\":[{\"id\":1,\"title\":\"Book1\",\"author\":\"Author1\"},{\"id\":2,\"title\":\"Book2\",\"author\":\"Author2\"},{\"id\":3,\"title\":\"Book3\",\"author\":\"Author3\"}]}"));18 body.should(equalJson("{\"books\":[{\"id\":1,\"title\":\"Book1\",\"author\":\"Author1\"},{\"id\":2,\"title\":\"Book2\",\"author\":\"Author2\"},{\"id\":3,\"title\":\"Book3\",\"author\":\"Author3\"}]}"));19 });20 }21}

Full Screen

Full Screen

Matchers

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.expectation.ActualPath;2import org.testingisdocumenting.webtau.expectation.ActualPathValue;3import org.testingisdocumenting.webtau.expectation.PathMatcher;4import org.testingisdocumenting.webtau.expectation.PathMatcherDsl;5import org.testingisdocumenting.webtau.expectation.PathMatchers;6import org.testingisdocumenting.webtau.expectation.ValueMatcher;7import org.testingisdocumenting.webtau.expectation.ValueMatcherDsl;8import org.testingisdocumenting.webtau.expectation.ValueMatchers;9import org.testingisdocumenting.webtau.expectation.web.WebMessageBuilder;10import org.testingisdocumenting.webtau.expectation.web.WebMessageBuilderDsl;11import org.testingisdocumenting.webtau.expectation.web.WebMessageBuilders;12import org.testingisdocumenting.webtau.expectation.web.WebMessagePayloadPathMatcher;13import org.testingisdocumenting.webtau.expectation.web.WebMessagePayloadPathMatcherDsl;14import org.testingisdocumenting.webtau.expectation.web.WebMessagePayloadPathMatchers;15import org.testingisdocumenting.webtau.http.HttpHeader;16import org.testingisdocumenting.webtau.http.HttpResponse;17import org.testingisdocumenting.webtau.http.datanode.DataNode;18import org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder;19import org.testingisdocumenting.webtau.reporter.TokenizedMessage;20import org.testingisdocumenting.webtau.reporter.WebTauStep;21import org.testingisdocumenting.webtau.reporter.WebTauStepOutput;22import org.testingisdocumenting.webtau.reporter.WebTauStepOutputPayload;23import org.testingisdocumenting.webtau.reporter.WebTauStepOutputPayloadHandler;24import org.testingisdocumenting.webtau.reporter.WebTauStepOutputPayloadHandlers;25import org.testingisdocumenting.webtau.reporter.WebTauStepOutputValu

Full Screen

Full Screen

Matchers

Using AI Code Generation

copy

Full Screen

1import static org.testingisdocumenting.webtau.WebTauDsl.*;2import static org.testingisdocumenting.webtau.http.Http.http;3import static org.testingisdocumenting.webtau.http.Http.httpDelete;4import static org.testingisdocumenting.webtau.http.Http.httpGet;5import static org.testingisdocumenting.webtau.http.Http.httpPost;6import static org.testingisdocumenting.webtau.http.Http.httpPut;7import static org.testingisdocumenting.webtau.http.validation.HttpValidationResultMatchers.*;8public class MatchersDemo {9 public void verifyResponseBody() {10 httpGet("/api/books", (header, body) -> {11 body.should(equalJson("{\n" +12 " {\n" +13 " },\n" +14 " {\n" +15 " }\n" +16 "}"));17 body.should(equalJson("{\n" +18 " {\n" +19 " },\n" +20 " {\n" +21 " }\n" +22 "}", "books[0].id", "books[0].title"));23 });24 }25}26import static org.hamcrest.Matchers.*;27import static org.testingisdocumenting.webtau.WebTauDsl.*;28import static

Full Screen

Full Screen

Matchers

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.Ddjt;2import org.testingisdocumenting.webtau.WebTauDsl;3import org.testingisdocumenting.webtau.http.Http;4import org.testingisdocumenting.webtau.http.validation.path.Path;5import org.testingisdocumenting.webtau.http.validation.path.PathMatchers;6import org.testingisdocumenting.webtau.http.validation.path.PathMatchersBuilder;7import static org.testingisdocumenting.webtau.WebTauDsl.*;8WebTauDsl.createWebTau();9Http http = Http.http();10http.get("/users")11 .should(12 Ddjt.statusCode(200),13 Ddjt.body(14 PathMatchers.arrayOfSize(2),15 PathMatchers.arrayElement(0, PathMatchers.jsonObjectWithField("name", "sally")),16 PathMatchers.arrayElement(1, PathMatchers.jsonObjectWithField("name", "bob"))17 );18import org.testingisdocumenting.webtau.Ddjt;19import org.testingisdocumenting.webtau.WebTauDsl;20import org.testingisdocumenting.webtau.http.Http;21import org.testingisdocumenting.webtau.http.validation.path.Path;22import org.testingisdocumenting.webtau.http.validation.path.PathMatchers;23import org.testingisdocumenting.webtau.http.validation.path.PathMatchersBuilder;24import static org.testingisdocumenting.webtau.WebTauDsl.*;25WebTauDsl.createWebTau();26Http http = Http.http();27http.get("/users")28 .should(29 Ddjt.statusCode(200),30 Ddjt.body(31 Path.arrayOfSize(2),32 Path.arrayElement(0, Path.jsonObjectWithField("name", "sally")),33 Path.arrayElement(1,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Webtau automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful