-
Selenium
-
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(); } }
-
WebDriver是一个接口,创建一个在维基百科中搜索的测试,并使用chrom驱动程序,
-
使用指令打开一个url
-
网页打开后,根据名称找到搜索框并指定要搜索的内容
-
指定要搜索的内容后,找到并点击按钮
-
进入目标网页后进行测试验证,
-
使用完驱动程序后,应将其关闭
-
selenium是最常用的Web测试框架,但他很低级,需要做大量微调
-
-
Selenide
-
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); } }
-
Selenide是一个基于Selenium的项目,提供了优良的测试编写语法,提高了测试的可读性,他将WebDriver和配置隐藏,同时提供了极大的定制空间
-
-
JBehave
-
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("")); } }
-
@Given,表示要成功执行后续操作必须满足的一个前置条件,指定前置条件后定义一些操作,用@When
-
操作是使用注解@When定义的,在这里,这些步骤设置一个文本框的值并单击特定按钮,操作执行完毕后进行验证,引入参数可以让步骤更灵活
-
验证是使用注解@Then声明的
-
这里使用前面定义的步骤文本,依次执行与这些步骤相关联的代码,如果有代码出现问题,将终止执行,而场景本身将被视为失败
-
这个故事运行时,将在浏览器看到执行的操作,运行完毕后,生成一个包含执行结果的报告,他存储在目录build/reports/jbehave中
-
-
Cucumber
-
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)); } }
-
和jbehave的差别在于Cucumber定义步骤文本的方法,他使用正则表达式匹配变量类型,而不像JBehave那样根据方法签名推断
-
结果报告存储在目录build/reports/cucumber-report中
-
08-09
3698
02-21
1690
04-14
3512
09-14
2351
08-30
2万+
10-04