From b157655ec85aede95a059068da09e454753cc3d5 Mon Sep 17 00:00:00 2001 From: Vladislav Bauer Date: Sun, 27 Sep 2015 21:41:00 +0600 Subject: [PATCH] #8 Working with cookies - adding, deleting - all that already can WebDriver --- .../actions/beans/AbstractCookieAction.java | 82 +++++++++++++++++++ .../src/main/resources/webdriver-actions.xsd | 13 ++- .../qatools/actions/DriverActionsTest.java | 45 +++++++--- .../actions/mockfactory/MockFactory.java | 6 +- 4 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 actions-beans/src/main/java/ru/yandex/qatools/actions/beans/AbstractCookieAction.java diff --git a/actions-beans/src/main/java/ru/yandex/qatools/actions/beans/AbstractCookieAction.java b/actions-beans/src/main/java/ru/yandex/qatools/actions/beans/AbstractCookieAction.java new file mode 100644 index 0000000..449e0b3 --- /dev/null +++ b/actions-beans/src/main/java/ru/yandex/qatools/actions/beans/AbstractCookieAction.java @@ -0,0 +1,82 @@ +package ru.yandex.qatools.actions.beans; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebDriver.Options; + +import java.util.*; + +/** + * @author Vladislav Bauer + */ + +public abstract class AbstractCookieAction extends WebDriverAction { + + private List> actions = new LinkedList<>(); + + + public AbstractCookieAction addCookies(final Collection newCookies) { + return CollectionUtils.isEmpty(newCookies) ? this : createSubAction(new Action() { + @Override + public void perform(final Options context) { + for (final Cookie newCookie : newCookies) { + context.addCookie(newCookie); + } + } + }); + } + + public AbstractCookieAction addCookie(final Cookie cookie) { + return cookie == null ? this : createSubAction(new Action() { + @Override + public void perform(final Options context) { + context.addCookie(cookie); + } + }); + } + + public AbstractCookieAction deleteCookieNamed(final String name) { + return StringUtils.isBlank(name) ? this : createSubAction(new Action() { + @Override + public void perform(final Options context) { + context.deleteCookieNamed(name); + } + }); + } + + public AbstractCookieAction deleteCookie(final Cookie cookie) { + return cookie == null ? this : createSubAction(new Action() { + @Override + public void perform(final Options context) { + context.deleteCookie(cookie); + } + }); + } + + public AbstractCookieAction deleteAllCookies() { + return createSubAction(new Action() { + @Override + public void perform(final Options context) { + context.deleteAllCookies(); + } + }); + } + + + @Override + public void perform(WebDriver context) { + final Options options = context.manage(); + for (final Action action : actions) { + action.perform(options); + } + } + + + private AbstractCookieAction createSubAction(final Action action) { + actions.add(action); + return this; + } + +} diff --git a/actions-beans/src/main/resources/webdriver-actions.xsd b/actions-beans/src/main/resources/webdriver-actions.xsd index 3786354..3e9f6c3 100644 --- a/actions-beans/src/main/resources/webdriver-actions.xsd +++ b/actions-beans/src/main/resources/webdriver-actions.xsd @@ -5,7 +5,8 @@ xmlns:itn="http://jaxb2-commons.dev.java.net/basic/inheritance" jaxb:extensionBindingPrefixes="itn" targetNamespace="urn:beans.actions.qatools.yandex.ru" - version="2.1"> + version="2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://jaxb2-commons.dev.java.net/basic/inheritance "> @@ -82,7 +83,7 @@ - + @@ -92,4 +93,12 @@ + + + + ru.yandex.qatools.actions.beans.AbstractCookieAction + + + + \ No newline at end of file diff --git a/actions-beans/src/test/java/ru/yandex/qatools/actions/DriverActionsTest.java b/actions-beans/src/test/java/ru/yandex/qatools/actions/DriverActionsTest.java index 136f4ed..cf1b393 100644 --- a/actions-beans/src/test/java/ru/yandex/qatools/actions/DriverActionsTest.java +++ b/actions-beans/src/test/java/ru/yandex/qatools/actions/DriverActionsTest.java @@ -1,26 +1,23 @@ package ru.yandex.qatools.actions; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.*; -import static ru.yandex.qatools.actions.ActionsTestData.TEST_URL; - import org.apache.commons.io.IOUtils; import org.junit.Test; import org.mockito.Mockito; import org.openqa.selenium.*; - -import org.openqa.selenium.Alert; -import org.openqa.selenium.WebDriver; import ru.yandex.qatools.actions.beans.*; import ru.yandex.qatools.actions.mockfactory.MockFactory; -import static org.mockito.Mockito.*; -import static ru.yandex.qatools.actions.ActionsTestData.TEST_URL; - import javax.imageio.ImageIO; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Collections; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.*; +import static ru.yandex.qatools.actions.ActionsTestData.TEST_URL; /** * @author Alexander Tolmachev starlight@yandex-team.ru @@ -28,6 +25,30 @@ */ public class DriverActionsTest { + @Test + public void manageCookiesTest() { + WebDriver driver = MockFactory.mockDriver(); + + String cookieName = "name"; + String cookieValue = "value"; + Cookie cookie = new Cookie(cookieName, cookieValue); + + AbstractCookieAction action = new CookieAction() + .deleteAllCookies() + .addCookie(cookie) + .deleteCookie(cookie) + .addCookies(Collections.singleton(cookie)) + .deleteCookieNamed(cookieName); + + action.perform(driver); + + WebDriver.Options options = driver.manage(); + verify(options).deleteAllCookies(); + verify(options).deleteCookie(cookie); + verify(options).deleteCookieNamed(cookieName); + verify(options, times(2)).addCookie(cookie); + } + @Test public void loadPageTest() { WebDriver driver = MockFactory.mockDriver(); diff --git a/actions-beans/src/test/java/ru/yandex/qatools/actions/mockfactory/MockFactory.java b/actions-beans/src/test/java/ru/yandex/qatools/actions/mockfactory/MockFactory.java index 46f6f2d..a79de34 100644 --- a/actions-beans/src/test/java/ru/yandex/qatools/actions/mockfactory/MockFactory.java +++ b/actions-beans/src/test/java/ru/yandex/qatools/actions/mockfactory/MockFactory.java @@ -21,11 +21,13 @@ public class MockFactory { public static WebDriver mockDriver() { WebDriver driverMock = mock(WebDriver.class); WebDriver.Navigation navigationMock = mock(WebDriver.Navigation.class); + WebDriver.Options optionsMock = mock(WebDriver.Options.class); Alert alert = mock(Alert.class); WebDriver.TargetLocator targetLocator = mock(WebDriver.TargetLocator.class); when(driverMock.switchTo()).thenReturn(targetLocator); when(driverMock.switchTo().alert()).thenReturn(alert); when(driverMock.navigate()).thenReturn(navigationMock); + when(driverMock.manage()).thenReturn(optionsMock); return driverMock; } @@ -59,7 +61,7 @@ public static Select mockSelect(WebDriver driverMock, String xpath, int optionsN WebElement selectMock = mockElementToLocate(driverMock, xpath); when(selectMock.getTagName()).thenReturn("select"); - List options = new ArrayList(optionsNumber); + List options = new ArrayList<>(optionsNumber); for (int i = 0; i < optionsNumber; i++) { WebElement optionMock = mock(WebElement.class); when(optionMock.getAttribute("index")).thenReturn(Integer.toString(i)); @@ -75,7 +77,7 @@ public static Radio mockRadio(WebDriver driverMock, String radioName, int button WebElement radioButtonMock = mockElementToLocate(driverMock, radioXpath); when(radioButtonMock.getAttribute("name")).thenReturn(radioName); - List radioButtons = new ArrayList(buttonsNumber); + List radioButtons = new ArrayList<>(buttonsNumber); radioButtons.add(radioButtonMock); for (int i = 1; i < buttonsNumber; i++) { WebElement buttonMock = mock(WebElement.class);