Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

windows7+eclipse+Selenium WebDriverその0006(JavaScriptを実行する)

参考にさせて頂いたページ

WebDriver(Java)からJavaScriptを実行させたい(5398)|teratail

[Selenium]executeScriptで外部jsファイルを読み込む - dackdive's blog

https://newcircle.com/bookshelf/selenium_tutorial/selenium_remote_control

HogeTestの記述内容

以下のように書くとIE11で、alertの実行が確認できた。
console.logはIEではうまく確認できなかった。
ChromeFirefoxコメントアウトしている箇所を解除すればとりあえず確認できる。

package com.example.tests;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;



public class HogeTest {
  public static WebDriver driver = null;
  public static JavascriptExecutor jexec;

  public static void main(String[] args) {
    // Firefoxを起動する
    // driver = new FirefoxDriver();
    // Chromeを起動する
    // System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
    // driver = new ChromeDriver();
    // IEを起動する
     System.setProperty("webdriver.ie.driver", "./driver/IEDriverServer.exe"); 
     driver = new InternetExplorerDriver();

    jexec = (JavascriptExecutor) driver;
    jexec.executeScript("console.log('testtest');");
    jexec.executeScript("alert('testtest');");
    // driver.quit();
  }
}

今回はここまで。