-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThird.java
More file actions
190 lines (169 loc) · 6.17 KB
/
Copy pathThird.java
File metadata and controls
190 lines (169 loc) · 6.17 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
public class Third {
package automation;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;
import org.json.JSONException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.json.simple.parser.*;
import org.json.simple.JSONObject;
public class Third {
static void user_auth()throws JSONException
{
Scanner in = new Scanner ( System.in) ;
String username , password ;
System.out.println ("Enter username(reg. no.):-");
username = in.next();
System.out.println (" Enter password:-");
password = in.next();
JSONObject obj = new JSONObject() ;
obj.put("USERNAME" ,username ) ;
obj.put("PASSWORD" ,password ) ;
try (FileWriter file = new FileWriter("data.json"))
{
file.write(obj.toString());
file.flush();
file.close();
}
catch ( IOException ex )
{
System.out.println(ex);
}
in.close();
}
public static void main ( String args [ ] ) throws net.minidev.json.parser.ParseException, JSONException, ParseException, org.json.simple.parser.ParseException
{
File f = new File("./data.json");
if(f.exists() == false && f.isDirectory()== false ) {
System.out.println("It's your first time...");
user_auth();
}
System.setProperty("webdriver.chrome.driver","C:/Users/Abhilasha/Downloads/chromedriver_win32/chromedriver.exe");
org.openqa.selenium.WebDriver driver = new ChromeDriver();
String url = "http://14.139.108.229/W27/login.aspx?ReturnUrl=%2fw27%2fMyInfo%2fw27MyInfo.aspx" ;
driver.get(url);
int attempt = 2 ;
while ( attempt > 0 )
{
if ( attempt != 2 )
user_auth();
String name="" , password="" ;
JSONParser parser = new JSONParser();
try (FileReader reader = new FileReader("data.json"))
{
JSONObject obj = (JSONObject)parser.parse(new FileReader("./data.json"));
name = (String)obj.get("USERNAME");
password = ( String) obj.get("PASSWORD");
reader.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
driver.findElement(By.xpath("//*[@id=\"txtUserName\"]")).sendKeys(name);
driver.findElement(By.xpath("//*[@id=\"Password1\"]")).sendKeys(password);
WebElement button = driver.findElement(By.xpath("//*[@id=\"Submit1\"]"));
button.click();
if ( driver.getCurrentUrl() == url )
{
attempt -- ;
driver.findElement(By.xpath("//*[@id=\\\"txtUserName\\\"]")).clear();
try(FileReader reader = new FileReader("data.json"))
{
JSONObject obj = (JSONObject)parser.parse(new FileReader("/data.json"));
name = (String)obj.get("USERNAME");
password = (String)obj.get("PASSWORD");
obj .remove(obj.toString());
reader.close();
if ( attempt == 0 )
{
System.out.println("Wrong UserName or Password");
driver.close();
System.exit(0);
}
System.out.println("Try Re-Login");
}
catch(FileNotFoundException e ) { e.printStackTrace();}
catch(IOException e ) { e.printStackTrace();}
catch(Exception e ) { e.printStackTrace();}
}
else break ;
}
ArrayList < WebElement > rows = (ArrayList<WebElement>) driver.findElements(By.cssSelector("#ctl00_ContentPlaceHolder1_CtlMyLoans1_lblItems"));
int tot = Integer.parseInt((rows.get(0).getText()).toString());
System.out.println("No. of books issued:" + tot) ;
ArrayList < String > due_date = new ArrayList <String >();
for ( int i = 0 ; i < tot ; i ++ )
{
String css = "#ctl00_ContentPlaceHolder1_CtlMyLoans1_grdLoans > tbody > tr:nth-child(" + (i +2) + ") > td:nth-child(5)" ;
ArrayList < WebElement > d = (ArrayList<WebElement>) driver.findElements(By.cssSelector( css ));
String date = d.get(0).getText().toString();
due_date.add( date ) ;
}
for ( String s : due_date )
System.out.println( s ) ;
ArrayList < String > BookName = new ArrayList <String >();
for ( int i = 0 ; i < tot ; i ++ )
{
String css = "#ctl00_ContentPlaceHolder1_CtlMyLoans1_grdLoans > tbody > tr:nth-child(" + (i + 2 )+ ") > td:nth-child(2)";
ArrayList < WebElement > d = (ArrayList<WebElement>) driver.findElements(By.cssSelector( css ));
String Book = d.get(0).getText().toString();
BookName.add( Book ) ;
}
for ( String s : BookName )
System.out.println( s ) ;
ArrayList < WebElement > Button = new ArrayList <WebElement >();
for ( int i = 0 ; i < tot ; i ++ )
{
String css = "#ctl00_ContentPlaceHolder1_CtlMyLoans1_grdLoans_ctl0"+(i+2)+"_Button1";
ArrayList < WebElement > d = (ArrayList<WebElement>) driver.findElements(By.cssSelector( css ));
Button.add(d.get(0) );
}
String months [ ] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"} ;
if ( tot != 0 )
{
for ( int i = 0 ; i < tot ; i ++ )
{
System.out.println( "Book no:-"+ (i+1) + " ");
System.out.print( "Due date is:-" );
String[] d = due_date .get(i).split("-");
System.out.println ( d [ 0 ] + d [ 1 ] + d [ 2 ] ) ;
LocalDateTime now = LocalDateTime.now() ;
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
if ( d [ 2 ] == Integer.toString(year))
{
if ( d [ 1 ] == months [ month ] )
{
if ( d [ 0 ] == Integer.toString(day))
{
if (Button.get(i).isEnabled())
{
Button.get(i).click();
}
else
{
System.out.println ( "RETURN NEEDED !!!");
System.out.println ( "You need to return " + BookName.get( i ) + " today. ") ;
}
}
}
}
}
}
driver.close();
//System.out.println( "THANK YOU");
}
}
}