From 030c763425fe2a36bf4f6e074131a4aab6732e94 Mon Sep 17 00:00:00 2001 From: chris9408 Date: Tue, 24 Dec 2019 21:20:23 +0200 Subject: [PATCH 1/3] [BAEL-2948] Using cookies with Selenium WebDriver --- .../junit/SeleniumCookiesJUnitLiveTest.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java new file mode 100644 index 0000000000..b01cf43fca --- /dev/null +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java @@ -0,0 +1,107 @@ +package test.java.com.baeldung.selenium.junit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.Capabilities; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.remote.DesiredCapabilities; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +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"; + driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); + } + + @After + public void teardown() { + driver.close(); + } + + @Test + public void givenHomePage_whenNavigate_thenCookiesExist() { + driver.navigate().to(navUrl); + Set cookies = driver.manage().getCookies(); + + assertThat(cookies, is(not(empty()))); + } + + @Test + public void givenHomePage_whenNavigate_thenLpCookieExists() { + driver.navigate().to(navUrl); + Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); + + assertThat(lpCookie, is(not(nullValue()))); + } + + @Test + public void givenHomePage_whenNavigate_thenLpCookieIsHasCorrectValue() { + driver.navigate().to(navUrl); + Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); + + assertThat(lpCookie.getValue(), containsString("www.baeldung.com")); + } + + @Test + public void givenHomePage_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)); + } + + @Test + public void givenHomePage_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")); + } + + @Test + public void givenHomePage_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())); + } + + @Test + public void givenHomePage_whenOverridingCookie_thenItIsUpdated() { + driver.navigate().to(navUrl); + Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); + driver.manage().deleteCookie(lpCookie); + + lpCookie = new Cookie("lp_120073", "foo"); + driver.manage().addCookie(lpCookie); + + assertThat(lpCookie.getValue(), equalTo("foo")); + } + +} From 77d442b02fdc761e57e54e3885dcd154b7b19eee Mon Sep 17 00:00:00 2001 From: chris9408 Date: Fri, 27 Dec 2019 15:22:38 +0200 Subject: [PATCH 2/3] updated test names --- .../junit/SeleniumCookiesJUnitLiveTest.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java index b01cf43fca..95cb013eb9 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java @@ -26,15 +26,16 @@ public class SeleniumCookiesJUnitLiveTest { driver = new FirefoxDriver(capabilities); navUrl = "https://baeldung.com"; driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); + System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); } @After public void teardown() { - driver.close(); + driver.quit(); } @Test - public void givenHomePage_whenNavigate_thenCookiesExist() { + public void whenNavigate_thenCookiesExist() { driver.navigate().to(navUrl); Set cookies = driver.manage().getCookies(); @@ -42,7 +43,7 @@ public class SeleniumCookiesJUnitLiveTest { } @Test - public void givenHomePage_whenNavigate_thenLpCookieExists() { + public void whenNavigate_thenLpCookieExists() { driver.navigate().to(navUrl); Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); @@ -50,7 +51,7 @@ public class SeleniumCookiesJUnitLiveTest { } @Test - public void givenHomePage_whenNavigate_thenLpCookieIsHasCorrectValue() { + public void whenNavigate_thenLpCookieIsHasCorrectValue() { driver.navigate().to(navUrl); Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); @@ -58,7 +59,7 @@ public class SeleniumCookiesJUnitLiveTest { } @Test - public void givenHomePage_whenNavigate_thenLpCookieHasCorrectProps() { + public void whenNavigate_thenLpCookieHasCorrectProps() { driver.navigate().to(navUrl); Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); @@ -70,7 +71,7 @@ public class SeleniumCookiesJUnitLiveTest { } @Test - public void givenHomePage_whenAddingCookie_thenItIsPresent() { + public void whenAddingCookie_thenItIsPresent() { driver.navigate().to(navUrl); Cookie cookie = new Cookie("foo", "bar"); driver.manage().addCookie(cookie); @@ -80,7 +81,7 @@ public class SeleniumCookiesJUnitLiveTest { } @Test - public void givenHomePage_whenDeletingCookie_thenItIsAbsent() { + public void whenDeletingCookie_thenItIsAbsent() { driver.navigate().to(navUrl); Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); @@ -93,7 +94,7 @@ public class SeleniumCookiesJUnitLiveTest { } @Test - public void givenHomePage_whenOverridingCookie_thenItIsUpdated() { + public void whenOverridingCookie_thenItIsUpdated() { driver.navigate().to(navUrl); Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); driver.manage().deleteCookie(lpCookie); From be6327545594ec898a843784f044841761731a34 Mon Sep 17 00:00:00 2001 From: chris9408 Date: Wed, 1 Jan 2020 18:12:54 +0200 Subject: [PATCH 3/3] [BAEL-2948] requested PR changes --- .../selenium/junit/SeleniumCookiesJUnitLiveTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java index 95cb013eb9..0cbbf52454 100644 --- a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/junit/SeleniumCookiesJUnitLiveTest.java @@ -99,10 +99,12 @@ public class SeleniumCookiesJUnitLiveTest { Cookie lpCookie = driver.manage().getCookieNamed("lp_120073"); driver.manage().deleteCookie(lpCookie); - lpCookie = new Cookie("lp_120073", "foo"); - driver.manage().addCookie(lpCookie); + Cookie newLpCookie = new Cookie("lp_120073", "foo"); + driver.manage().addCookie(newLpCookie); - assertThat(lpCookie.getValue(), equalTo("foo")); + Cookie overriddenCookie = driver.manage().getCookieNamed("lp_120073"); + + assertThat(overriddenCookie.getValue(), equalTo("foo")); } }