web测试框架

  1. Selenium

    1. package com.lpy.unittest;
      
      
      import org.junit.jupiter.api.Test;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.chrome.ChromeDriver;
      import org.springframework.boot.test.context.SpringBootTest;
      
      
      import static org.easymock.EasyMock.*;
      import static org.hamcrest.MatcherAssert.assertThat;
      
      @SpringBootTest
      class SeleniumTestApplicationTests {
      
          @Test
          public void joeHas5Friends() {
              WebDriver webDriver = new ChromeDriver();
              webDriver.get("https://www.igfwz.com/mingzi/393830372c30/");
              WebElement query =
                      webDriver.findElement(By.name("s"));
              query.sendKeys("Test-driven development");
              WebElement goButton =
                      webDriver.findElement(By.className("btn btn-lg btn-success rounded-0"));
              goButton.click();
              assertThat(webDriver.getTitle(),
                      startsWith("Test-driven development") == null);
      
              webDriver.quit();
          }
      }
      
      
    2. WebDriver是一个接口,创建一个在维基百科中搜索的测试,并使用chrom驱动程序,

    3. 使用指令打开一个url

    4. 网页打开后,根据名称找到搜索框并指定要搜索的内容

    5. 指定要搜索的内容后,找到并点击按钮

    6. 进入目标网页后进行测试验证,

    7. 使用完驱动程序后,应将其关闭

    8. selenium是最常用的Web测试框架,但他很低级,需要做大量微调

  2. Selenide

    1. package com.lpy.unittest;
      
      
      import org.junit.jupiter.api.Test;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.WebElement;
      import org.openqa.selenium.chrome.ChromeDriver;
      import org.springframework.boot.test.context.SpringBootTest;
      
      import static com.codeborne.selenide.Selenide.*;
      import static org.easymock.EasyMock.startsWith;
      import static org.hamcrest.MatcherAssert.assertThat;
      
      @SpringBootTest
      class SelenideTestApplicationTests {
      
          @Test
          public void wikipediaSearchFeature() throws
                  InterruptedException {
              // 打开维基百科页面
              open("https://www.igfwz.com/mingzi/393830372c30/");
              // 搜索TDD
              $(By.name("s")).setValue("Test-driven" +
                      "development");
              // 单击搜索按钮
              $(By.className("btn btn-lg btn-success rounded-0")).click();
              // 检查结果
              assertThat(title(), startsWith("Test-driven" +
                      "development") == null);
          }
      }
      
      
    2. Selenide是一个基于Selenium的项目,提供了优良的测试编写语法,提高了测试的可读性,他将WebDriver和配置隐藏,同时提供了极大的定制空间

  3. JBehave

    1. package com.lpy.unittest;
      
      
      import org.jbehave.core.annotations.Given;
      import org.jbehave.core.annotations.Then;
      import org.jbehave.core.annotations.When;
      import org.jbehave.core.io.CodeLocations;
      import org.jbehave.core.io.StoryFinder;
      import org.jbehave.core.junit.JUnitStories;
      import org.openqa.selenium.By;
      import org.springframework.boot.test.context.SpringBootTest;
      
      import java.util.Arrays;
      import java.util.List;
      
      import static com.codeborne.selenide.Selenide.*;
      import static org.hamcrest.MatcherAssert.assertThat;
      import static org.hamcrest.Matchers.containsString;
      
      @SpringBootTest
      class JBehaveTestApplicationTests extends JUnitStories {
      
          @Given("I go to Wikipedia homepage")
          public void goToWikiPage() {
              open("https://www.igfwz.com/mingzi/393830372c30/");
          }
          @When("I enter the value $value on a field named " +
                  "$fieldName")
          public void enterValueOnFieldByName(String value,
                                              String fieldName){
              $(By.name(fieldName)).setValue(value);
          }
          @When("I click the button $buttonName")
          public void clickButonByName(String buttonName){
              $(By.name(buttonName)).click();
          }
          @Then("the page title contains $title")
          public void pageTitleIs(String title) {
              assertThat(title(), containsString(title));
          }
      
          @Override
          public List<String> storyPaths() {
              return new StoryFinder().
      
                      findPaths(CodeLocations.codeLocationFromClass(
      
                                      this.getClass()),
      
                              Arrays.asList("**/*.story"),
      
                              Arrays.asList(""));
      
          }
      
      }
      
      
    2. @Given,表示要成功执行后续操作必须满足的一个前置条件,指定前置条件后定义一些操作,用@When

    3. 操作是使用注解@When定义的,在这里,这些步骤设置一个文本框的值并单击特定按钮,操作执行完毕后进行验证,引入参数可以让步骤更灵活

    4. 验证是使用注解@Then声明的

    5. 这里使用前面定义的步骤文本,依次执行与这些步骤相关联的代码,如果有代码出现问题,将终止执行,而场景本身将被视为失败

    6. 这个故事运行时,将在浏览器看到执行的操作,运行完毕后,生成一个包含执行结果的报告,他存储在目录build/reports/jbehave中

  4. Cucumber

    1. package com.lpy.unittest;
      
      
      import org.jbehave.core.annotations.Given;
      import org.jbehave.core.annotations.Then;
      import org.jbehave.core.annotations.When;
      import org.jbehave.core.io.CodeLocations;
      import org.jbehave.core.io.StoryFinder;
      import org.jbehave.core.junit.JUnitStories;
      import org.openqa.selenium.By;
      import org.springframework.boot.test.context.SpringBootTest;
      
      import java.util.Arrays;
      import java.util.List;
      
      import static com.codeborne.selenide.Selenide.*;
      import static org.hamcrest.MatcherAssert.assertThat;
      import static org.hamcrest.Matchers.containsString;
      
      @SpringBootTest
      class CucumberTestApplicationTests {
      
          @Given("^I go to Wikipedia homepage$")
          public void goToWikiPage() {
              open("http://en.wikipedia.org/wiki/Main_Page");
          }
          @When("^I enter the value (.*) on a field named (.*)$")
          public void enterValueOnFieldByName(String value,
                                              String fieldName){
              $(By.name(fieldName)).setValue(value);
          }
          @When("^I click the button (.*)$")
          public void clickButonByName(String buttonName){
              $(By.name(buttonName)).click();
          }
          @Then("^the page title contains (.*)$")
          public void pageTitleIs(String title) {
              assertThat(title(), containsString(title));
          }
      }
      
      
    2. 和jbehave的差别在于Cucumber定义步骤文本的方法,他使用正则表达式匹配变量类型,而不像JBehave那样根据方法签名推断

    3. 结果报告存储在目录build/reports/cucumber-report中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值