Skip to content

Commit

Permalink
Merge pull request #158 from aleksandrlenkov/add-waiting-of-elementsc…
Browse files Browse the repository at this point in the history
…ollections-on-page

fix bug in with waiting ElementCollection items
  • Loading branch information
ivanboklach authored Jul 27, 2021
2 parents 9414f5c + c9b92b0 commit 8ece0c3
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 27 deletions.
84 changes: 63 additions & 21 deletions src/main/java/ru/alfabank/alfatest/cucumber/api/AkitaPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ public abstract class AkitaPage extends ElementsContainer {
*/
private static final String WAITING_APPEAR_TIMEOUT_IN_MILLISECONDS = "8000";

/**
* Список всех элементов страницы
*/
private Map<String, Object> namedElements;
/**
* Список элементов страницы, не помеченных аннотацией "Optional" или "Hidden"
*/
private List<SelenideElement> primaryElements;

/**
* Список элементов страницы, помеченных аннотацией "Hidden"
*/
private List<SelenideElement> hiddenElements;

/**
* Список списочных элементов страницы, не помеченных аннотацией "Optional" или "Hidden"
*/
private List<ElementsCollection> primaryElementCollections;

public AkitaPage() {
super();
}
Expand Down Expand Up @@ -180,6 +199,18 @@ public List<SelenideElement> getHiddenElements() {
return new ArrayList<>(hiddenElements);
}

/**
* Получения всех элементов с типом ElementsCollection, не помеченных аннотацией "Optional" или "Hidden"
* @return
*/
public List<ElementsCollection> getPrimaryElementsCollections() {
if (primaryElementCollections == null) {
primaryElementCollections = readPrimaryElementsCollections();
}

return new ArrayList<>(primaryElementCollections);
}

/**
* Обертка над AkitaPage.isAppeared
* Ex: AkitaPage.appeared().doSomething();
Expand All @@ -204,10 +235,18 @@ public final AkitaPage disappeared() {
*/
protected void isAppeared() {
String timeout = loadProperty("waitingAppearTimeout", WAITING_APPEAR_TIMEOUT_IN_MILLISECONDS);

int timeoutMilliseconds = Integer.parseInt(timeout);

getPrimaryElements().parallelStream().forEach(elem ->
elem.waitUntil(Condition.appear, Integer.valueOf(timeout)));
elem.waitUntil(Condition.appear, timeoutMilliseconds));

getHiddenElements().parallelStream().forEach(elem ->
elem.waitUntil(Condition.hidden, Integer.valueOf(timeout)));
elem.waitUntil(Condition.hidden, timeoutMilliseconds));

getPrimaryElementsCollections().parallelStream()
.forEach(col -> waitElementsUntil(Condition.appear, timeoutMilliseconds, col));

eachForm(AkitaPage::isAppeared);
}

Expand Down Expand Up @@ -254,14 +293,19 @@ public final AkitaPage ieDisappeared() {
/**
* Проверка того, что элементы, не помеченные аннотацией "Optional", отображаются,
* а элементы, помеченные аннотацией "Hidden", скрыты.
* Вместо parallelStream используется stream из-за медленной работы IE
* Вместо parallelStream используется обычный Iterable.forEach() из-за медленной работы IE
*/
protected void isAppearedInIe() {
String timeout = loadProperty("waitingAppearTimeout", WAITING_APPEAR_TIMEOUT_IN_MILLISECONDS);
getPrimaryElements().stream().forEach(elem ->
elem.waitUntil(Condition.appear, Integer.valueOf(timeout)));
getHiddenElements().stream().forEach(elem ->
elem.waitUntil(Condition.hidden, Integer.valueOf(timeout)));

int timeoutMilliseconds = Integer.parseInt(timeout);

getPrimaryElements().forEach(elem -> elem.waitUntil(Condition.appear, timeoutMilliseconds));

getHiddenElements().forEach(elem -> elem.waitUntil(Condition.hidden, timeoutMilliseconds));

getPrimaryElementsCollections().forEach(col -> waitElementsUntil(Condition.appear, timeoutMilliseconds, col));

eachForm(AkitaPage::isAppearedInIe);
}

Expand Down Expand Up @@ -340,20 +384,6 @@ private static AkitaPage castToAkitaPage(Object object) {
return null;
}

/**
* Список всех элементов страницы
*/
private Map<String, Object> namedElements;
/**
* Список элементов страницы, не помеченных аннотацией "Optional" или "Hidden"
*/
private List<SelenideElement> primaryElements;

/**
* Список элементов страницы, помеченных аннотацией "Hidden"
*/
private List<SelenideElement> hiddenElements;

@Override
public void setSelf(SelenideElement self) {
super.setSelf(self);
Expand Down Expand Up @@ -440,6 +470,18 @@ private List<SelenideElement> readWithHiddenElements() {
.collect(toList());
}

/**
* Поиск элементов страницы с типом ElementsCollection, не помеченных как Optional или Hidden
* @return
*/
private List<ElementsCollection> readPrimaryElementsCollections() {
return Arrays.stream(getClass().getDeclaredFields())
.filter(f -> f.getType().equals(ElementsCollection.class))
.filter(f -> f.getDeclaredAnnotation(Optional.class) == null && f.getDeclaredAnnotation(Hidden.class) == null)
.map(f -> (ElementsCollection) Reflection.extractFieldValue(f, this))
.collect(toList());
}

private Object extractFieldValueViaReflection(Field field) {
return Reflection.extractFieldValue(field, this);
}
Expand Down
42 changes: 36 additions & 6 deletions src/test/java/ru/alfabank/steps/WebPageStepsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package ru.alfabank.steps;

import com.codeborne.selenide.SelenideDriver;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.ex.ElementShouldNot;
import cucumber.api.Scenario;
Expand Down Expand Up @@ -50,12 +51,18 @@ static void setup() {
wpis = new WebPageInteractionSteps();
wpvs = new WebPageVerificationSteps();
iis = new InputInteractionSteps();
String inputFilePath = "src/test/resources/AkitaPageMock.html";
String url = new File(inputFilePath).getAbsolutePath();
akitaScenario.setVar("Page", "file://" + url);
String inputFilePath2 = "src/test/resources/RedirectionPage.html";
String url2 = new File(inputFilePath2).getAbsolutePath();
akitaScenario.setVar("RedirectionPage", "file://" + url2);
addPage("Page", "src/test/resources/AkitaPageMock.html");
addPage("Page_without_ElementsCollection", "src/test/resources/AkitaPageMock_without_ElementsCollection.html");
addPage("RedirectionPage", "src/test/resources/RedirectionPage.html");
}

private static void addPage(String name, String htmlFile) {
String absolutePath = new File(htmlFile).getAbsolutePath();

akitaScenario.setVar(
name,
"file://" + absolutePath
);
}

@BeforeEach
Expand Down Expand Up @@ -111,11 +118,34 @@ void testCheckCurrentURLIsNotEqualsAnotherNegative() {

@Test
void testLoadPagePositive() {
SelenideDriver selenideDriver = WebDriverRunner.getSelenideDriver();

selenideDriver.open(akitaScenario.getVar("Page_without_ElementsCollection").toString());

Object page = akitaScenario.getVar("Page");

Thread thread = new Thread(() -> {
try {
System.out.println("start thread");
Thread.sleep(600L);
System.out.println("waiting complete");

selenideDriver.open(page.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}

});

thread.start();

wpis.loadPage("AkitaPageMock");
}

@Test
void testLoadPageNegative() {
WebDriverRunner.getSelenideDriver().open(akitaScenario.getVar("Page_without_ElementsCollection").toString());

assertThrows(IllegalArgumentException.class, () ->
wpis.loadPage("thisPageDoesNotExists"));
}
Expand Down
81 changes: 81 additions & 0 deletions src/test/resources/AkitaPageMock_without_ElementsCollection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!--
Copyright 2017 Alfa Laboratory
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
var HIDEnSHOW = function () {
var element = document.getElementById("ul");
if (element.style.display === "none") {
element.style.display = "block";
}
else element.style.display = "none";
}
</script>
</head>
<body>
<div id="mockId">Serious testing page</div>
<div class="mockCss">
<a name="mockName">321</a>
<button onclick="HIDEnSHOW()" name="SUPERBUTTON">SUPERBUTTON</button>
<button onclick="this.disabled=true" name="goodButton">GoodButton</button>
<button onclick="this.disabled=true" name="mixedButton">English? Русский.</button>
<button disabled="true" id="permDisabledBtn">Disabled :p</button>
<div class="mockXpath">
<p id="ul" style="display: block">Serious testing page</p>
</div>
<div id="hiddenDiv" style="display: none">123</div>

<ul id="list">
<li><a href="#">Three</a></li>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
</ul>

<ul id="list2">
<li><a href="#">Item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item 3</a></li>
</ul>
</div>
<table id="t1" border="1">
<tr><th>First title</th><th>Second title</th></tr>
<tr><td>String 1, cell 1</td><td>String 1, cell 2</td></tr>
<tr><td>String 2, cell 1</td><td>String 2, cell 2</td></tr>
</table>
<form class="searchBlock">
<input type="text" name="searchInput">
<button onclick="this.disabled=true" name="searchButton">SearchButton</button>
</form>
<input type="text" name="normalField">
<input type="text" name="disabledField" readonly disabled="true" value="text">
<input type="text" name="fieldWithText" value="text1 text2 text3">
<textarea type="textarea" name="normalTextAreaWithText"></textarea>
<br><a href="RedirectionPage.html" id="link">LINK</a>
<form>
<input type="email" id="login">
<input type="password" id="psw">
<input type="submit" id="submit">
</form>
<div id="innerText1">inner<p> text </p></div>
<div class="action-buttons__send-to-bank"><button role="button" type="button" class="button button_disabled button_view_extra button_size_l button_width_available button_theme_alfa-on-white" tabindex="-1" disabled=""><span class="button__text">Подписать и отправить</span></button></div>
<form id="multi-file-upload-form" action="/upload" method="post" enctype="multipart/form-data">
<input type="file" class="file" name="multiFile" multiple />
<input type="submit" class="submit"/>
</form>
</body>
</html>

0 comments on commit 8ece0c3

Please sign in to comment.