このエントリーをはてなブックマークに追加

googleへアクセスし、検索結果を確認する

googleのTOPページへアクセスし、keywordの[selenide]を入力し、検索結果の1件目のテキストを確認するサンプル

package jp.dip.snowhiro;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.screenshot;

import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverRunner;

/**
 * GoogleのTOPページへアクセスし検索結果を確認するサンプル
 * @author snowhiro
 */
public class GoogleSearchTest {

   @BeforeClass
   public static void setupClass() {
      Configuration.browser = WebDriverRunner.CHROME;

      Configuration.remote = null;

      Configuration.timeout = 1000L;
      System.setProperty("webdriver.chrome.driver", "webdriver/chromedriver");

      Configuration.reportsFolder = "report";

      Configuration.reopenBrowserOnFail = true;
   }


   @Test
   public void testNo01_googleを表示し検索してから1件目を確認する() {
      open("https://www.google.co.jp/");

      screenshot("01_初期表示");

      $("#lst-ib").setValue("selenide").pressEnter();
      screenshot("02_キー入力");

      // cssクラスの r 要素の数をチェックする
      $$(By.className("r")).shouldHaveSize(10);
      screenshot("03_検索後");

      // cssクラスの r 要素の1つ目のテキストの内容をチェックする
      $(".r").shouldHave(text("Selenide: concise UI tests in Java"));
   }

}