-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.java
More file actions
260 lines (238 loc) · 10.1 KB
/
final.java
File metadata and controls
260 lines (238 loc) · 10.1 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package selenium;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.formula.functions.Column;
import org.apache.poi.ss.formula.functions.Even;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.omg.SendingContext.RunTime;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class final_p {
static WebDriver driver;
String adate = new SimpleDateFormat("dd_MM_yy_hh_mm_ss").format(new Date());
ExtentReports report = new ExtentReports("D://final//report_" + adate + "_.html");
ExtentTest fin;
@BeforeSuite
public void startexec() {
System.out.println("Execution of BeforeSuite started\n");
String browser = "chrome";
if (browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\workspace\\Auto\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
} else if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\admin\\workspace\\Auto\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
} else if (browser.equals("ie")) {
System.setProperty("webdriver.ie.driver", "C:\\Users\\admin\\workspace\\Auto\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
} else {
System.out.println("\nNo appropriate browser defined.");
}
System.out.println("Execution of BeforeSuite finished");
}
@BeforeTest
public void geturl() throws IOException {
String adate = new SimpleDateFormat("dd_MM_yy_hh_mm_ss").format(new Date());
report.loadConfig(new File(System.getProperty("user.dir") + "\\extent-config.xml"));
fin = report.startTest("BeforeTest");
String expected_title = "Demo Form for practicing Selenium Automation";
String url = "https://www.toolsqa.com/automation-practice-form/";
driver.get(url);
Assert.assertEquals(driver.getTitle(), expected_title);
File ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ss, new File("D://final//ss_" + adate + "_.png"));
System.out.println("\nExpected title visited---- " + driver.getTitle() + " ----" + "\nURL visited was---- "
+ driver.getCurrentUrl() + " ----");
fin.log(LogStatus.PASS, "Passed for title" + fin.addScreenCapture("D://final//ss_" + adate + "_.png"));
report.endTest(fin);
}
@Test
public void a_form_submit() throws IOException, InterruptedException {
String adate = new SimpleDateFormat("dd_MM_yy_hh_mm_ss").format(new Date());
report.loadConfig(new File(System.getProperty("user.dir") + "\\extent-config.xml"));
fin = report.startTest("Form Test");
String expected_title = "Demo Table for practicing Selenium Automation";
Properties pro = new Properties();
FileInputStream locators = new FileInputStream(System.getProperty("user.dir") + "\\app.properties");
pro.load(locators);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(pro.getProperty("cookie")))).click();
driver.findElement(By.linkText("Link Test")).click();
Assert.assertEquals(driver.getTitle(), expected_title);
System.out.println("\nLink text clicked");
Thread.sleep(2000);
driver.navigate().back();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(pro.getProperty("fname"))))
.sendKeys("Dipanshu");
driver.findElement(By.xpath(pro.getProperty("submit"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(pro.getProperty("lname")))).sendKeys("Jha");
driver.findElement(By.xpath(pro.getProperty("male_radio"))).click();
driver.findElement(By.xpath(pro.getProperty("check1"))).click();
driver.findElement(By.xpath(pro.getProperty("photo"))).click();
Runtime.getRuntime().exec("D:\\final\\final.exe");
Select drp = new Select(driver.findElement(By.xpath(pro.getProperty("dropdown1"))));
drp.selectByVisibleText("Europe");
File ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ss, new File("D://final" + adate + "_.png"));
fin.log(LogStatus.ERROR, driver.getTitle() + fin.addScreenCapture("D://final" + adate + "_.png"));
report.endTest(fin);
}
@Test
public void b_alert_handle() throws IOException, InterruptedException{
fin = report.startTest("Alerts");
Properties pro = new Properties();
FileInputStream locators = new FileInputStream(System.getProperty("user.dir") + "\\app.properties");
pro.load(locators);
String expected_title = "Selenium Easy Demo - Automate All Scenarios";
driver.get("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");
Assert.assertEquals(driver.getTitle(), expected_title);
driver.findElement(By.xpath(pro.getProperty("simple_alert"))).click();
Alert al = driver.switchTo().alert();
System.out.println("Text for alert was "+al.getText());
al.accept();
Thread.sleep(1000);
driver.findElement(By.xpath(pro.getProperty("an_alert"))).click();
Alert an = driver.switchTo().alert();
System.out.println("Alert text "+an.getText());
an.dismiss();
fin.log(LogStatus.INFO, "alert done");
report.endTest(fin);
}
@Test
public void c_events() throws IOException{
Properties pro = new Properties();
fin = report.startTest("Events");
FileInputStream locators = new FileInputStream(System.getProperty("user.dir") + "\\app.properties");
pro.load(locators);
String exp = "Simple Context Menu";
String adate = new SimpleDateFormat("dd_MM_yy_hh_mm_ss").format(new Date());
driver.get("http://demo.guru99.com/test/simple_context_menu.html");
Actions act = new Actions(driver);
WebElement dc = driver.findElement(By.xpath(pro.getProperty("double_cl")));
try{
Assert.assertEquals(driver.getTitle(), exp);
act.doubleClick(dc).perform();
driver.switchTo().alert().accept();
}
catch (Error e) {
// TODO: handle exception
System.out.println("Assert failed ");
File ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ss, new File("D://final" + adate + "_.png"));
fin.log(LogStatus.FAIL, "failed for title "+fin.addScreenCapture("D://final" + adate + "_.png"));
}
report.endTest(fin);
}
@Test
public void d_excel_read() throws IOException {
File exfile = new File("D:\\excelr.xlsx");
FileInputStream fis = new FileInputStream(exfile);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheetAt(0);
int rowcount = ws.getLastRowNum() - ws.getFirstRowNum();
int colcount = ws.getRow(0).getLastCellNum() - 1;
for (int i = 0; i < rowcount + 1; i++) {
XSSFRow row = ws.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
String celldt = row.getCell(j).getStringCellValue();
if (celldt.equals("Y")) {
String data1 = row.getCell(colcount - 2).getStringCellValue();
String data2 = row.getCell(colcount - 1).getStringCellValue();
System.out.println(data1);
System.out.println(data2);
// String dt = (data1+" "+data2);
// System.out.println(dt);
/*
* String[] parts = dt.split(" "); String user = parts[0];
* // 004 String pass = parts[1]; // 034556
* System.out.println(user);
*/
// System.out.println(data2);
}
}
System.out.println();
}
}
@Test
public void e_excel_write() throws IOException {
String adate = new SimpleDateFormat("dd_MM_yy_hh_mm_ss").format(new Date());
File exc = new File("D://excelr.xlsx");
FileInputStream fis = new FileInputStream(exc);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheetAt(0);
int rowcount = ws.getLastRowNum() - ws.getFirstRowNum();
int colcount = ws.getRow(0).getLastCellNum() - 1;
for (int i = 0; i < rowcount + 1; i++) {
XSSFRow row = ws.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
String cell_data = row.getCell(j).getStringCellValue();
if (cell_data.equals("Y")) {
// ws.createRow(2).createCell(colcount).setCellValue("");
row.createCell(colcount + 1).setCellValue("PASSED");
/*
* String dt1 =
* row.getCell(colcount-1).getStringCellValue(); String dt2
* = row.getCell(colcount-2).getStringCellValue();
* System.out.println(dt1+" "+dt2);
*
* System.out.println("Changing passowrds");
*/
String data = ws.getRow(rowcount - 2).getCell(colcount - 1).getStringCellValue();
System.out.println(data);
if (data.equals("a1234")) {
XSSFCell cell = ws.getRow(rowcount - 2).getCell(colcount - 1);
cell.setCellValue("Changed_pass");
} else {
System.out.println("not printed " + data);
}
FileOutputStream outputStream = new FileOutputStream("D://JavaBooks" + adate + "_.xlsx");
wb.write(outputStream);
outputStream.close();
}
}
System.out.println();
}
}
@Test
public void zz() {
String adate = new SimpleDateFormat("dd_MM_YY_hh_mm_ss").format(new Date());
System.out.println(adate);
}
@AfterTest
public void aft() {
report.flush();
System.out.println("TEst executed successfully");
}
}