本文共 1435 字,大约阅读时间需要 4 分钟。
如何将Cookie从Selenium WebDriver传递给Rest-Assured?当您在API和UI层进行自动化测试时,可能会出现这样的情况:您需要将API测试中的属性传递给UI测试,反之亦然。
在此示例中,我们将展示如何使用Java将Selenium WebDriver中的Cookie传递给Rest-Assured。
将Cookie从Selenium传递给Rest-Assuredimport 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 {
@Testpublic void cookieTest() { WebDriver driver = new ChromeDriver(); driver.navigate().to("http://www.someurl.com"); SetseleniumCookies = 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);}
转载于:https://blog.51cto.com/13887297/2151279