-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestng.java
More file actions
144 lines (121 loc) · 4.4 KB
/
testng.java
File metadata and controls
144 lines (121 loc) · 4.4 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
package selenium;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testng {
static WebDriver driver;
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd_MM_yy_hh_mm_ss");
LocalDateTime now = LocalDateTime.now();
String curr_date = dtf.format(now);
/*public static WebDriver OpenApp(String BrowserName, String url){
fn_LaunchBrowser(BrowserName);
fn_OpenURL(url);
return driver;
}
public static void fn_OpenURL(String url){
driver.get(url);
driver.manage().window().maximize();
}
public static WebDriver fn_LaunchBrowser(String BrowserName){
if(BrowserName=="CH"){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\workspace\\testng\\src\\main\\java\\testng\\chromedriver.exe");
driver = new ChromeDriver();
}*/
public static void open(String browser, String url) throws InterruptedException{
try{
if(browser.equals("CH")){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\workspace\\testng\\src\\main\\java\\testng\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
}
}
catch(Exception e){
System.out.println(e);
}
}
static public void print(String name){
System.out.println("name is "+name);
}
@BeforeSuite
public void launch(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\workspace\\testng\\src\\main\\java\\testng\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
System.out.println("Before suite called\n");
}
@BeforeTest
public void b4test(){
System.out.println("Execution started\n ");
}
@BeforeMethod
public void b4method(){
System.out.println("Before Method called\n");
}
@Test
public void a_geturl(){
driver.get("https://www.google.com");
System.out.println(driver.getCurrentUrl()+"URL visited\n");
}
@Test
public void b_gmail() throws InterruptedException{
Thread.sleep(1000);
String exp_title = "Sign in – Google accounts";
String url = "https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin";
driver.get(url);
Assert.assertEquals(driver.getTitle(), exp_title);
System.out.println("Gmail login page invoked\n");
}
@Test
public void c_login() throws IOException, InterruptedException {
Properties pro = new Properties();
FileInputStream file = new FileInputStream(System.getProperty("user.dir")+"\\app.properties");
pro.load(file);
driver.findElement(By.xpath(pro.getProperty("email_box_locator"))).sendKeys("email@gmail.com");
driver.findElement(By.xpath(pro.getProperty("next1"))).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(pro.getProperty("pass_locator"))));
WebElement pass = driver.findElement(By.xpath(pro.getProperty("pass_locator")));
pass.sendKeys("password");
WebElement clk2 = driver.findElement(By.xpath(pro.getProperty("next2")));
clk2.click();
Thread.sleep(5000);
}
@Test
public void d_as(){
String exp_title = "Google Account";
String error_msg = "Can't access Google account";
Assert.assertEquals(driver.getTitle(), exp_title, error_msg);
System.out.println("Appropriate site visited");
}
@AfterTest
public void e_ss() throws IOException{
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File("D:\\Selenium_ss\\Success\\Gmail_"+curr_date+"_.png"));
}
@AfterSuite
public void f_ter(){
driver.close();
System.out.println("Driver terminated");
}
}