Skip to content

Instantly share code, notes, and snippets.

@sezemiadmin
Last active October 23, 2018 03:17
Show Gist options
  • Select an option

  • Save sezemiadmin/66be54cb2bd0e872640fc92379255c1a to your computer and use it in GitHub Desktop.

Select an option

Save sezemiadmin/66be54cb2bd0e872640fc92379255c1a to your computer and use it in GitHub Desktop.
テストの自動化とそれを支えるツール サンプルプログラム
package sample;
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.openqa.selenium.By;
import com.codeborne.selenide.Condition;
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
public class CalculatorTest {
@Test(expected = Exception.class) // @Test でテスト対象にする
public void 数量0でcalcを呼び出すと例外が発生する() throws Exception { // 日本語で書いておくと結果がわかりやすい
Calculator.calc(100, 0);
}
@Test
public void 数量1でcalcを呼び出す() throws Exception {
// setup 準備
int price = 100;
int count = 1;
int expected = 100;
// when
int actual = Calculator.calc(price, count);
// then 検証
assertThat(Calculator.calc(100, 1), is(100)); // assertThatメソッドをよく使う第1引数と第2引数が一致している
}
/* 中略 */
/**
* Selenideテスト
*/
@Test
public void selenideTest() {
// Configure Chrome driver
Configuration.browser = WebDriverRunner.CHROME;
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
// ブラウザでGoogleを開く
Selenide.open("http://www.google.com/");
// Google検索
SelenideElement element = Selenide.$(By.name("q")); // htmlのname属性をターゲットに
element.val("selenide").pressEnter();
// 検索結果を確認
ElementsCollection results = Selenide.$$(By.cssSelector("#ires .g"));
results.shouldHaveSize(10);
results.get(2).shouldHave(Condition.text("Selenide: concise UI tests in Java")); // 検索順位が3位...
}
}
<dependencies>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment