/
Stepdefs.java
116 lines (92 loc) · 2.85 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
package stepdefs;
import static org.testng.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.core.api.Scenario;
import io.cucumber.java.*;
import io.cucumber.java.en.*;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Stepdefs {
private Scenario scenario;
/*
* @BeforeStep public void beforeStep() throws InterruptedException {
* 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) {
System.out.format("%s step from %s.\n", step.toUpperCase(), scenario.toUpperCase());
}
@Then("Raise exception")
public void raiseExcep() {
Random r = new Random();
boolean flag = r.nextBoolean();
System.out.println(flag);
assertEquals(flag, true);
}
@Then("Do not raise exception")
public void doNotRaiseExcep() {
assertEquals(true, true);
}
@Given("Customer orders the dishes")
public void dataTable(List<List<String>> table) {
System.out.println(table);
}
@Given("the doc string is")
public void docStr(String docStr) {
System.out.println(docStr);
}
@Before(value = "@website")
public void before(Scenario scenario) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
this.scenario = scenario;
}
@After(value = "@website")
public void after() {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
}
private WebDriver driver;
private String site;
@And("Go to {word}")
public void visitweb(String site) throws Exception {
System.out.println(site);
driver.get(site);
this.site = site;
scenario.log("scenario write");
Thread.sleep(5000);
}
/*
* @Before("not @foo") public void before(Scenario scenario) {
* scenario.write("Runs BEFORE scenarios *not* tagged with @foo"); }
*
* @After("not @foo") public void after(Scenario scenario) {
* scenario.write("Runs AFTER scenarios *not* tagged with @foo"); }
*/
@BeforeStep(value = "@website")
public void beforeSite() {
System.out.println("BEFORE SITE");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@AfterStep(value = "@website")
public void afterSite() {
System.out.println("AFTER SITE");
TakesScreenshot ts = (TakesScreenshot) driver;
byte[] screenshot = ts.getScreenshotAs(OutputType.BYTES);
scenario.log("this is my failure message……….");
scenario.attach(screenshot, "image/png", this.site);
driver.quit();
}
}