1. Overview
1.概述
In this article, we’ll have a quick look at how to use cookies with Selenium WebDriver in Java.
在本文中,我们将快速了解如何在Java中使用Selenium WebDriver。
We’ll talk a bit about some use cases, and then we’ll jump straight into code.
我们将谈一谈一些用例,然后我们将直接跳到代码。
2. Working with Cookies
2.使用Cookies的工作
An everyday use case for manipulating cookies is to persist our session between tests.
操纵cookie的一个日常用例是在测试之间坚持我们的会话。。
An even simpler scenario is when we want to test that our backend is setting cookies properly.
一个更简单的情况是,我们想测试我们的后端是否正确设置了cookies。
In the next sections, we’ll briefly talk about handling cookies while providing simple code examples.
在接下来的章节中,我们将简要谈论处理cookies的问题,同时提供简单的代码示例。
2.1. Setup
2.1.设置
We’ll need to add the selenium-java dependency to our project:
我们需要将selenium-java 依赖性添加到我们的项目中。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
Next, we should download the latest version of the Gecko driver.
接下来,我们应该下载最新版本的Gecko驱动。
Now let’s set up our test class:
现在让我们来设置我们的测试类。
public class SeleniumCookiesJUnitLiveTest {
private WebDriver driver;
private String navUrl;
@Before
public void setUp() {
Capabilities capabilities = DesiredCapabilities.firefox();
driver = new FirefoxDriver(capabilities);
navUrl = "https://baeldung.com";
}
}
2.2. Reading Cookies
2.2.读取Cookies
Next, we’ll implement a simple test to verify that cookies exist in our driver after we’ve navigated to a webpage:
接下来,我们将实现一个简单的测试,以验证我们的驱动程序在导航到一个网页后是否存在cookies。
@Test
public void whenNavigate_thenCookiesExist() {
driver.navigate().to(navUrl);
Set<Cookie> cookies = driver.manage().getCookies();
assertThat(cookies, is(not(empty())));
}
Often, we might want to search for a specific cookie:
通常情况下,我们可能想要搜索一个特定的cookie。
@Test
public void whenNavigate_thenLpCookieIsHasCorrectValue() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie.getValue(), containsString("www.baeldung.com"));
}
2.3. Cookie Properties
2.3.饼干属性
A cookie can be associated with a domain, have an expiry date, and much more.
一个cookie可以与一个域名相关联,有一个过期日期,以及更多。
Let’s take a look at some common cookie properties:
让我们来看看一些常见的cookie属性。
@Test
public void whenNavigate_thenLpCookieHasCorrectProps() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie.getDomain(), equalTo(".baeldung.com"));
assertThat(lpCookie.getPath(), equalTo("/"));
assertThat(lpCookie.getExpiry(), is(not(nullValue())));
assertThat(lpCookie.isSecure(), equalTo(false));
assertThat(lpCookie.isHttpOnly(), equalTo(false));
}
2.4. Adding Cookies
2.4.添加Cookies
Adding a cookie is a straightforward process.
添加一个cookie是一个简单的过程。。
We create the cookie and add it to the driver using the addCookie method:
我们创建cookie,并使用addCookie方法将其添加到驱动程序。
@Test
public void whenAddingCookie_thenItIsPresent() {
driver.navigate().to(navUrl);
Cookie cookie = new Cookie("foo", "bar");
driver.manage().addCookie(cookie);
Cookie driverCookie = driver.manage().getCookieNamed("foo");
assertThat(driverCookie.getValue(), equalTo("bar"));
}
2.5. Deleting Cookies
2.5.删除Cookies
As we might’ve expected, we can also delete a cookie using the deleteCookie method:
正如我们可能已经预料到的那样,我们也可以使用deleteCookie方法删除一个cookie:。
@Test
public void whenDeletingCookie_thenItIsAbsent() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(lpCookie, is(not(nullValue())));
driver.manage().deleteCookie(lpCookie);
Cookie deletedCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(deletedCookie, is(nullValue()));
}
2.6. Overriding Cookies
2.6.重写Cookies
Although there’s no explicit method for overriding a cookie, there’s a simple way.
虽然没有明确的方法来重写一个cookie,但有一个简单的方法。
We can delete the cookie and add a new one with the same name but a different value:
我们可以删除这个cookie并添加一个具有相同名称但不同值的新cookie。
@Test
public void whenOverridingCookie_thenItIsUpdated() {
driver.navigate().to(navUrl);
Cookie lpCookie = driver.manage().getCookieNamed("lp_120073");
driver.manage().deleteCookie(lpCookie);
Cookie newLpCookie = new Cookie("lp_120073", "foo");
driver.manage().addCookie(newLpCookie);
Cookie overriddenCookie = driver.manage().getCookieNamed("lp_120073");
assertThat(overriddenCookie.getValue(), equalTo("foo"));
}
3. Conclusion
3.总结
In this quick tutorial, we learned how to work with cookies using Selenium WebDriver in Java through quick and practical examples.
在这个快速教程中,我们通过快速和实际的例子学习了如何在Java中使用Selenium WebDriver来处理cookie。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。