ChengYuXuan 5 år sedan
förälder
incheckning
65d42863c2

+ 11 - 0
pom.xml

@@ -73,6 +73,17 @@
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.seleniumhq.selenium</groupId>
+            <artifactId>selenium-java</artifactId>
+            <scope>test</scope>
+            <version>3.14.0</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

+ 30 - 0
src/test/README.md

@@ -0,0 +1,30 @@
+# 测试说明文档
+
+## 前端测试
+
+使用 Selenium 测试框架,需要配置对应浏览器的 driver
+
+示例文件为 `TestBase.java` 和 `TestBrowser.java`
+
+### 配置 driver
+
+根据自己电脑上的浏览器配置对应 driver,示例如下 `TestBase.java`
+
+假如本机已经安装 Firefox 浏览器,需要安装对应的 driver 名为 geckodriver
+
+从网上下载对应系统的 driver 后,根据存放路径配置
+
+其他浏览器以及对应的 driver 可从 Selenium 官方文档查找
+
+```java
+public class TestBase {
+    public void startBrowser() {
+        System.setProperty("webdriver.gecko.driver", "C://geckodriver.exe"); // 配置 driver 的路径
+        driver = new FirefoxDriver();
+    }
+}
+```
+
+### 编写测试代码
+
+参见 `TestBrowser.java`

+ 21 - 0
src/test/java/cn/seecoder/courselearning/TestBase.java

@@ -0,0 +1,21 @@
+package cn.seecoder.courselearning;
+
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.firefox.FirefoxDriver;
+
+public class TestBase {
+    public static WebDriver driver;
+
+    public void startBrowser() {
+//        System.setProperty("webdriver.firefox.bin", "/usr/bin/firefox");
+        System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver.exe");
+        System.setProperty("url", "http://localhost:8080");
+        driver = new FirefoxDriver();
+    }
+
+    public static WebDriver getDriver() {
+        return driver;
+    }
+
+}
+

+ 106 - 0
src/test/java/cn/seecoder/courselearning/TestBrowser.java

@@ -0,0 +1,106 @@
+package cn.seecoder.courselearning;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.openqa.selenium.*;
+
+import java.util.concurrent.TimeUnit;
+
+public class TestBrowser {
+
+    public static String deployUrl;
+
+    public static WebDriver driver;
+
+    //实例化TestBase的对象,以方便调用其中的方法
+    static TestBase base = new TestBase();
+
+    @Before
+    public void init(){
+        base.startBrowser();
+        driver = TestBase.getDriver();
+
+        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
+        deployUrl = System.getProperty("url");
+        driver.get(deployUrl);
+        driver.manage().window().setPosition(new Point(100, 50));
+        driver.manage().deleteAllCookies();
+        driver.manage().window().maximize();
+    }
+
+    /**
+     * 充值123,充值成功
+     */
+    @Test
+    public void test1() throws Exception {
+        try {
+            driver.findElement(By.xpath("/html/body/div/div/div/div/header/div/button[2]")).click();
+            driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[1]/div/div[1]/div/input")).sendKeys("10212345678");
+            driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[2]/div/div[1]/div/input")).sendKeys("123456");
+            driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/button")).click();
+            Thread.sleep(1000);
+            driver.findElement(By.xpath("/html/body/div/div[1]/div/div/header/div/button[2]")).click();
+            driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]")).click();
+            // 记录充值前的余额
+            int beforeRecharge = Integer.parseInt(driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/div[6]/div/div[1]/div/input")).getAttribute("value"));
+            int rechargeNum = 123;
+
+            driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/button")).click();
+            driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(Keys.BACK_SPACE);
+            driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(String.valueOf(rechargeNum));
+            driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[3]/button[1]")).click();
+
+            WebElement element = driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[1]/div/div"));
+            Assert.assertTrue(element.isDisplayed()); // 显示提示:“充值成功”
+            // 充值后的余额
+            int afterRecharge = Integer.parseInt(driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/div[6]/div/div[1]/div/input")).getAttribute("value"));
+            int diff = afterRecharge - beforeRecharge;
+            Assert.assertEquals(rechargeNum, diff); // 充值前后差额==充值金额
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.out.println("充值异常");
+        }
+    }
+
+    /**
+     * 充值-123,充值失败
+     */
+    @Test
+    public void test2() throws Exception {
+        try {
+            driver.findElement(By.xpath("/html/body/div/div/div/div/header/div/button[2]")).click();
+            driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[1]/div/div[1]/div/input")).sendKeys("10212345678");
+            driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[2]/div/div[1]/div/input")).sendKeys("123456");
+            driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/button")).click();
+            Thread.sleep(1000);
+            driver.findElement(By.xpath("/html/body/div/div[1]/div/div/header/div/button[2]")).click();
+            driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]")).click();
+
+            int beforeRecharge = Integer.parseInt(driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/div[6]/div/div[1]/div/input")).getAttribute("value"));;
+            int rechargeNum = -123;
+
+            driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/button")).click();
+            driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(Keys.BACK_SPACE);
+            driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(String.valueOf(rechargeNum));
+            driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[3]/button[1]")).click();
+
+            WebElement element = driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[1]/div/div"));
+            Assert.assertFalse(element.isDisplayed()); // 不可以充值负数,因此不应当显示“充值成功”
+
+            int afterRecharge = Integer.parseInt(driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/div[6]/div/div[1]/div/input")).getAttribute("value"));;
+            int diff = afterRecharge - beforeRecharge;
+            Assert.assertEquals(0, diff); // 充值前后结果应当没变
+        } catch (Exception e) {
+            System.out.println("充值异常");
+        }
+    }
+
+
+    @After
+    public void closeBrowser() {
+        //退出浏览器
+        driver.quit();
+    }
+}