-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleTest.java
More file actions
105 lines (90 loc) · 4.28 KB
/
ExampleTest.java
File metadata and controls
105 lines (90 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package tests;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
import de.isys.selrep.Fallbacks;
import de.isys.selrep.UITest;
import de.isys.selrep.UITestException;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;
import static com.codeborne.selenide.CollectionCondition.*;
import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Selectors.*;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.WebDriverRunner.source;
@Fallbacks(baseUrl="https://www.isys.de/", apiUrl1="https://24pullrequests.com/projects.json")
public class ExampleTest extends UITest {
@Test
public void homepage() {
this.run("Homepage", "check if homepage contains all expected elements", settings -> {
open(BASE_URL);
acceptCookieBannerIfNecessary();
report.startSection("Header");
SelenideElement context = $(".vs-main-navigation").should(appear);
checkLogo(context, BASE_URL);
ElementsCollection allVisibleLinks = $(".vs-main").$$("a[href]").filterBy(visible);
clickRandomSameSiteLinkFrom(allVisibleLinks, BASE_URL);
checkLogo($(".vs-main-navigation"), BASE_URL);
report.info("Random same-site Link clicked, Logo still present");
open(BASE_URL);
report.pass("Header Section ok", false); // this ends the section
report.startSection("Content Area");
settings.setTimeout(8000l); // for UI operations that need more time to complete
$$(".wp-block-columns").filterBy(visible).shouldHave(sizeGreaterThanOrEqual(1));
settings.restoreDefaultTimeout();
report.pass("Content Section ok");
report.startSection("Footer");
$(byTagName("footer")).$(withText("iSYS Software GmbH")).scrollTo().should(appear);
report.pass("Footer Section ok");
}, result -> {
report.info("This is called after the test has finished (whether successfully or not) and can be used to clean up", false);
if (result.getThrowable() != null) {
report.info("This test has thrown: " + result.getThrowable().getClass().getName(), false);
}
});
}
private void acceptCookieBannerIfNecessary() {
SelenideElement acceptCookiesButton = $("#CookieBoxSaveButton");
if (acceptCookiesButton.exists() && acceptCookiesButton.isDisplayed()) {
report.info("Cookie Notice appeared");
acceptCookiesButton.click();
$("#CookieBoxSaveButton").shouldNotBe(visible);
report.info("Cookies accepted");
}
}
@Test
public void firefoxTest() {
this.run("Homepage with Firefox", "check Homepage with different Browser", settings -> {
settings.setBrowser("firefox");
Selenide.closeWebDriver(); // start with a new webdriver
open(BASE_URL);
acceptCookieBannerIfNecessary();
checkLogo($(".vs-main-navigation"), BASE_URL);
report.pass("Homepage loads with Firefox");
});
}
@Test
public void callExternalApi() {
this.run("External API", "check if external APIs can be configured", settings -> {
open(API_URL_1);
if(! source().contains("github_url")) {
throw new UITestException("API call not successful!");
}
report.info("External API called");
});
}
private void checkLogo(SelenideElement context, String baseUrl) {
SelenideElement logo = context.$$(".vs-logo a").filterBy(visible).shouldHave(size(1)).get(0);
logo.shouldHave(attribute("href", baseUrl));
report.info("Logo found on page " + Selenide.title());
}
private void clickRandomSameSiteLinkFrom(ElementsCollection allLinks, String baseUrl) {
SelenideElement randomLink = null;
while (randomLink == null || !randomLink.attr("href").startsWith(baseUrl)) {
randomLink = allLinks.get(RandomUtils.nextInt(0, allLinks.size()));
}
randomLink.scrollIntoView(false);
report.info("Random Link: " + randomLink.attr("href"));
randomLink.click();
}
}