forked from npolyakova/bad-test-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadE2ESecondExample.java
123 lines (98 loc) · 3.5 KB
/
BadE2ESecondExample.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.example;
import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
import com.github.javafaker.Faker;
import io.qameta.allure.Step;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
public class BadE2ESecondExample {
Faker faker = new Faker();
SelenideElement inputUsername = $("#user-name");
SelenideElement inputPassword = $("#password");
SelenideElement buttonLogin = $("#login-button");
@BeforeEach
public void setUp() {
openAuthorizationPage();
}
// Instead of writing the full address, it's better to use the baseUrl property
@Step("Open the login page")
public void openAuthorizationPage() {
open("https://www.saucedemo.com");
inputUsername.shouldBe(Condition.visible);
}
// A maze of conditional logic. Even if we hide filling in the credentials, this
// won't look much better.
@Step
public void authorize(Boolean username, Boolean password) {
String trueUsername = "standard_user";
String truePassword = "secret_sauce";
if (username && password) {
inputUsername.setValue(trueUsername);
inputPassword.setValue(truePassword);
buttonLogin.click();
checkUserAuthorized();
} else if (username) {
inputUsername.setValue(trueUsername);
inputPassword.setValue(faker.internet().password());
buttonLogin.click();
checkUserNotAuthorized();
} else if (password) {
inputUsername.setValue(faker.name().username());
inputPassword.setValue(truePassword);
buttonLogin.click();
checkUserNotAuthorized();
} else {
inputUsername.setValue(faker.name().username());
inputPassword.setValue(faker.internet().password());
buttonLogin.click();
checkUserNotAuthorized();
}
}
@Step
public void checkUserAuthorized() {
$("[data-test='secondary-header']").shouldBe(Condition.visible);
}
// Too many checks - and none of them are definitive
@Step
public void checkUserNotAuthorized() {
$(".login_logo").shouldBe(Condition.visible);
$("#login_button_container").shouldBe(Condition.visible);
inputUsername.shouldBe(Condition.visible);
inputPassword.shouldBe(Condition.visible);
}
// It's not clear what "true" and "false" mean here, you
// have to look up the method.
// It' not clear what data the test is using.
@Test
public void shouldAuthorizeUser() {
authorize(true, true);
checkUserAuthorized();
}
// This is the exact same test with different parameters.
@Test
public void shouldNotAuthorizeUserWithInvalidPassword() {
authorize(true, false);
checkUserNotAuthorized();
}
// Same here.
@Test
public void shouldNotAuthorizeUserWithInvalidUsername() {
authorize(false, true);
checkUserNotAuthorized();
}
// The authorization method is not used.
@Test
public void shouldNotAuthorizeUserWithBlankInputs() {
inputUsername.setValue(" ");
inputPassword.setValue(" ");
buttonLogin.click();
checkUserNotAuthorized();
}
@Test
public void shouldNotAuthorizeUserWithEmptyInputs() {
buttonLogin.click();
checkUserNotAuthorized();
}
}