-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdoTarget_updated.txt
More file actions
561 lines (482 loc) · 20.5 KB
/
Copy pathdoTarget_updated.txt
File metadata and controls
561 lines (482 loc) · 20.5 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
*
* @author kylea
*/
public class doTarget extends JFrame implements ActionListener {
//-------JButton------//
public JButton engineBtn;
//------JTextField----//
private JPasswordField ccField;
private JPasswordField cvvField;
private JTextField Wait;
private JTextField sWait;
private JTextField serWait;
private JTextField sLength;
//-------JLabel------//
//JLabel title = new JLabel("PS5 Preorder Script: Target");
JLabel wLabel = new JLabel("Wait");
JLabel sLabel = new JLabel("ShortWait");
JLabel serLabel = new JLabel("ShorterWait");
JLabel sleepLabel = new JLabel("SleepLength");
JLabel CC = new JLabel("Enter CC: ");
JLabel CVV = new JLabel("Enter CVV: ");
//------Font----//
private Font f1 = new Font("serif",Font.BOLD,12);
WebDriver driver;
WebDriverWait wait;
WebDriverWait shortWait;
WebDriverWait shorterWait;
int sleepLength = 45;
boolean isUnavail = false;
boolean isDone = false;
int currentStep = 0;
String[] stepStrings = { "Preorder", "SFL-AddToCart"};
String[] choicesStrings = { "PS5 Physical", "PS5 Digital", "PS5 Headset"};
JComboBox stepList;
JComboBox choiceList;
String URL = "https://www.target.com/p/playstation-5-console/-/A-81114595";
JTextArea feedback;
public static void main(String[] args){
doTarget tS = new doTarget();
tS.setSize(180,300);
tS.setVisible(true);
tS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tS.setResizable(false);
}
public doTarget() {
setTitle("Target");
Container myContainer = getContentPane();
myContainer.setLayout(new FlowLayout());
//ButtonClicker myListener = new ButtonClicker();
//title.setFont(f1);
//myContainer.add(title);
myContainer.add(wLabel);
Wait = new JTextField(2);
Wait.setText("10");
myContainer.add(Wait);
myContainer.add(sLabel);
sWait = new JTextField(2);
sWait.setText("4");
myContainer.add(sWait);
myContainer.add(serLabel);
serWait = new JTextField(2);
serWait.setText("2");
myContainer.add(serWait);
myContainer.add(sleepLabel);
sLength = new JTextField(2);
sLength.setText("30");
myContainer.add(sLength);
myContainer.add(CC);
ccField = new JPasswordField(16);
myContainer.add(ccField);
//ccField.addActionListener(this);
myContainer.add(CVV);
cvvField = new JPasswordField (3);
myContainer.add(cvvField);
//cvvField.addActionListener(myListener);
choiceList = new JComboBox(choicesStrings);
choiceList.setSelectedIndex(0);
choiceList.setVisible(false);
choiceList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
switch(choiceList.getSelectedIndex()){
case 0:
URL = "https://www.target.com/p/playstation-5-console/-/A-81114595";
break;
case 1:
URL = "https://www.target.com/p/playstation-5-digital-edition-console/-/A-81114596#lnk=sametab";
break;
case 2:
URL = "https://www.target.com/p/sony-pulse-3d-wireless-gaming-headset-for-playstation-5/-/A-81114474";
break;
}
JOptionPane.showMessageDialog( null, "This URL Setting only applies if starting at Preorder step", "Warning", JOptionPane.WARNING_MESSAGE );
}
});
myContainer.add(choiceList);
stepList = new JComboBox(stepStrings);
stepList.setSelectedIndex(1);
stepList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
switch(stepList.getSelectedIndex()){
case 0:
choiceList.setVisible(true);
JOptionPane.showMessageDialog( null, "Choose which item", "Warning", JOptionPane.WARNING_MESSAGE );
break;
case 1:
choiceList.setVisible(false);
break;
}
}
});
myContainer.add(stepList);
engineBtn = new JButton("Start");
myContainer.add(engineBtn);
engineBtn.addActionListener(this);
engineBtn.setActionCommand("Start");
//feedback=new JTextArea("Echo Echo ");
//feedback.setBounds(10,30, 200,200);
// myContainer.add(feedback);
}
public void doSleep(int Length){
try{
TimeUnit.SECONDS.sleep(Length);
}
catch(Exception e){
}
}
public void doStepOne(){
}
public void doStepTwo(){
String preorderXpath ="//button[@data-test='preorderButton']";
currentStep = 2;
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(preorderXpath)));
WebElement preorderButton = driver.findElement(By.xpath(preorderXpath));
preorderButton.click();
}
public void doStepThree(){
currentStep = 3;
String errorX ="//button[@data-test='errorContent-okButton']";
//"//div[@data-test='flyoutTypeAddToCartError']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(errorX)));
WebElement errorB = driver.findElement(By.xpath(errorX));
if (errorB.isDisplayed()){
errorB.click();
this.doALap(currentStep -1);
return;
}
}
public void doStepFour(){
currentStep = 4;
String declineCoverageXPATH = "//button[@data-test=\'espModalContent-declineCoverageButton\']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(declineCoverageXPATH)));
WebElement declineCoverageButton = driver.findElement(By.xpath(declineCoverageXPATH));
declineCoverageButton.click();
}
public void doStepFive(){
currentStep = 5;
String viewCartXPATH = "//button[@data-test=\'addToCartModalViewCartCheckout\']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(viewCartXPATH)));
WebElement viewCartButton = driver.findElement(By.xpath(viewCartXPATH));
viewCartButton.click();
}
public void doStepSix(){
currentStep = 6;
String checkoutXPATH = "//button[@data-test=\'checkout-button\']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(checkoutXPATH)));
WebElement checkoutButton = driver.findElement(By.xpath(checkoutXPATH));
checkoutButton.click();
}
public void doStepSeven(){
currentStep = 7;
String addX = "(//button[@data-test='moveSFLToCartBtn'])[1]";
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(addX)));
WebElement doAdd = driver.findElement(By.xpath(addX));
doAdd.click();
}
public void doStepEight(){
currentStep = 8;
String error ="//*[@data-test='form-error-alert-box']";
shortWait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(error)));//if too fast then can it fail and get stuck at step 7 ? o_O
WebElement errorE = driver.findElement(By.xpath(error));
if (errorE.isDisplayed()){
System.out.println("Item(s) currently unavailable ");
isUnavail = true;
driver.navigate().refresh();
throw new NullPointerException("");
}
}
public void doStepNine(){
System.out.println("****ITEM AVAILABLE***");
//Only bad thing is now that the next step is delayed 5s looking for the above error
//driver.navigate().refresh();
//If all goes well this should error at 8 then be recalled to start at 9
currentStep = 9;//I might have to write this so this is the first step. And if it fails here a step 9 then go back to 7. B.C I might have to login manually.???
String checkoutB = "//button[@data-test='checkout-button']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(checkoutB)));
WebElement doCh = driver.findElement(By.xpath(checkoutB));
doCh.click();
//If it fails here i should do a shortwait if not her ethen skip to 19
}
public void doStepEighteen(){
currentStep = 18;
String cardXpath = "//input[@id='creditCardInput-cardNumber']";
shortWait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(cardXpath)));
WebElement cardB = driver.findElement(By.xpath(cardXpath));
cardB.sendKeys(new String(ccField.getPassword()));
}
public void doStepNineteen(){
currentStep = 19;
String verfiyX = "//button[@data-test='verify-card-button']";
shortWait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(verfiyX)));
WebElement verifyB = driver.findElement(By.xpath(verfiyX));
verifyB.click();
}
public void doStepTwenty(){
currentStep = 20;
String CCVXp = "//input[@id='creditCardInput-cvv']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(CCVXp)));
WebElement CcvIn = driver.findElement(By.xpath(CCVXp));
String load = new String(ccField.getPassword());
CcvIn.sendKeys(load); //editme
}
public void doStepTwentyOne(){
//One is skipped eh
currentStep = 21;
String verfiyXXX = "//button[@data-test='placeOrderButton']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(verfiyXXX)));
WebElement verifyBBB = driver.findElement(By.xpath(verfiyXXX));
//verifyBBB.click();
}
public void doALap(int SkipStep){
System.out.println("Calling doALap, isDone is " + isDone);
while (!isDone){
try{
//driver.get("https://www.target.com/p/playstation-5-console/-/A-81114595");
//driver.get("https://www.target.com/p/playstation-5-hd-camera/-/A-81114478");
if (SkipStep == 0){driver.get(URL);}
if (SkipStep == 0 || SkipStep <= 2){doStepTwo();}
if (SkipStep == 0 || SkipStep <= 3){doStepThree();}
if (SkipStep == 0 || SkipStep <= 4){doStepFour();}
if (SkipStep == 0 || SkipStep <= 5){doStepFive();}
if (SkipStep == 0 || SkipStep <= 6){doStepSix();}
if (SkipStep <= 7){doStepSeven(); }
if (SkipStep <= 8){doStepEight();}
doStepNine();
if (SkipStep<= 18){doStepEighteen();doStepNineteen();}
doStepTwenty();
doStepTwentyOne();
doSound();
isDone = true;
}
catch(Exception e){
System.out.println("Failed at step: " + currentStep);
doSound();
doLoop();
}
}
}
public void doSound(){
try{
String failedPath;
boolean loop = false;
boolean doAudio = true;
// if (Step < 4){
failedPath = "sound\\speech.wav";
//}
if (currentStep == 8){//Cart Screen - Error unavail
//loop = true;
if (isUnavail){
sleepLength = Integer.parseInt(sLength.getText()); //sigh i dont want to ddos. There's no point in short wait. It can be 30s. The stock is serverside. I got in twice earlier.
}
else if (!isUnavail){
sleepLength = 0; //down below will call doStart at step 9 to click proceed
}
failedPath = "sound\\alarm.wav";
doAudio = false;
}
else if (currentStep >= 9){//9 is cant find error and fails at finding checkout button. may throw false positive. it's ok.
sleepLength = 0;
failedPath = "sound\\alarm.wav";
isDone = true;
doAudio = true;
//driver.navigate().refresh();
}
else if (currentStep == 2){
failedPath = "sound\\speech.wav";
sleepLength = Integer.parseInt(sLength.getText());
}
else{
failedPath = "sound\\alarm.wav";
if (currentStep <= 3 || currentStep >=9){
loop = true;
}
}
if (doAudio && currentStep != 8){
System.out.println("WTFF!!!!!!");
AudioInputStream failed = AudioSystem.getAudioInputStream(new File(failedPath).getAbsoluteFile());
Clip failedClip = AudioSystem.getClip();
failedClip.open(failed);
failedClip.start();
if (loop){
failedClip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
TimeUnit.SECONDS.sleep(sleepLength);
}
catch(IOException | InterruptedException | LineUnavailableException | UnsupportedAudioFileException ee){
System.out.println("exception " + ee);
}
}
public void doLoop (){
// driver.quit();
//System.exit(0);
if ( ( currentStep >= 4 && currentStep < 7 )|| currentStep == 8 || currentStep == 20 || currentStep == 2 || currentStep == 18) {
int tempStep = currentStep;
if (tempStep == 2){
System.out.println("Didn't find preorder button. Reloading page.");
driver.navigate().refresh();
}
else if (tempStep == 3){ // error unavail
tempStep = tempStep + 1;
}
else if (tempStep == 4){
//go back to 3!
tempStep = tempStep - 1;
}
else if (tempStep == 8){
if (isUnavail == true){
tempStep = tempStep - 1;
isUnavail = false;//reset var
}
else{
tempStep = tempStep + 1;
if (currentStep == 20){
tempStep = tempStep + 1;
}
System.out.println("****ITEM AVAILABLE***");
}
}
else if (tempStep == 18){
tempStep = tempStep + 2;
//verify button for CC wont be here
}
System.out.println("Failed at step %s" + currentStep + " retrying the loop starting at" + tempStep);
this.doALap(tempStep);
}
}
public void startIgnition(){
doSleep(1);
JOptionPane.showMessageDialog( null, "You have 15 seconds to login before automation starts.", "Warning", JOptionPane.WARNING_MESSAGE );
System.setProperty("webdriver.chrome.driver", "chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.target.com"); //if it adds or i get it to add..
//driver.get("https://www.target.com/p/playstation-5-console/-/A-81114595");
wait = new WebDriverWait(driver, Integer.parseInt(Wait.getText()) );
shortWait = new WebDriverWait(driver,Integer.parseInt(sWait.getText()));
shorterWait = new WebDriverWait(driver,Integer.parseInt(serWait.getText()));
doSleep(15);
//this.doALap(0);
int startingLap = 0;
if (stepList.getSelectedIndex() == 1){
driver.get("https://www.target.com/co-cart");
startingLap = 7;
}
this.doALap(startingLap); // if it adds or i get it to add..
// engineBtn.setText("Stop");
//engineBtn.setActionCommand("Stop");
// engineBtn.setVisible(false);
}
public void actionPerformed(ActionEvent evt) {
if( evt.getActionCommand().equals( "Start" ) ) {
try {
startIgnition();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog( null, "Invalid Input!", "Error", JOptionPane.ERROR_MESSAGE );
}
}
if( evt.getActionCommand().equals( "Stop" ) ) {
try {
isDone = true;
driver.quit();
engineBtn.setText("Start");
engineBtn.setActionCommand("Start");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog( null, "Invalid Input!", "Error", JOptionPane.ERROR_MESSAGE );
}
}
else {
System.out.println( "Clicked!" );
}
}
}
/*
Step = 8;
String shipping ="(//input[contains(@id,'ulfillment-options')])[1]";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(shipping)));
WebElement doShipping = driver.findElement(By.xpath(shipping));
doShipping.click();
*/
//otherwise 8 start here -.-
/*
//it might have a login screen around here which I think is blocked unless I try more testing on it lol
Step = 10;
String saveandContinue = "//button[@data-test='save-and-continue-button']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(saveandContinue)));
WebElement savecB = driver.findElement(By.xpath(saveandContinue));
savecB.click();
*/
/* First time payment setup only
Step = 11;
String firstNameXpath = "//input[@id='full_name']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(firstNameXpath)));
WebElement fNBt = driver.findElement(By.xpath(firstNameXpath));
fNBt.sendKeys("");
Step = 12;
String addressXpath = "//input[@id='address_line1']";
WebElement adBt = driver.findElement(By.xpath(addressXpath));
adBt.sendKeys("");
Step = 13;
//input[@id='']
String zipcodeXpath = "//*[@id='zip_code']";
WebElement ziBt = driver.findElement(By.xpath(zipcodeXpath));
ziBt.clear();
ziBt.sendKeys("");
Step = 14;
//input[@id='city']
String cityXpath = "//input[@id='city']";
WebElement ciBt = driver.findElement(By.xpath(cityXpath));
ciBt.clear();
ciBt.sendKeys("");
Step = 15;
//*[@id='state']
String stateXpath = "//*[@id='state']";
WebElement stBt = driver.findElement(By.xpath(stateXpath));
//stBt.click();
stBt.sendKeys(""); //JS Set value?
Step = 16;
//input[@id='mobile']
String phoneXpath = "//*[@id='mobile']";
WebElement phBt = driver.findElement(By.xpath(phoneXpath));
phBt.sendKeys("");
Step = 17;
//button[@data-test='saveButton']
String continueXpath = "//button[@data-test='saveButton']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(continueXpath)));
WebElement continueB = driver.findElement(By.xpath(continueXpath));
continueB.click();
*/
/* only for CC
Step = 21;
String verfiyXX = "//button[@data-test='save-and-continue-button']";
wait.until(ExpectedConditions.presenceOfElementLocated (By.xpath(verfiyXX)));
WebElement verifyBB = driver.findElement(By.xpath(verfiyX));
verifyBB.click();
*/