/
Stepdefs.java
210 lines (168 loc) · 5.57 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package stepdefs;
import static org.testng.Assert.assertEquals;
//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 org.testng.SkipException;
import org.testng.SkipException;
import io.cucumber.java.After;
import io.cucumber.java.AfterStep;
import io.cucumber.java.Before;
import io.cucumber.java.BeforeStep;
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;
import sun.security.util.PendingException;
public class Stepdefs {
private Scenario scenario;
@Before(value = "not @failure")
public void before(Scenario scenario) {
this.scenario = scenario;
scenario.log("BEFORE HI");
scenario.log("BEFORE HELLO");
}
@After(value = "not @failure")
public void after(Scenario scenario) {
this.scenario = scenario;
scenario.log("AFTER HI");
scenario.log("AFTER HELLO");
}
@Before(value = "@failure")
public void beforeFailure(Scenario scenario) { //
this.scenario = scenario;
scenario.log("FAILURE HI");
scenario.log("FAILURE HELLO");
throw new RuntimeException();
}
@After(value = "@failure")
public void afterFailure() { //
scenario.log("FAILURE HI");
scenario.log("FAILURE HELLO");
throw new RuntimeException();
}
@BeforeStep(value = "@failure")
public void beforeStepFailure() { //
}
@AfterStep(value = "@failure")
public void afterStepFailure() { //
}
@Given("Hook failure step")
public void hook_failure_step() throws InterruptedException {
// System.out.println("Failure step");
Thread.sleep(500);
scenario.log("FAILURE STEP HI");
scenario.log("FAILURE STEP HELLO");
}
@Given("Skip hook failure step")
public void skip_hook_failure_step() throws InterruptedException {
Thread.sleep(250);
}
@Given("Hello background")
public void background() throws InterruptedException {
this.scenario.log("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.log("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("Raise exception Skip")
public void raiseExcepSkip() throws InterruptedException {
// throw new SkipException("Sample skip");
}
@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(2000);
// System.out.println(table);
}
@Given("the doc string is")
public void docStr(String docStr) throws InterruptedException {
Thread.sleep(2000);
// 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.log("scenario website name - " + site);
}
@BeforeStep(value = "@website or @large", order = 1)
public void beforeSite(Scenario scenario) throws Exception {
this.scenario = scenario;
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(500);
scenario.log("HELLO THERE!!! ");
}
@BeforeStep(value = "@website", order = 2)
public void beforeSite2() {
scenario.log("GOOD BYE!!! ");
}
@AfterStep(value = "@website or @large", order = 2)
public void afterSite() {
TakesScreenshot ts = (TakesScreenshot) driver;
byte[] screenshot = ts.getScreenshotAs(OutputType.BYTES);
scenario.log("HELLO THERE!!! " + this.site);
scenario.attach(screenshot, "image/png", this.site);
driver.quit();
}
@AfterStep(value = "@website", order = 1)
public void afterSite2() {
scenario.log("GOOD BYE!!! " + this.site);
}
@Given("Pending step definition")
public void pendingStep() throws PendingException {
throw new PendingException();
}
@Given("Skipped step definition")
public void skippedStep() {
throw new SkipException("SKip it");
}
@Given("Go to capture 2 images in one step")
public void twoImages() throws Exception {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(500);
driver.get("https://github.com/");
Thread.sleep(500);
TakesScreenshot ts = (TakesScreenshot) driver;
byte[] screenshot = ts.getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", "github");
driver.get("https://stackoverflow.com/");
Thread.sleep(500);
ts = (TakesScreenshot) driver;
screenshot = ts.getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", "stackoverflow");
driver.quit();
}
}