spring-in-action-5-samples-master源代码.zip

  • L8_112627
    了解作者
  • 14.7MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-13 11:32
    上传日期
spring-in-action-5-samples-master源代码
spring-in-action-5-samples-master随书源代码.zip
内容介绍
package tacos; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @Ignore("Ignore for now...deal with security implications in tests.") public class DesignAndOrderTacosBrowserTest { private static HtmlUnitDriver browser; @LocalServerPort private int port; @Autowired TestRestTemplate rest; @BeforeClass public static void setup() { browser = new HtmlUnitDriver(); browser.manage().timeouts() .implicitlyWait(10, TimeUnit.SECONDS); } @AfterClass public static void closeBrowser() { browser.quit(); } @Test public void testDesignATacoPage_HappyPath() throws Exception { browser.get(homePageUrl()); clickDesignATaco(); assertLandedOnLoginPage(); doRegistration("testuser", "testpassword"); assertLandedOnLoginPage(); doLogin("testuser", "testpassword"); assertDesignPageElements(); buildAndSubmitATaco("Basic Taco", "FLTO", "GRBF", "CHED", "TMTO", "SLSA"); clickBuildAnotherTaco(); buildAndSubmitATaco("Another Taco", "COTO", "CARN", "JACK", "LETC", "SRCR"); fillInAndSubmitOrderForm(); assertEquals(homePageUrl(), browser.getCurrentUrl()); doLogout(); } @Test public void testDesignATacoPage_EmptyOrderInfo() throws Exception { browser.get(homePageUrl()); clickDesignATaco(); assertLandedOnLoginPage(); doRegistration("testuser2", "testpassword"); doLogin("testuser2", "testpassword"); assertDesignPageElements(); buildAndSubmitATaco("Basic Taco", "FLTO", "GRBF", "CHED", "TMTO", "SLSA"); submitEmptyOrderForm(); fillInAndSubmitOrderForm(); assertEquals(homePageUrl(), browser.getCurrentUrl()); doLogout(); } @Test public void testDesignATacoPage_InvalidOrderInfo() throws Exception { browser.get(homePageUrl()); clickDesignATaco(); assertLandedOnLoginPage(); doRegistration("testuser3", "testpassword"); doLogin("testuser3", "testpassword"); assertDesignPageElements(); buildAndSubmitATaco("Basic Taco", "FLTO", "GRBF", "CHED", "TMTO", "SLSA"); submitInvalidOrderForm(); fillInAndSubmitOrderForm(); assertEquals(homePageUrl(), browser.getCurrentUrl()); doLogout(); } // // Browser test action methods // private void buildAndSubmitATaco(String name, String... ingredients) { assertDesignPageElements(); for (String ingredient : ingredients) { browser.findElementByCssSelector("input[value='" + ingredient + "']").click(); } browser.findElementByCssSelector("input#name").sendKeys(name); browser.findElementByCssSelector("form#tacoForm").submit(); } private void assertLandedOnLoginPage() { assertEquals(loginPageUrl(), browser.getCurrentUrl()); } private void doRegistration(String username, String password) { browser.findElementByLinkText("here").click(); assertEquals(registrationPageUrl(), browser.getCurrentUrl()); browser.findElementByName("username").sendKeys(username); browser.findElementByName("password").sendKeys(password); browser.findElementByName("confirm").sendKeys(password); browser.findElementByName("fullname").sendKeys("Test McTest"); browser.findElementByName("street").sendKeys("1234 Test Street"); browser.findElementByName("city").sendKeys("Testville"); browser.findElementByName("state").sendKeys("TX"); browser.findElementByName("zip").sendKeys("12345"); browser.findElementByName("phone").sendKeys("123-123-1234"); browser.findElementByCssSelector("form#registerForm").submit(); } private void doLogin(String username, String password) { browser.findElementByName("username").sendKeys(username); browser.findElementByName("password").sendKeys(password); browser.findElementByCssSelector("form#loginForm").submit(); } private void doLogout() { WebElement logoutForm = browser.findElementByCssSelector("form#logoutForm"); if (logoutForm != null) { logoutForm.submit(); } } private void assertDesignPageElements() { assertEquals(designPageUrl(), browser.getCurrentUrl()); List<WebElement> ingredientGroups = browser.findElementsByClassName("ingredient-group"); assertEquals(5, ingredientGroups.size()); WebElement wrapGroup = browser.findElementByCssSelector("div.ingredient-group#wraps"); List<WebElement> wraps = wrapGroup.findElements(By.tagName("div")); assertEquals(2, wraps.size()); assertIngredient(wrapGroup, 0, "FLTO", "Flour Tortilla"); assertIngredient(wrapGroup, 1, "COTO", "Corn Tortilla"); WebElement proteinGroup = browser.findElementByCssSelector("div.ingredient-group#proteins"); List<WebElement> proteins = proteinGroup.findElements(By.tagName("div")); assertEquals(2, proteins.size()); assertIngredient(proteinGroup, 0, "GRBF", "Ground Beef"); assertIngredient(proteinGroup, 1, "CARN", "Carnitas"); WebElement cheeseGroup = browser.findElementByCssSelector("div.ingredient-group#cheeses"); List<WebElement> cheeses = proteinGroup.findElements(By.tagName("div")); assertEquals(2, cheeses.size()); assertIngredient(cheeseGroup, 0, "CHED", "Cheddar"); assertIngredient(cheeseGroup, 1, "JACK", "Monterrey Jack"); WebElement veggieGroup = browser.findElementByCssSelector("div.ingredient-group#veggies"); List<WebElement> veggies = proteinGroup.findElements(By.tagName("div")); assertEquals(2, veggies.size()); assertIngredient(veggieGroup, 0, "TMTO", "Diced Tomatoes"); assertIngredient(veggieGroup, 1, "LETC", "Lettuce"); WebElement sauceGroup = browser.findElementByCssSelector("div.ingredient-group#sauces"); List<WebElement> sauces = proteinGroup.findElements(By.tagName("div")); assertEquals(2, sauces.size()); assertIngredient(sauceGroup, 0, "SLSA", "Salsa"); assertIngredient(sauceGroup, 1, "SRCR", "Sour Cream"); } private void fillInAndSubmitOrderForm() { assertTrue(browser.getCurrentUrl().startsWith(orderDetailsPageUrl())); fillField("input#deliveryName", "Ima Hungry"); fillField("input#deliveryStreet", "1234 Culinary Blvd."); fillField("input#deliveryCity", "Foodsville"); fillField("input#deliveryState", "CO"); fillField("input#deliveryZip", "81019"); fillField("input#ccNumber", "4111111111111111"); fillField("input#ccExpiration", "10/19"); fillField("input#ccCVV", "123"); browser.findElementByCssSelector("form#orderForm").submit(); } private void submitEmptyOrderForm() { assertEquals(currentOrderDetailsPageUrl(), browser.getCurrentUrl()); // clear fields automatically populated from user profile fillField("input#deliveryName", ""); fillField("input#deliveryStreet", ""); fillField("input#deliveryCity", ""); fillField("input#deliveryState", ""); fillField("input#deliveryZip", ""); browser.findElementByCssSelector("form#orderForm").submit(); assertEquals(orderDetailsPageUrl(), browser.getCurrentUrl()); List<String> validationErrors = getValidationErrorTexts(); assertEquals(9, validationErrors.size()); assertTrue(validationErrors.contains("Ple
评论
    相关推荐
    • spring
      春天 1,HelloSpring IOC创建对象spring1模块 2,依赖注入spring2模块 3,自动装配spring3模块 4,注释spring4模块 5,JavaConfig spring5模块
    • Spring
      Spring
    • spring
      春天
    • springspring
      spring开发包 spring开发包 spring开发包 spring开发包
    • Spring
      春天
    • Spring
      spring ppt
    • Spring
      Spring
    • Spring
      Spring
    • SPRING
      SPRING.jl
    • spring
      spring sample demo