Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api Page #141

Merged
merged 19 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private Response execute(HttpRequest request) {
}

@SneakyThrows
public Response sendRequest() {
public Response get() {
HttpRequest request = requestBuilder.GET().build();
return execute(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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() + "");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ default <T, V, R> R waitFor(final T locator, final V arg, final WaitCondition co
.until((Function<WebDriver, ?>) condition.getType().apply(locator, arg));
}

default WebClient api(String url) {
default WebClient request(String url) {
return new WebClient(url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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\": \"[email protected]\",\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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private String getBrowserVersionFromCommand(String regQuery) {
public List<String> getBinaryVersions(String binaryDownloadUrl, String matcher) {
List<String> 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(
Expand Down