TestBrowser.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package cn.seecoder.courselearning;
  2. import org.junit.After;
  3. import org.junit.Assert;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.openqa.selenium.*;
  7. import java.util.concurrent.TimeUnit;
  8. public class TestBrowser {
  9. public static String deployUrl;
  10. public static WebDriver driver;
  11. //实例化TestBase的对象,以方便调用其中的方法
  12. static TestBase base = new TestBase();
  13. @Before
  14. public void init(){
  15. base.startBrowser();
  16. driver = TestBase.getDriver();
  17. driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  18. deployUrl = System.getProperty("url");
  19. driver.get(deployUrl);
  20. driver.manage().window().setPosition(new Point(100, 50));
  21. driver.manage().deleteAllCookies();
  22. driver.manage().window().maximize();
  23. }
  24. /**
  25. * 充值123,充值成功
  26. */
  27. @Test
  28. public void test1() throws Exception {
  29. try {
  30. driver.findElement(By.xpath("/html/body/div/div/div/div/header/div/button[2]")).click();
  31. driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[1]/div/div[1]/div/input")).sendKeys("10212345678");
  32. driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[2]/div/div[1]/div/input")).sendKeys("123456");
  33. driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/button")).click();
  34. Thread.sleep(1000);
  35. driver.findElement(By.xpath("/html/body/div/div[1]/div/div/header/div/button[2]")).click();
  36. driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]")).click();
  37. // 记录充值前的余额
  38. 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"));
  39. int rechargeNum = 123;
  40. driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/button")).click();
  41. driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(Keys.BACK_SPACE);
  42. driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(String.valueOf(rechargeNum));
  43. driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[3]/button[1]")).click();
  44. WebElement element = driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[1]/div/div"));
  45. Assert.assertTrue(element.isDisplayed()); // 显示提示:“充值成功”
  46. // 充值后的余额
  47. 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"));
  48. int diff = afterRecharge - beforeRecharge;
  49. Assert.assertEquals(rechargeNum, diff); // 充值前后差额==充值金额
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. System.out.println("充值异常");
  53. }
  54. }
  55. /**
  56. * 充值-123,充值失败
  57. */
  58. @Test
  59. public void test2() throws Exception {
  60. try {
  61. driver.findElement(By.xpath("/html/body/div/div/div/div/header/div/button[2]")).click();
  62. driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[1]/div/div[1]/div/input")).sendKeys("10212345678");
  63. driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/div[2]/div/div[1]/div/input")).sendKeys("123456");
  64. driver.findElement(By.xpath("/html/body/div/div/div/div/div/div/form/button")).click();
  65. Thread.sleep(1000);
  66. driver.findElement(By.xpath("/html/body/div/div[1]/div/div/header/div/button[2]")).click();
  67. driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]")).click();
  68. 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"));;
  69. int rechargeNum = -123;
  70. driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[3]/form/button")).click();
  71. driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(Keys.BACK_SPACE);
  72. driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div/div[1]/div/input")).sendKeys(String.valueOf(rechargeNum));
  73. driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[3]/button[1]")).click();
  74. WebElement element = driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div[1]/div/div"));
  75. Assert.assertFalse(element.isDisplayed()); // 不可以充值负数,因此不应当显示“充值成功”
  76. 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"));;
  77. int diff = afterRecharge - beforeRecharge;
  78. Assert.assertEquals(0, diff); // 充值前后结果应当没变
  79. } catch (Exception e) {
  80. System.out.println("充值异常");
  81. }
  82. }
  83. @After
  84. public void closeBrowser() {
  85. //退出浏览器
  86. driver.quit();
  87. }
  88. }