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(); } }