5
5
import TeamControlium .Utilities .TestData ;
6
6
import org .apache .commons .io .FilenameUtils ;
7
7
import org .apache .commons .lang3 .time .StopWatch ;
8
+ import org .junit .jupiter .api .Assertions ;
8
9
import org .openqa .selenium .By ;
9
10
import org .openqa .selenium .JavascriptExecutor ;
10
11
import org .openqa .selenium .WebDriver ;
21
22
import org .openqa .selenium .ie .InternetExplorerOptions ;
22
23
23
24
import javax .swing .text .Utilities ;
25
+ import java .io .BufferedReader ;
24
26
import java .io .File ;
27
+ import java .io .InputStreamReader ;
25
28
import java .nio .file .Files ;
26
29
import java .nio .file .Path ;
27
30
import java .nio .file .Paths ;
@@ -63,33 +66,31 @@ public class SeleniumDriver {
63
66
private String seleniumLogFilename =null ;
64
67
65
68
public SeleniumDriver (String seleniumHost ,String device ,String browser ) {
66
- commonConstructs ();
67
- Browsers .SetTestBrowser (browser );
68
- Devices .SetTestDevice (device );
69
- setSeleniumHost (seleniumHost );
69
+ commonConstructs (false ,seleniumHost ,device ,browser );
70
70
}
71
71
public SeleniumDriver (String seleniumHost ) {
72
- commonConstructs ();
73
- Browsers .SetTestBrowser ();
74
- Devices .SetTestDevice ();
75
- setSeleniumHost (null );
72
+ commonConstructs (false ,seleniumHost ,null ,null );
76
73
}
77
74
public SeleniumDriver (String device ,String browser ) {
78
- commonConstructs ();
79
- Browsers .SetTestBrowser (browser );
80
- Devices .SetTestDevice (device );
81
- setSeleniumHost (seleniumHost );
82
- startOrConnectToSeleniumServer ();
75
+ commonConstructs (false ,null ,device ,browser );
83
76
}
84
77
public SeleniumDriver () {
85
- commonConstructs ();
86
- Browsers .SetTestBrowser ();
87
- Devices .SetTestDevice ();
88
- setSeleniumHost (null );
89
- startOrConnectToSeleniumServer ();
78
+ commonConstructs (false ,null ,null ,null );
79
+ }
80
+ public SeleniumDriver (boolean killDriverInstancesFirst ,String seleniumHost ,String device ,String browser ) {
81
+ commonConstructs (killDriverInstancesFirst ,seleniumHost ,device ,browser );
82
+ }
83
+ public SeleniumDriver (boolean killDriverInstancesFirst ,String seleniumHost ) {
84
+ commonConstructs (killDriverInstancesFirst ,seleniumHost ,null ,null );
85
+ }
86
+ public SeleniumDriver (boolean killDriverInstancesFirst ,String device ,String browser ) {
87
+ commonConstructs (killDriverInstancesFirst ,null ,device ,browser );
88
+ }
89
+ public SeleniumDriver (boolean killDriverInstancesFirst ) {
90
+ commonConstructs (killDriverInstancesFirst ,null ,null ,null );
90
91
}
91
92
92
- private void commonConstructs () {
93
+ private void commonConstructs (boolean killFirst , String seleniumHost , String device , String browser ) {
93
94
// Initialise defaults
94
95
setFindTimeout (Duration .ofMillis (defaultTimeout ));
95
96
setPollInterval (Duration .ofMillis (defaultPollInterval ));
@@ -122,6 +123,13 @@ private void commonConstructs() {
122
123
seleniumLogFilename =null ;
123
124
}
124
125
Logger .WriteLine (Logger .LogLevels .TestInformation , "Selenium Log filename: [%s]" ,seleniumLogFilename ==null ?"stdio (console)" :seleniumLogFilename );
126
+
127
+ Browsers .SetTestBrowser (browser );
128
+ Devices .SetTestDevice (device );
129
+
130
+ setSeleniumHost (seleniumHost );
131
+
132
+ startOrConnectToSeleniumServer (killFirst );
125
133
}
126
134
127
135
@@ -367,9 +375,9 @@ public void executeJavaScriptNoReturnData(String script, Object[] args)
367
375
}
368
376
369
377
370
- private void startOrConnectToSeleniumServer () {
378
+ private void startOrConnectToSeleniumServer (boolean killFirst ) {
371
379
if (isLocalSelenium ) {
372
- setupLocalRun ();
380
+ setupLocalRun (killFirst );
373
381
} else {
374
382
throw new RuntimeException ("Remote server execution mot yet implemented!" );
375
383
}
@@ -395,7 +403,7 @@ private void setSeleniumHost(String host) {
395
403
isLocalSelenium = (seleniumHost .equalsIgnoreCase ("localhost" ) || seleniumHost .equals ("127.0.0.1" ));
396
404
}
397
405
398
- private void setupLocalRun () {
406
+ private void setupLocalRun (boolean killFirst ) {
399
407
Logger .WriteLine (Logger .LogLevels .FrameworkDebug , "Running Selenium locally" );
400
408
401
409
try {
@@ -441,6 +449,7 @@ else if (Browsers.isChrome()) {
441
449
Logger .WriteLine (Logger .LogLevels .FrameworkInformation , "Writing Selenium Server Output to console" );
442
450
}
443
451
452
+ if (killFirst ) killAllProcesses (executable );
444
453
webDriver = new ChromeDriver (ChromeDriverService .createDefaultService (), options );
445
454
}
446
455
else if (Browsers .isEdge ()) {
@@ -573,6 +582,62 @@ private String durationFormatted(long millis) {
573
582
return String .format ("%02d:%02d:%02d.%03d" ,hours ,minutes ,seconds ,milliseconds );
574
583
}
575
584
585
+ private void killAllProcesses (String name ) {
586
+ int matchingProcessCount = getProcessCount (name );
587
+ int newProcessCount =0 ;
588
+ String line ;
589
+ while (matchingProcessCount >0 ) {
590
+ try {
591
+ if (matchingProcessCount >= 0 ) {
592
+ Process p = Runtime .getRuntime ().exec (System .getenv ("windir" ) + String .format ("\\ system32\\ " + "taskkill.exe /F /IM %s" , name ));
593
+ BufferedReader input = new BufferedReader (new InputStreamReader (p .getInputStream ()));
594
+ while ((line = input .readLine ()) != null ) {
595
+ int g = 99 ;
596
+ }
597
+ input .close ();
598
+ }
599
+ } catch (Exception err ) {
600
+ Assertions .fail (String .format ("Error killing [%s] processes " , name ), err );
601
+ }
602
+ newProcessCount = getProcessCount (name );
603
+ if (newProcessCount == matchingProcessCount ) {
604
+ Assertions .fail (String .format ("Error killing [%s] processes. Did not kill processes! " , name ));
605
+ }
606
+ matchingProcessCount =newProcessCount ;
607
+ }
608
+ }
609
+
610
+
611
+ private int getProcessCount (String name ) {
612
+ int count =0 ;
613
+ List <String []> processInstanceCount = getProcessList ();
614
+
615
+ for (String [] line : getProcessList ()) {
616
+ if (line [0 ].equalsIgnoreCase (name )) count ++;
617
+ }
618
+ return count ;
619
+ }
620
+
621
+ private List <String []> getProcessList () {
622
+ boolean startLogging = false ;
623
+ // MAT GET THIS WORKING - Driver executable filesnames.........
624
+
625
+ List <String []> list = new ArrayList <String []>();
626
+ try {
627
+ String line ;
628
+ //Process p = Runtime.getRuntime().exec("ps -e");
629
+ Process p = Runtime .getRuntime ().exec (System .getenv ("windir" ) +"\\ system32\\ " +"tasklist.exe" );
630
+ BufferedReader input = new BufferedReader (new InputStreamReader (p .getInputStream ()));
631
+ while ((line = input .readLine ()) != null ) {
632
+ String [] arrayLine = line .split ("\\ s+" );
633
+ if (startLogging ) list .add (arrayLine );
634
+ if (line .contains ("=======" )) startLogging =true ;
635
+ }
636
+ input .close ();
637
+ } catch (Exception err ) {Assertions .fail ("Error getting process list: " ,err );
638
+ }
639
+ return list ;
640
+ }
576
641
577
642
}
578
643
0 commit comments