Skip to content

Commit

Permalink
add selenium test generation support (#7)
Browse files Browse the repository at this point in the history
* wip selenium test generator

* MVP working selenium test generate

* add interface for translator

* move templates to new dir

* add more translation for selenium

* fix selenium bugs

Template unknown translator: selenium
Testsuite generator unknown translator selenium
  • Loading branch information
wuttinanhi authored Nov 23, 2024
1 parent 327aaa8 commit 8e3a30e
Show file tree
Hide file tree
Showing 32 changed files with 728 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ screenshot.png
app.test.ts
gen.test.ts
*.test.ts
examples/java_selenium/src/test/java/BasicTest.java
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"editor.inlineSuggest.suppressSuggestions": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
},
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 2 additions & 0 deletions examples/java_selenium/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
26 changes: 26 additions & 0 deletions examples/java_selenium/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

target
21 changes: 21 additions & 0 deletions examples/java_selenium/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Main",
"request": "launch",
"mainClass": "com.example.Main",
"projectName": "demo"
}
]
}
10 changes: 10 additions & 0 deletions examples/java_selenium/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"[xml]": {
"editor.defaultFormatter": "redhat.vscode-xml",
"editor.formatOnSave": true,
"editor.autoClosingBrackets": "never",
"files.trimFinalNewlines": true
},
"java.compile.nullAnalysis.mode": "automatic"
}
21 changes: 21 additions & 0 deletions examples/java_selenium/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 wuttinanhi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions examples/java_selenium/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# aitestgen java test

## Run test
```bash
mvn test
```
32 changes: 32 additions & 0 deletions examples/java_selenium/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<selenium.version>4.26.0</selenium.version>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
7 changes: 7 additions & 0 deletions examples/java_selenium/src/main/java/com/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
48 changes: 48 additions & 0 deletions examples/java_selenium/src/test/java/BasicTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BasicTest {

WebDriver driver;

@BeforeEach
public void setup() {
driver = new ChromeDriver();
}

@AfterEach
public void teardown() {
driver.quit();
}

@Test
public void SimpleTest() {
driver.get("https://www.selenium.dev/selenium/web/web-form.html");

WebElement textInput = driver.findElement(By.cssSelector("#my-text-id"));

WebElement submitButton = driver.findElement(By.cssSelector(".btn"));

textInput.sendKeys("hello");

submitButton.click();

new WebDriverWait(driver, Duration.ofSeconds(10)).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

WebElement successMessage = driver.findElement(By.cssSelector("#message"));
String textsuccessMessage = successMessage.getText();
assertEquals("Received!", textsuccessMessage);

driver.quit();
}
}
20 changes: 20 additions & 0 deletions examples/testprompts/selenium.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<testsuite>
<name>Selenium Test</name>
<output>examples/java_selenium/src/test/java/BasicTest.java</output>
<language>java</language>
<translator>selenium</translator>
<provider>openai</provider>
<model>gpt-4o-mini</model>
<java_classname>BasicTest</java_classname>
<testcases>
<testcase>
<name>SimpleTest</name>
<prompt>
1. go to https://www.selenium.dev/selenium/web/web-form.html
2. set text input to "hello"
3. click submit
4. expected received message
</prompt>
</testcase>
</testcases>
</testsuite>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"openai": "4.68.4",
"ora": "^8.1.1",
"prettier": "^3.3.3",
"prettier-plugin-java": "^2.6.5",
"puppeteer": "^23.5.0",
"puppeteer-element2selector": "^0.0.3",
"ts-node": "^10.9.2",
Expand Down
92 changes: 92 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8e3a30e

Please sign in to comment.