/
Stepdefs.java
179 lines (144 loc) · 4.75 KB
/
Stepdefs.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package stepdefs;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.Random;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.After;
import io.cucumber.java.AfterStep;
import io.cucumber.java.Before;
import io.cucumber.java.BeforeStep;
import io.cucumber.java.PendingException;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
/*import cucumber.api.*;
import cucumber.api.java.*;
import cucumber.api.java.en.*;*/
import io.github.bonigarcia.wdm.WebDriverManager;
public class Stepdefs {
private Scenario scenario;
@Before(value = "not @failure")
public void before(Scenario scenario) {
this.scenario = scenario;
scenario.write("BEFORE HI");
scenario.write("BEFORE HELLO");
}
@After(value = "not @failure")
public void after(Scenario scenario) {
this.scenario = scenario;
scenario.write("AFTER HI");
scenario.write("AFTER HELLO");
}
@Before(value = "@failure")
public void beforeFailure(Scenario scenario) { //
System.out.println("Before failure");
this.scenario = scenario;
scenario.write("FAILURE HI");
scenario.write("FAILURE HELLO");
throw new RuntimeException();
}
@After(value = "@failure")
public void afterFailure() { //
System.out.println("After failure");
scenario.write("FAILURE HI");
scenario.write("FAILURE HELLO");
throw new RuntimeException();
}
@BeforeStep(value = "@failure")
public void beforeStepFailure() { //
System.out.println("Before Step failure");
}
@AfterStep(value = "@failure")
public void afterStepFailure() { //
System.out.println("After Step failure");
}
@Given("Hook failure step")
public void hook_failure_step() throws InterruptedException {
// System.out.println("Failure step");
Thread.sleep(500);
scenario.write("FAILURE STEP HI");
scenario.write("FAILURE STEP HELLO");
}
@Given("Skip hook failure step")
public void skip_hook_failure_step() throws InterruptedException {
Thread.sleep(250);
}
@Given("{string} background")
public void background(String type) throws InterruptedException {
System.out.format("%s type background. \n", type);
this.scenario.write("background");
Thread.sleep(250);
}
@Given("Write a {string} step with precondition in {string}")
@When("Complete action in {string} step in {string}")
@Then("Validate the outcome in {string} step in {string}")
public void step(String step, String scenario) throws InterruptedException {
System.out.format("%s step from %s.\n", step.toUpperCase(), scenario.toUpperCase());
this.scenario.write("log HATE THIS");
Thread.sleep(1000);
}
@Then("Raise exception")
public void raiseExcep() throws InterruptedException {
Thread.sleep(2500);
Random r = new Random();
boolean flag = r.nextBoolean();
assertEquals(flag, true);
}
@Then("Do not raise exception")
public void doNotRaiseExcep() throws InterruptedException {
Thread.sleep(1500);
assertEquals(true, true);
}
@Given("Customer orders the dishes")
public void dataTable(List<List<String>> table) throws InterruptedException {
Thread.sleep(4000);
System.out.println(table);
}
@Given("the doc string is")
public void docStr(String docStr) throws InterruptedException {
Thread.sleep(4000);
System.out.println(docStr);
}
private WebDriver driver;
private String site;
@And("Go to {word}")
public void visitweb(String site) throws Exception {
driver.get(site);
this.site = site;
scenario.write("scenario website name - " + site);
Thread.sleep(500);
}
@BeforeStep(value = "@website", order = 1)
public void beforeSite(Scenario scenario) {
this.scenario = scenario;
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
scenario.write("HELLO THERE!!! ");
}
@BeforeStep(value = "@website", order = 2)
public void beforeSite2() {
scenario.write("GOOD BYE!!! ");
}
@AfterStep(value = "@website", order = 2)
public void afterSite() {
TakesScreenshot ts = (TakesScreenshot) driver;
byte[] screenshot = ts.getScreenshotAs(OutputType.BYTES);
scenario.write("HELLO THERE!!! " + this.site);
scenario.embed(screenshot, "image/png", this.site);
driver.quit();
}
@AfterStep(value = "@website", order = 1)
public void afterSite2() {
scenario.write("GOOD BYE!!! " + this.site);
}
@Given("Pending step definition methods")
public void pending_step_definition_methods() {
throw new PendingException();
}
}