Skip to content

Commit

Permalink
Merge pull request #53 from blibli-badak/dev-argo
Browse files Browse the repository at this point in the history
Support For Charset in HAR Writting
  • Loading branch information
argotri authored Jun 21, 2024
2 parents 56cc607 + 3c7623b commit 765505f
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
run: chrome --version

- name: Build with Maven
run: mvn -B package --file pom.xml
run: mvn -B package --file pom.xml -Dfile.encoding=UTF-8

- name: Publish to GitHub Packages Apache Maven
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml -Dfile.encoding=UTF-8
env:
GITHUB_TOKEN: ${{ github.token }}
USERNAME: ${{ github.actor }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
- name: test chrome
run: chrome --version
- name: Build with Maven
run: mvn -B clean verify --file pom.xml -s $GITHUB_WORKSPACE/settings.xml
run: mvn -B clean verify --file pom.xml -s $GITHUB_WORKSPACE/settings.xml -Dfile.encoding=UTF-8
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ Or if you already have existing CDP session you can use this one (Starting from
```java
NetworkListener networkListener = new NetworkListener(driver, "Your Existing CDP Sessions","har.har" );
```
[Optional] Setup the Default Charset (since 1.3.0)

This is used for unicode support like chinese or japanese character, reffer to this for the value https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
```java
networkListener.setCharset("UTF-8");
```

Start Capture your network request
```java
networkListener.start();
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.blibli.oss.qa.util</groupId>
<artifactId>har-util</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>

<name>har-util</name>
<url>http://www.blibli.com</url>
Expand All @@ -15,7 +15,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<selenium.version>4.17.0</selenium.version>
<selenium.version>4.21.0</selenium.version>
</properties>

<distributionManagement>
Expand All @@ -41,7 +41,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/de.sstoehr/har-reader -->
Expand All @@ -64,7 +64,7 @@
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-devtools-v121</artifactId>
<artifactId>selenium-devtools-v125</artifactId>
<version>${selenium.version}</version>
</dependency>
</dependencies>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/blibli/oss/qa/util/ResponseModel.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.blibli.oss.qa.util;

import org.openqa.selenium.devtools.v121.network.Network;
import org.openqa.selenium.devtools.v121.network.model.ResponseReceived;

import org.openqa.selenium.devtools.v125.network.model.ResponseReceived;
import org.openqa.selenium.devtools.v125.network.Network;

public class ResponseModel {
ResponseReceived responseReceived;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/blibli/oss/qa/util/model/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.blibli.oss.qa.util.model;

public class Constant {
public static String DEFAULT_UNICODE="UTF-8";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import lombok.Getter;
import lombok.Setter;
import org.openqa.selenium.devtools.v121.network.model.Request;
import org.openqa.selenium.devtools.v121.network.model.Response;
import org.openqa.selenium.devtools.v125.network.model.Request;
import org.openqa.selenium.devtools.v125.network.model.Response;


import java.util.Date;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.blibli.oss.qa.util.model;

import lombok.Getter;
import org.openqa.selenium.devtools.v121.network.model.Request;
import org.openqa.selenium.devtools.v121.network.model.Response;
import org.openqa.selenium.devtools.v125.network.model.Request;
import org.openqa.selenium.devtools.v125.network.model.Response;

import java.util.ArrayList;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import de.sstoehr.harreader.model.HarResponse;
import de.sstoehr.harreader.model.HarTiming;
import de.sstoehr.harreader.model.HttpMethod;
import org.openqa.selenium.devtools.v121.network.model.Request;
import org.openqa.selenium.devtools.v121.network.model.ResourceTiming;
import org.openqa.selenium.devtools.v121.network.model.Response;
import org.openqa.selenium.devtools.v125.network.model.Request;
import org.openqa.selenium.devtools.v125.network.model.ResourceTiming;
import org.openqa.selenium.devtools.v125.network.model.Response;

import java.io.IOException;
import java.io.InputStream;
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/com/blibli/oss/qa/util/services/NetworkListener.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.blibli.oss.qa.util.services;

import com.blibli.oss.qa.util.model.Constant;
import com.blibli.oss.qa.util.model.HarModel;
import com.blibli.oss.qa.util.model.RequestResponseStorage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import de.sstoehr.harreader.model.Har;
import de.sstoehr.harreader.model.HarCreatorBrowser;
import de.sstoehr.harreader.model.HarEntry;
Expand All @@ -16,15 +16,11 @@
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.NetworkInterceptor;
import org.openqa.selenium.devtools.v121.network.Network;
import org.openqa.selenium.devtools.v121.network.model.Request;
import org.openqa.selenium.devtools.v121.network.model.ResourceTiming;
import org.openqa.selenium.devtools.v121.network.model.Response;
import org.openqa.selenium.devtools.v125.network.Network;
import org.openqa.selenium.devtools.v125.network.model.Request;
import org.openqa.selenium.devtools.v125.network.model.Response;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -48,6 +44,7 @@ public class NetworkListener {
private String baseRemoteUrl;
private DevTools devTools;
private String harFile = "";
private String charset = Constant.DEFAULT_UNICODE;

private HarCreatorBrowser harCreatorBrowser;

Expand Down Expand Up @@ -250,7 +247,6 @@ public void createHarFile() {
});
log.info("har entry size : %d", harEntries.size());
harLog.setPages(harPages);

harLog.setEntries(harEntries);
har.setLog(harLog);
createFile(har);
Expand Down Expand Up @@ -289,17 +285,25 @@ public void createHarFile(String filter) {
createFile(har);
}

public void createFile(Har har) {
private void createFile(Har har) {
ObjectMapper om = new ObjectMapper();
try {
String json = om.writeValueAsString(har);
String json = new String(om.writeValueAsString(har).getBytes(), charset);
// write json to file
Files.write(java.nio.file.Paths.get(harFile), json.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Setup Charset on the file generation
* @param charset default will be UTF-8 , you can get from here https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html
*/
public void setCharset(String charset){
this.charset = charset;
}

private void createHarBrowser() {
harCreatorBrowser = new HarCreatorBrowser();
harCreatorBrowser.setName("gdn-qa-automation");
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/blibli/oss/qa/util/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.blibli.oss.qa.util;

import com.blibli.oss.qa.util.services.NetworkListener;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;

public class BaseTest {



public String readHarData(String fileName) throws IOException {
String harFile = Paths.get(".").toAbsolutePath().normalize().toString() + ""+File.separator +""+fileName;
System.out.println("Read Har Data " + harFile);
return new String(Files.readAllBytes(Paths.get(fileName)));
}

}

72 changes: 72 additions & 0 deletions src/test/java/com/blibli/oss/qa/util/UnicodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.blibli.oss.qa.util;

import com.blibli.oss.qa.util.services.NetworkListener;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.IOException;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class UnicodeTest extends BaseTest {
private NetworkListener networkListener;
private static String HAR_UNICODE_NAME = "har-unicode.har";
private ChromeDriver driver;
private ChromeOptions options;
private DesiredCapabilities capabilities;

@BeforeEach
public void setup() {
this.setupLocalDriver();
}
public void setupLocalDriver(){
options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--remote-allow-origins=*");
options.addArguments("--disable-dev-shm-usage");
if(Optional.ofNullable(System.getenv("CHROME_MODE")).orElse("").equalsIgnoreCase("headless")){
options.addArguments("--headless");
System.out.println("Running With headless mode");
}else{
System.out.println("Running Without headless mode");
}
WebDriverManager.chromedriver().setup();
}


@Test
public void testUnicode() throws InterruptedException, IOException {
driver = new ChromeDriver(options);
driver.manage().window().maximize();
networkListener = new NetworkListener(driver, driver.getDevTools(), HAR_UNICODE_NAME);
networkListener.setCharset("UTF-8");
networkListener.start();
driver.get("https://gosoft.web.id/selenium/unicode.html");
Thread.sleep(5000); // make sure the web are loaded
driver.quit();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// in the github actions we need add some wait , because chrome exited too slow ,
// so when we create new session previous chrome is not closed completly
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
networkListener.createHarFile();
String harFile = this.readHarData(HAR_UNICODE_NAME);
System.out.println(harFile);
assertTrue(harFile.contains("接口路径不存在 请前往"));
}


}
32 changes: 15 additions & 17 deletions src/test/java/com/blibli/oss/qa/util/UsingCdpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@
import java.time.Duration;
import java.util.Optional;

public class UsingCdpTest {
public class UsingCdpTest extends BaseTest{

private ChromeDriver driver;
private NetworkListener networkListener;
private ChromeDriver driver;
private ChromeOptions options;
private DesiredCapabilities capabilities;
@BeforeEach
public void setup() {

public void setupLocalDriver(){
options = new ChromeOptions();
options.addArguments("--no-sandbox");
options.addArguments("--remote-allow-origins=*");
options.addArguments("--disable-dev-shm-usage");
if(Optional.ofNullable(System.getenv("CHROME_MODE")).orElse("").equalsIgnoreCase("headless")){
options.addArguments("--headless");
System.out.println("Running With headless mode");
}else{
System.out.println("Running Without headless mode");
}
WebDriverManager.chromedriver().setup();
}


@Test
public void testWithLocalDriver() {
setupLocalDriver();
Expand All @@ -41,19 +52,6 @@ public void testWithLocalDriver() {
element.sendKeys("Kiwi/n");
}

private void setupLocalDriver(){
options.addArguments("--no-sandbox");
options.addArguments("--remote-allow-origins=*");
options.addArguments("--disable-dev-shm-usage");
if(Optional.ofNullable(System.getenv("CHROME_MODE")).orElse("").equalsIgnoreCase("headless")){
options.addArguments("--headless");
System.out.println("Running With headless mode");
}else{
System.out.println("Running Without headless mode");
}
WebDriverManager.chromedriver().setup();
}

@AfterEach
public void tearDown() {
driver.quit();
Expand Down

0 comments on commit 765505f

Please sign in to comment.