Skip to content

Commit

Permalink
#354 remove usages static Configuration fields in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 22, 2018
1 parent c9cfc4b commit a80f8ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.stream.IntStream;

import static com.codeborne.selenide.CollectionCondition.size;
import static com.codeborne.selenide.Configuration.browser;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doThrow;
Expand All @@ -37,7 +36,6 @@ class ElementsCollectionTest implements WithAssertions {

@BeforeEach
final void mockWebDriver() {
browser = null;
when(source.driver()).thenReturn(driver);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.codeborne.selenide.impl;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Screenshots;
import com.codeborne.selenide.SelenideConfig;
import com.codeborne.selenide.SelenideDriver;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.ex.ElementNotFound;
Expand All @@ -11,7 +12,6 @@
import com.codeborne.selenide.logevents.SelenideLogger;
import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,7 +33,6 @@
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Condition.value;
import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.impl.SelenideElementProxy.shouldRetryAfterError;
import static com.codeborne.selenide.logevents.LogEvent.EventStatus.FAIL;
import static com.codeborne.selenide.logevents.LogEvent.EventStatus.PASS;
Expand All @@ -47,18 +46,14 @@ class SelenideElementProxyTest implements WithAssertions {

private RemoteWebDriver webdriver = mock(RemoteWebDriver.class);
private WebElement element = mock(WebElement.class);

@AfterAll
static void restoreOldValues() {
Configuration.screenshots = true;
}
private SelenideConfig config = new SelenideConfig()
.screenshots(false)
.timeout(3)
.pollingInterval(1);
private SelenideDriver driver = new SelenideDriver(config, webdriver);

@BeforeEach
void mockWebDriver() {
Configuration.timeout = 3;
Configuration.pollingInterval = 1;
Configuration.screenshots = false;

WebDriverRunner.setWebDriver(webdriver);
when(webdriver
.executeScript(anyString(), any(WebElement.class)))
Expand All @@ -72,11 +67,6 @@ void mockWebDriver() {
when(element.isDisplayed()).thenReturn(true);
}

@AfterEach
void tearDown() {
WebDriverRunner.closeWebDriver();
}

@AfterEach
void after() {
SelenideLogger.removeListener("test");
Expand All @@ -86,76 +76,76 @@ void after() {
void elementShouldBeVisible() {
when(element.isDisplayed()).thenReturn(true);
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
$("#firstName").shouldBe(visible);
driver.find("#firstName").shouldBe(visible);
}

@Test
void elementNotFound() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(null);
assertThatThrownBy(() -> $("#firstName").shouldBe(visible))
assertThatThrownBy(() -> driver.find("#firstName").shouldBe(visible))
.isInstanceOf(ElementNotFound.class);
}

@Test
void elementFoundButNotMatched() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(element.isDisplayed()).thenReturn(false);
assertThatThrownBy(() -> $("#firstName").shouldBe(visible))
assertThatThrownBy(() -> driver.find("#firstName").shouldBe(visible))
.isInstanceOf(ElementShould.class);
}

@Test
void elementFoundButInvisible() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(element.isDisplayed()).thenThrow(new WebDriverException("failed to call isDisplayed"));
assertThatThrownBy(() -> $("#firstName").shouldBe(visible))
assertThatThrownBy(() -> driver.find("#firstName").shouldBe(visible))
.isInstanceOf(ElementShould.class);
}

@Test
void elementFoundButConditionCheckFailed() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(element.isDisplayed()).thenReturn(true);
assertThatThrownBy(() -> $("#firstName").shouldHave(text("goodbye")))
assertThatThrownBy(() -> driver.find("#firstName").shouldHave(text("goodbye")))
.isInstanceOf(ElementShould.class);
}

@Test
void elementNotFoundAsExpected() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(null);
$("#firstName").shouldNotBe(exist);
$("#firstName").shouldNotBe(present);
$("#firstName").should(disappear);
$("#firstName").shouldNotBe(visible);
$("#firstName").shouldNotBe(enabled);
$("#firstName").shouldNotHave(text("goodbye"));
driver.find("#firstName").shouldNotBe(exist);
driver.find("#firstName").shouldNotBe(present);
driver.find("#firstName").should(disappear);
driver.find("#firstName").shouldNotBe(visible);
driver.find("#firstName").shouldNotBe(enabled);
driver.find("#firstName").shouldNotHave(text("goodbye"));
}

@Test
void elementNotFoundAsExpected2() {
when(webdriver.findElement(By.cssSelector("#firstName")))
.thenThrow(new WebDriverException("element is not found and this is expected"));
$("#firstName").shouldNot(exist);
$("#firstName").shouldNotBe(present);
$("#firstName").should(disappear);
$("#firstName").shouldNotBe(visible);
$("#firstName").shouldNotBe(enabled);
$("#firstName").shouldNotHave(text("goodbye"));
driver.find("#firstName").shouldNot(exist);
driver.find("#firstName").shouldNotBe(present);
driver.find("#firstName").should(disappear);
driver.find("#firstName").shouldNotBe(visible);
driver.find("#firstName").shouldNotBe(enabled);
driver.find("#firstName").shouldNotHave(text("goodbye"));
}

@Test
void webdriverReportsInvalidXpath_using_should() {
when(webdriver.findElement(By.cssSelector("#firstName")))
.thenThrow(new InvalidSelectorException("Error INVALID_EXPRESSION_ERR ups"));
assertThatThrownBy(() -> $("#firstName").should(disappear))
assertThatThrownBy(() -> driver.find("#firstName").should(disappear))
.isInstanceOf(InvalidSelectorException.class);
}

@Test
void webdriverReportsInvalidXpath_using_shouldNot() {
when(webdriver.findElement(By.cssSelector("#firstName")))
.thenThrow(new InvalidSelectorException("Error INVALID_EXPRESSION_ERR ups"));
assertThatThrownBy(() -> $("#firstName").shouldNot(exist))
assertThatThrownBy(() -> driver.find("#firstName").shouldNot(exist))
.isInstanceOf(InvalidSelectorException.class);
}

Expand All @@ -164,7 +154,7 @@ void setValueShouldNotFailIfElementHasDisappearedWhileEnteringText() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(webdriver.executeScript(anyString(), any()))
.thenThrow(new StaleElementReferenceException("element disappeared after entering text"));
$("#firstName").setValue("john");
driver.find("#firstName").setValue("john");
}

@Test
Expand All @@ -173,7 +163,7 @@ void shouldLogSetValueSubject() {
SelenideLogger.addListener("test", createListener(selector, "set value", PASS));

when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
SelenideElement selEl = $("#firstName");
SelenideElement selEl = driver.find("#firstName");
selEl.setValue("ABC");
}

Expand All @@ -197,7 +187,7 @@ void shouldLogShouldSubject() {

when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(element.getAttribute("value")).thenReturn("ABC");
SelenideElement selEl = $("#firstName");
SelenideElement selEl = driver.find("#firstName");
selEl.shouldHave(value("ABC"));
}

Expand All @@ -208,7 +198,7 @@ void shouldLogShouldNotSubject() {

when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(element.getAttribute("value")).thenReturn("wrong value");
SelenideElement selEl = $("#firstName");
SelenideElement selEl = driver.find("#firstName");
selEl.shouldNotHave(value("ABC"));
}

Expand All @@ -220,7 +210,7 @@ void shouldLogFailedShouldNotSubject() {
when(webdriver.findElement(By.cssSelector("#firstName"))).thenReturn(element);
when(element.getAttribute("value")).thenReturn("wrong value");

assertThatThrownBy(() -> $("#firstName").shouldHave(value("ABC")))
assertThatThrownBy(() -> driver.find("#firstName").shouldHave(value("ABC")))
.isInstanceOf(ElementShould.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
import com.codeborne.selenide.SelenideConfig;
import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import static com.codeborne.selenide.Browsers.CHROME;
import static com.codeborne.selenide.Configuration.browser;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
Expand All @@ -38,14 +35,9 @@ private WebElement createWebElement() {
return element;
}

@BeforeEach
final void mockWebDriver() {
browser = null;
}

@Test
void toStringPrintsTagNameWithAllAttributes() {
browser = CHROME;
config.browser(new SelenideConfig.SelenideBrowserConfig().browser("chrome"));
when(((JavascriptExecutor) webDriver)
.executeScript(anyString(), any()))
.thenReturn(ImmutableMap.of("id", "id1", "class", "class1 class2", "data-binding", "to-name"));
Expand Down

0 comments on commit a80f8ef

Please sign in to comment.