Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8 Working with cookies - adding, deleting - all that already can WebDriver #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Action<Options>> actions = new LinkedList<>();


public AbstractCookieAction addCookies(final Collection<Cookie> newCookies) {
return CollectionUtils.isEmpty(newCookies) ? this : createSubAction(new Action<Options>() {
@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<Options>() {
@Override
public void perform(final Options context) {
context.addCookie(cookie);
}
});
}

public AbstractCookieAction deleteCookieNamed(final String name) {
return StringUtils.isBlank(name) ? this : createSubAction(new Action<Options>() {
@Override
public void perform(final Options context) {
context.deleteCookieNamed(name);
}
});
}

public AbstractCookieAction deleteCookie(final Cookie cookie) {
return cookie == null ? this : createSubAction(new Action<Options>() {
@Override
public void perform(final Options context) {
context.deleteCookie(cookie);
}
});
}

public AbstractCookieAction deleteAllCookies() {
return createSubAction(new Action<Options>() {
@Override
public void perform(final Options context) {
context.deleteAllCookies();
}
});
}


@Override
public void perform(WebDriver context) {
final Options options = context.manage();
for (final Action<Options> action : actions) {
action.perform(options);
}
}


private AbstractCookieAction createSubAction(final Action<Options> action) {
actions.add(action);
return this;
}

}
13 changes: 11 additions & 2 deletions actions-beans/src/main/resources/webdriver-actions.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -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 ">

<xsd:complexType name="WebDriverAction" abstract="true">
<xsd:annotation>
Expand Down Expand Up @@ -82,7 +83,7 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:complexType>

<xsd:complexType name="TakeScreenshotAction">
<xsd:annotation>
<xsd:appinfo>
Expand All @@ -92,4 +93,12 @@
<xsd:attribute name="path" type="xsd:string" use="required"/>
</xsd:complexType>

<xsd:complexType name="CookieAction">
<xsd:annotation>
<xsd:appinfo>
<itn:extends>ru.yandex.qatools.actions.beans.AbstractCookieAction</itn:extends>
</xsd:appinfo>
</xsd:annotation>
</xsd:complexType>

</xsd:schema>
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
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 [email protected]
* Date: 05.09.12
*/
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<WebElement> options = new ArrayList<WebElement>(optionsNumber);
List<WebElement> options = new ArrayList<>(optionsNumber);
for (int i = 0; i < optionsNumber; i++) {
WebElement optionMock = mock(WebElement.class);
when(optionMock.getAttribute("index")).thenReturn(Integer.toString(i));
Expand All @@ -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<WebElement> radioButtons = new ArrayList<WebElement>(buttonsNumber);
List<WebElement> radioButtons = new ArrayList<>(buttonsNumber);
radioButtons.add(radioButtonMock);
for (int i = 1; i < buttonsNumber; i++) {
WebElement buttonMock = mock(WebElement.class);
Expand Down