Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 46ef461

Browse files
committed
Added tests for the identifications package
- ByProducers factory - all ByProducer implementations
1 parent 97d193e commit 46ef461

File tree

11 files changed

+453
-0
lines changed

11 files changed

+453
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package info.novatec.testit.webtester.pagefragments.identification;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.lang.reflect.Method;
6+
7+
import org.junit.Test;
8+
import org.junit.experimental.runners.Enclosed;
9+
import org.junit.runner.RunWith;
10+
import org.openqa.selenium.By;
11+
12+
import info.novatec.testit.webtester.pagefragments.PageFragment;
13+
import info.novatec.testit.webtester.pagefragments.annotations.IdentifyUsing;
14+
import info.novatec.testit.webtester.pagefragments.identification.producers.ClassName;
15+
16+
17+
@RunWith(Enclosed.class)
18+
public class ByProducersTest {
19+
20+
public static class ClassNameTest {
21+
22+
@Test
23+
public void producesCorrectBy() {
24+
By by = ByProducers.className("some-class");
25+
assertThat(by).isInstanceOf(By.ByClassName.class);
26+
assertThat(by).hasToString("By.className: some-class");
27+
}
28+
29+
}
30+
31+
public static class CssSelectorTest {
32+
33+
@Test
34+
public void producesCorrectBy() {
35+
By by = ByProducers.css("#id");
36+
assertThat(by).isInstanceOf(By.ByCssSelector.class);
37+
assertThat(by).hasToString("By.cssSelector: #id");
38+
}
39+
40+
}
41+
42+
public static class IdEndsWithTest {
43+
44+
@Test
45+
public void producesCorrectBy() {
46+
By by = ByProducers.idEndsWith(":partial-id");
47+
assertThat(by).isInstanceOf(By.ByCssSelector.class);
48+
assertThat(by).hasToString("By.cssSelector: [id$=':partial-id']");
49+
}
50+
51+
}
52+
53+
public static class IdStartsWithTest {
54+
55+
@Test
56+
public void producesCorrectBy() {
57+
By by = ByProducers.idStartsWith("partial-id:");
58+
assertThat(by).isInstanceOf(By.ByCssSelector.class);
59+
assertThat(by).hasToString("By.cssSelector: [id^='partial-id:']");
60+
}
61+
62+
}
63+
64+
public static class IdTest {
65+
66+
@Test
67+
public void producesCorrectBy() {
68+
By by = ByProducers.id("someId");
69+
assertThat(by).isInstanceOf(By.ById.class);
70+
assertThat(by).hasToString("By.id: someId");
71+
}
72+
73+
}
74+
75+
public static class LinkTextTest {
76+
77+
@Test
78+
public void producesCorrectBy() {
79+
By by = ByProducers.linkText("a link");
80+
assertThat(by).isInstanceOf(By.ByLinkText.class);
81+
assertThat(by).hasToString("By.linkText: a link");
82+
}
83+
84+
}
85+
86+
public static class NameTest {
87+
88+
@Test
89+
public void producesCorrectBy() {
90+
By by = ByProducers.name("name");
91+
assertThat(by).isInstanceOf(By.ByName.class);
92+
assertThat(by).hasToString("By.name: name");
93+
}
94+
95+
}
96+
97+
public static class PartialLinkTextTest {
98+
99+
@Test
100+
public void producesCorrectBy() {
101+
By by = ByProducers.partialLinkText("partial link");
102+
assertThat(by).isInstanceOf(By.ByPartialLinkText.class);
103+
assertThat(by).hasToString("By.partialLinkText: partial link");
104+
}
105+
106+
}
107+
108+
public static class TagNameTest {
109+
110+
@Test
111+
public void producesCorrectBy() {
112+
By by = ByProducers.tagName("tag");
113+
assertThat(by).isInstanceOf(By.ByTagName.class);
114+
assertThat(by).hasToString("By.tagName: tag");
115+
}
116+
117+
}
118+
119+
public static class XPathTest {
120+
121+
@Test
122+
public void producesCorrectBy() {
123+
By by = ByProducers.xPath("//div/a[1]");
124+
assertThat(by).isInstanceOf(By.ByXPath.class);
125+
assertThat(by).hasToString("By.xpath: //div/a[1]");
126+
}
127+
128+
}
129+
130+
public static class CreateByIdentifyUsing {
131+
132+
@Test
133+
public void producesCorrectBy() throws NoSuchMethodException {
134+
135+
Method method = TestClass.class.getDeclaredMethod("fragment");
136+
IdentifyUsing annotation = method.getAnnotation(IdentifyUsing.class);
137+
138+
By by = ByProducers.createBy(annotation);
139+
assertThat(by).isInstanceOf(By.ByClassName.class);
140+
assertThat(by).hasToString("By.className: some-class");
141+
142+
}
143+
144+
public interface TestClass {
145+
146+
@IdentifyUsing(value = "some-class", how = ClassName.class)
147+
PageFragment fragment();
148+
149+
}
150+
151+
}
152+
153+
public static class ByProducerInstantiation {
154+
155+
@Test(expected = InvalidByProducerException.class)
156+
public void byProducerNeedsDefaultConstructor() throws NoSuchMethodException {
157+
try {
158+
ByProducers.createBy(NoDefaultConstructor.class, "value");
159+
} catch (InvalidByProducerException e) {
160+
assertThat(e).hasMessage("unable to create instance of " + NoDefaultConstructor.class);
161+
assertThat(e).hasCauseInstanceOf(InstantiationException.class);
162+
throw e;
163+
}
164+
}
165+
166+
@Test(expected = InvalidByProducerException.class)
167+
public void defaultConstructorNeedsToBePublic() throws NoSuchMethodException {
168+
try {
169+
ByProducers.createBy(PrivateDefaultConstructor.class, "value");
170+
} catch (InvalidByProducerException e) {
171+
assertThat(e).hasMessage("unable to create instance of " + PrivateDefaultConstructor.class);
172+
assertThat(e).hasCauseInstanceOf(IllegalAccessException.class);
173+
throw e;
174+
}
175+
}
176+
177+
public static class NoDefaultConstructor implements ByProducer {
178+
179+
public NoDefaultConstructor(Object someObject) {
180+
}
181+
182+
@Override
183+
public By createBy(String value) {
184+
return null;
185+
}
186+
187+
}
188+
189+
public static class PrivateDefaultConstructor implements ByProducer {
190+
191+
private PrivateDefaultConstructor() {
192+
}
193+
194+
@Override
195+
public By createBy(String value) {
196+
return null;
197+
}
198+
199+
}
200+
201+
}
202+
203+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class ClassNameTest {
10+
11+
ClassName cut = new ClassName();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy("some-class");
16+
assertThat(by).isInstanceOf(By.ByClassName.class);
17+
assertThat(by).hasToString("By.className: some-class");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("Class Name");
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class CssSelectorTest {
10+
11+
CssSelector cut = new CssSelector();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy("#id");
16+
assertThat(by).isInstanceOf(By.ByCssSelector.class);
17+
assertThat(by).hasToString("By.cssSelector: #id");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("CSS Selector");
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class IdEndsWithTest {
10+
11+
IdEndsWith cut = new IdEndsWith();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy(":partial-id");
16+
assertThat(by).isInstanceOf(By.ByCssSelector.class);
17+
assertThat(by).hasToString("By.cssSelector: [id$=':partial-id']");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("ID Ends With");
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class IdStartsWithTest {
10+
11+
IdStartsWith cut = new IdStartsWith();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy("partial-id:");
16+
assertThat(by).isInstanceOf(By.ByCssSelector.class);
17+
assertThat(by).hasToString("By.cssSelector: [id^='partial-id:']");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("ID Starts With");
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class IdTest {
10+
11+
Id cut = new Id();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy("someId");
16+
assertThat(by).isInstanceOf(By.ById.class);
17+
assertThat(by).hasToString("By.id: someId");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("ID");
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class LinkTextTest {
10+
11+
LinkText cut = new LinkText();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy("a link");
16+
assertThat(by).isInstanceOf(By.ByLinkText.class);
17+
assertThat(by).hasToString("By.linkText: a link");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("Link Text");
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package info.novatec.testit.webtester.pagefragments.identification.producers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
import org.openqa.selenium.By;
7+
8+
9+
public class NameTest {
10+
11+
Name cut = new Name();
12+
13+
@Test
14+
public void producesCorrectBy() {
15+
By by = cut.createBy("name");
16+
assertThat(by).isInstanceOf(By.ByName.class);
17+
assertThat(by).hasToString("By.name: name");
18+
}
19+
20+
@Test
21+
public void hasCorrectToString() {
22+
assertThat(cut).hasToString("Name");
23+
}
24+
25+
}

0 commit comments

Comments
 (0)