diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java index 3b91007b1..d30e15a5e 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java @@ -112,7 +112,7 @@ private Response execute(HttpRequest request) { } @SneakyThrows - public Response sendRequest() { + public Response get() { HttpRequest request = requestBuilder.GET().build(); return execute(request); } diff --git a/selcukes-commons/src/test/java/io/github/selcukes/commons/tests/WebClientTest.java b/selcukes-commons/src/test/java/io/github/selcukes/commons/tests/WebClientTest.java index e3906ff47..b58d71af8 100644 --- a/selcukes-commons/src/test/java/io/github/selcukes/commons/tests/WebClientTest.java +++ b/selcukes-commons/src/test/java/io/github/selcukes/commons/tests/WebClientTest.java @@ -48,7 +48,7 @@ public void postTest() { public void requestTest() { WebClient client = new WebClient("https://httpbin.org/get"); - Response response = client.sendRequest(); + Response response = client.get(); logger.info(response::getBody); } @@ -58,7 +58,7 @@ public void bearerAuthTest() { WebClient client = new WebClient("https://httpbin.org/#/Auth/get_bearer"); Response response = client.authenticator("hello") - .sendRequest(); + .get(); logger.debug(response::getBody); } @@ -68,7 +68,7 @@ public void authTest() { WebClient client = new WebClient("https://httpbin.org/#/Auth/get_basic_auth__user___passwd_"); Response response = client.authenticator("hello", "hello") - .sendRequest(); + .get(); logger.debug(() -> response.getStatusCode() + ""); } } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/page/ApiPage.java b/selcukes-core/src/main/java/io/github/selcukes/core/page/ApiPage.java new file mode 100644 index 000000000..71c2e9474 --- /dev/null +++ b/selcukes-core/src/main/java/io/github/selcukes/core/page/ApiPage.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) Ramesh Babu Prudhvi. + * + * 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 io.github.selcukes.core.page; + +import org.openqa.selenium.WebDriver; + +public class ApiPage implements Page { + @Override + public WebDriver getDriver() { + return null; + } + +} diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/page/Page.java b/selcukes-core/src/main/java/io/github/selcukes/core/page/Page.java index f5883bc82..a50f1cfd0 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/page/Page.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/page/Page.java @@ -284,7 +284,7 @@ default R waitFor(final T locator, final V arg, final WaitCondition co .until((Function) condition.getType().apply(locator, arg)); } - default WebClient api(String url) { + default WebClient request(String url) { return new WebClient(url); } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/page/Pages.java b/selcukes-core/src/main/java/io/github/selcukes/core/page/Pages.java index 317a8b0a5..0fa2be409 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/page/Pages.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/page/Pages.java @@ -38,4 +38,8 @@ public static synchronized MobilePage mobilePage() { WebDriver driver = DriverManager.createDriver(DeviceType.MOBILE); return new MobilePage(driver); } + + public static synchronized ApiPage apiPage() { + return new ApiPage(); + } } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java b/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java index 4563841c7..25874a96b 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java @@ -17,6 +17,7 @@ package io.github.selcukes.core.validation; import io.appium.java_client.AppiumBy; +import io.github.selcukes.commons.http.Response; import io.github.selcukes.core.page.Page; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; @@ -68,4 +69,8 @@ public ElementValidation element(WebElement element) { return new ElementValidation(isSoft, page, element); } + public ResponseValidation response(Response response) { + return new ResponseValidation(isSoft, page, response); + } + } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/validation/ResponseValidation.java b/selcukes-core/src/main/java/io/github/selcukes/core/validation/ResponseValidation.java new file mode 100644 index 000000000..be27a2b18 --- /dev/null +++ b/selcukes-core/src/main/java/io/github/selcukes/core/validation/ResponseValidation.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) Ramesh Babu Prudhvi. + * + * 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 io.github.selcukes.core.validation; + +import io.github.selcukes.commons.http.Response; +import io.github.selcukes.core.page.Page; + +import static io.github.selcukes.commons.http.Response.getReasonPhrase; +import static io.github.selcukes.core.validation.Validation.failWithMessage; + +public class ResponseValidation { + Response response; + Page page; + + boolean isSoft; + + public ResponseValidation(boolean isSoft, Page page, Response response) { + this.response = response; + this.page = page; + this.isSoft = isSoft; + } + + public ResponseValidation isOK() { + if (response.getStatusCode() != 200) { + failWithMessage(isSoft, "Expected Response Status should be [%s] but was [%s]", "OK", getReasonPhrase(response.getStatusCode())); + } + return this; + } + + public ResponseValidation containsText(String expectedText) { + if (!response.getBody().contains(expectedText)) { + failWithMessage(isSoft, "Expected Response should contain text [%s] but actual text was [%s]", expectedText, response.getBody()); + } + return this; + } + +} diff --git a/selcukes-core/src/test/java/io/github/selcukes/core/tests/api/ApiTest.java b/selcukes-core/src/test/java/io/github/selcukes/core/tests/api/ApiTest.java new file mode 100644 index 000000000..68a35c23c --- /dev/null +++ b/selcukes-core/src/test/java/io/github/selcukes/core/tests/api/ApiTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) Ramesh Babu Prudhvi. + * + * 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 io.github.selcukes.core.tests.api; + +import io.github.selcukes.commons.http.Response; +import io.github.selcukes.core.page.ApiPage; +import io.github.selcukes.core.page.Pages; +import lombok.CustomLog; +import lombok.Data; +import org.testng.annotations.Test; + +@CustomLog +public class ApiTest { + @Test + public void authTest() { + String user = "{\n" + + " \"email\": \"eve.holt@reqres.in\",\n" + + " \"password\": \"admin\"\n" + + "}"; + ApiPage page = Pages.apiPage(); + Response response = page.request("https://reqres.in/api/register") + .post(user); + page.assertThat().response(response).isOK(); + logger.info(() -> "Token is: " + response.getBodyAs(ResponseBody.class).getToken()); + } + + @Data + static class ResponseBody { + String id; + String token; + } +} diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/AbstractBinary.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/AbstractBinary.java index 7a67963f1..74566aa5f 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/AbstractBinary.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/AbstractBinary.java @@ -77,7 +77,7 @@ public void setProxy(String proxy) { } protected Response sendRequest(String binaryDownloadUrl) { - return new WebClient(binaryDownloadUrl).proxy(getProxy()).sendRequest(); + return new WebClient(binaryDownloadUrl).proxy(getProxy()).get(); } protected String getVersionNumberFromGit(String binaryDownloadUrl) { diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/version/VersionDetector.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/version/VersionDetector.java index 5d7de8ddd..f8e37dcb1 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/version/VersionDetector.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/version/VersionDetector.java @@ -94,7 +94,7 @@ private String getBrowserVersionFromCommand(String regQuery) { public List getBinaryVersions(String binaryDownloadUrl, String matcher) { List versions = new ArrayList<>(); WebClient client = new WebClient(binaryDownloadUrl); - String response = client.sendRequest().getBody(); + String response = client.get().getBody(); try (InputStream downloadStream = new ByteArrayInputStream(response.getBytes())) { Document doc = parse(downloadStream, null, ""); Elements elements = doc.select(