1
1
package stepDefinitions ;
2
2
3
3
import io .cucumber .java .After ;
4
- import io .cucumber .java .AfterStep ;
5
4
import io .cucumber .java .Before ;
6
5
import io .cucumber .java .Scenario ;
7
6
import manager .Driver ;
8
7
import manager .DriverManager ;
9
8
import org .openqa .selenium .remote .RemoteWebDriver ;
10
- import java .io .IOException ;
11
- import java .io .InputStream ;
12
- import java .net .HttpURLConnection ;
13
- import java .net .URL ;
14
- import java .nio .file .Files ;
15
- import java .nio .file .Path ;
16
- import java .nio .file .Paths ;
17
- import java .nio .file .StandardCopyOption ;
18
- import org .json .JSONObject ;
19
- import java .io .OutputStream ;
20
- import java .util .Base64 ;
21
9
22
10
public class Hook {
23
11
public RemoteWebDriver driver = DriverManager .getDriver ();
@@ -27,66 +15,12 @@ public void updateName(Scenario scenario) throws InterruptedException {
27
15
driver .executeScript ("lambda-name=" + scenario .getName ());
28
16
}
29
17
30
- @ AfterStep
31
- public void captureDom () throws InterruptedException {
32
- driver .executeScript ("return document.documentElement.outerHTML;" );
33
- }
34
-
35
18
@ After
36
19
public void close_the_browser (Scenario scenario ) throws InterruptedException {
37
20
driver .executeScript ("lambda-status=" + (scenario .isFailed () ? "failed" : "passed" ));
38
-
39
21
String testId = driver .getSessionId ().toString ();
40
22
System .out .println (testId );
41
- Thread .sleep (20000 );
42
- downloadFiles (testId ,System .getenv ("LT_USERNAME" ),System .getenv ("LT_ACCESS_KEY" ));
43
23
Driver .quitDriver ();
44
24
}
45
25
46
- public static void downloadFiles (String testId , String username , String accessKey ) {
47
- String directoryPath = "./" + testId ;
48
- String apiUrl = "https://api.lambdatest.com/automation/api/v1/sessions/" + testId + "/results" ;
49
-
50
- // Encode username and access key
51
- String authValue = Base64 .getEncoder ().encodeToString ((username + ":" + accessKey ).getBytes ());
52
-
53
- try {
54
- // Create directory for testId if it doesn't already exist
55
- Path directory = Paths .get (directoryPath );
56
- if (!Files .exists (directory )) {
57
- Files .createDirectories (directory );
58
- }
59
-
60
- // Download ZIP file containing results
61
- downloadFile (apiUrl , authValue , Paths .get (directoryPath , testId + "_results.zip" ));
62
-
63
- System .out .println ("ZIP file downloaded successfully." );
64
- } catch (IOException e ) {
65
- System .err .println ("Error downloading the ZIP file: " + e .getMessage ());
66
- }
67
- }
68
-
69
- private static void downloadFile (String fileUrl , String authValue , Path targetPath ) throws IOException {
70
- URL url = new URL (fileUrl );
71
- HttpURLConnection httpConn = (HttpURLConnection ) url .openConnection ();
72
- httpConn .setRequestMethod ("GET" );
73
- httpConn .setRequestProperty ("Authorization" , "Basic " + authValue );
74
-
75
- // Check response code and handle input stream accordingly
76
- if (httpConn .getResponseCode () == HttpURLConnection .HTTP_OK ) {
77
- try (InputStream in = httpConn .getInputStream ();
78
- OutputStream out = Files .newOutputStream (targetPath )) {
79
- byte [] buffer = new byte [4096 ];
80
- int bytesRead ;
81
- while ((bytesRead = in .read (buffer )) != -1 ) {
82
- out .write (buffer , 0 , bytesRead );
83
- }
84
- }
85
- } else {
86
- throw new IOException ("Server returned non-OK status: " + httpConn .getResponseCode ());
87
- }
88
-
89
- httpConn .disconnect ();
90
- }
91
-
92
26
}
0 commit comments