Memo

メモ > 技術 > 開発: Selenium > Java版Selenium環境をWindowsに構築

■Java版Selenium環境をWindowsに構築
■Eclipseインストール Pleiades - 日本語化プラグイン Eclipse, IntelliJ, PhpStorm... http://mergedoc.osdn.jp/ 「Eclipse 4.7 Oxygen」 のボタンから 「Windows 64bit」「Full Edition」「Java」 のEclipseをダウンロード 「pleiades-4.7.1-java-win-64bit-jre_20171019.zip」 を保存 展開して以下に移動 C:\Pleiades-4.7.1 初回は以下を起動 C:\Pleiades-4.7.1\eclipse\eclipse.exe -clean.cmd ワークスペースに「C:\Users\refirio\Selenium」を設定して起動(ソースコードを置く任意の場所) 次回は以下を起動。このファイルのショートカットを、デスクトップに作っておくといい C:\Pleiades-4.7.1\eclipse\eclipse.exe 以降は主に、以下のページを参照 [初心者向け] JavaでSeleniumを動かす - Qiita https://qiita.com/tsukakei/items/41bc7f3827407f8f37e8 ■Selenium入手 Downloads http://www.seleniumhq.org/download/ 本来は「Java 3.7.1」をダウンロードすればいいはずだが、今回は以前使った以下を使用する selenium-java-3.0.1 ■ChromeDriver入手 Downloads - ChromeDriver - WebDriver for Chrome https://sites.google.com/a/chromium.org/chromedriver/downloads 以前使ったドライバをインストール…と思ったが、使っているChromeが62なので、それにあったドライバを入手 ■Eclipse設定 メニューから「ファイル → 新規 → Javaプロジェクト」を実行 プロジェクト名に「selenium-sample」を入力して「完了」ボタンを押す プロジェクト内に「exe」フォルダを作成し、入手した chromedriver.exe を配置 プロジェクト内に「lib」フォルダを作成し、入手した client-combined-3.0.1-nodeps.jar を配置。selenium-java-3.0.1\lib の内容も同階層にすべて配置 プロジェクトを右クリック「ビルドパス → 外部アーカイブの追加」を選択し、 selenium-sample\lib 内のファイルをすべて追加 ■テスト実行 「src」フォルダを右クリックし、「新規 → クラス」を選択 パッケージに「jp.co.example.test」を入力し、名前に「SampleTest」を入力して完了を押す パッケージ名以外を以下に変更
import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class SampleTest { @Test public void testGoogleSearch() throws InterruptedException { // Optional, if not specified, WebDriver will search your path for chromedriver. System.setProperty("webdriver.chrome.driver", "C:/Users/refirio/Selenium/selenium-sample/exe/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.google.com/xhtml"); Thread.sleep(5000); // Let the user actually see something! WebElement searchBox = driver.findElement(By.name("q")); searchBox.sendKeys("ChromeDriver"); searchBox.submit(); Thread.sleep(5000); // Let the user actually see something! driver.quit(); } }
SampleTest.java を右クリックし、「実行 → JUnitテスト」で実行できる Chromeが立ち上がり、Googleにアクセスして検索結果が表示されれば成功

Advertisement