如何將Cookie從Selenium WebDriver傳遞到休息保證

如何將Cookie從Selenium WebDriver傳遞給Rest-Assured?當您在API和UI層進行自動化測試時,可能會出現這樣的情況:您需要將API測試中的屬性傳遞給UI測試,反之亦然。

在此示例中,我們將展示如何使用Java將Selenium WebDriver中的Cookie傳遞給Rest-Assured。
將Cookie從Selenium傳遞給Rest-Assured
import io.restassured.RestAssured;
import io.restassured.http.Cookies;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static io.restassured.RestAssured.given;

public class RestAssuredWebDriverCookie {

@Test
public void cookieTest() {
    WebDriver driver = new ChromeDriver();

    driver.navigate().to("http://www.someurl.com");

    Set<Cookie> seleniumCookies = driver.manage().getCookies();

    // This is where the Cookies will live going forward
    List restAssuredCookies = new ArrayList();

    // Simply pull all the cookies into Rest-Assured
    for (org.openqa.selenium.Cookie cookie : seleniumCookies) {
        restAssuredCookies.add(new io.restassured.http.Cookie.Builder(cookie.getName(), cookie.getValue()).build());
    }

    // Pass them into the Rest-Assured Call
    given().spec(RestAssured.requestSpecification)
            .basePath("/some-path")
            .cookies(new Cookies(restAssuredCookies))
            .queryParam("id", "1234")
            .get()
            .then().statusCode(200);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章