Help us understand the problem. What is going on with this article?

Selenide4.0でEdgeDriverを使う

More than 3 years have passed since last update.

背景

Selenideでは通常以下のようにしてブラウザを切り替えます。

Configuration.browser = WebDriverRunner.CHROME;
System.setProperty("webdriver.chrome.driver", "[パス]/chromedriver.exe");

しかし、Selenide4.0の段階では同じようにEdgeを指定することが出来ません。

Configuration.browser = WebDriverRunner.EDGE; // EDGEが存在しないためエラー。

解決法

ずばり、こうです。

System.setProperty("webdriver.edge.driver", "[パス]/MicrosoftWebDriver.exe");
Configuration.browser = "org.openqa.selenium.edge.EdgeDriver";

※ドライバはこちら→https://developer.microsoft.com/en-us/microsoft-edge/platform/documentation/dev-guide/tools/webdriver/

これでEdgeでテストできます。WebDriverRunnerクラスに定数を準備していない辺り、Selenide4.0ではまだEdge対応が不完全ということが伺えます。不具合に遭遇する可能性は高いので自己責任で・・・

きっとバージョンがあがれば普通にWebDriverRunner.EDGEと書けるようになるのでしょう。

おまけ(解決までの流れ)

SelenideではEdgeは一切使えないのかな―と思ったのですが、Mavenの依存関係よくよく見るとselenium-edge-driver-3.0.0.jarが入っているではありませんか。きっと動かす方法あるのだろうと思いました。

試しに

Configuration.browser = "edge";

と指定してみました。すると

java.lang.IllegalArgumentException: java.lang.ClassNotFoundException: edge
    at com.codeborne.selenide.webdriver.WebDriverFactory.createInstanceOf(WebDriverFactory.java:230)
    at com.codeborne.selenide.webdriver.WebDriverFactory.createWebDriver(WebDriverFactory.java:51)
    at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.createDriver(WebDriverThreadLocalContainer.java:244)
    at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.getAndCheckWebDriver(WebDriverThreadLocalContainer.java:117)
    at com.codeborne.selenide.WebDriverRunner.getAndCheckWebDriver(WebDriverRunner.java:128)
    at com.codeborne.selenide.impl.Navigator.navigateToAbsoluteUrl(Navigator.java:68)
    at com.codeborne.selenide.impl.Navigator.open(Navigator.java:31)
    at com.codeborne.selenide.Selenide.open(Selenide.java:81)
    at com.codeborne.selenide.Selenide.open(Selenide.java:55)
    at HogeTest.test(HogeTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: edge
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.codeborne.selenide.webdriver.WebDriverFactory.createInstanceOf(WebDriverFactory.java:216)
    ... 32 more

スタックトレースからWebDriverFactory.createInstanceOfで「edge」を受け入れてくれていないことがわかります。そこで、WebDriverFactoryクラスの中からcreateInstanceOfを呼び出している部分を探しました。すると、次のようなコードが見つかりました。

protected WebDriver createSafariDriver(Proxy proxy) {
  return createInstanceOf("org.openqa.selenium.safari.SafariDriver", proxy);
}

この記述から、どうやらドライバーのクラス名を記述すれば良さそうだとわかりました。selenium-edge-driver-3.0.0.jarの中身を見るとorg.openqa.selenium.edge.EdgeDriverというクラスがあるのでこれで良さそうです。早速次のコードで試してみます。

Configuration.browser = "org.openqa.selenium.edge.EdgeDriver";
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.edge.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/MicrosoftWebDriver. The latest version can be downloaded from http://go.microsoft.com/fwlink/?LinkId=619687
    at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.edge.EdgeDriverService.access$000(EdgeDriverService.java:33)
    at org.openqa.selenium.edge.EdgeDriverService$Builder.findDefaultExecutable(EdgeDriverService.java:63)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.edge.EdgeDriverService.createDefaultService(EdgeDriverService.java:55)
    at org.openqa.selenium.edge.EdgeDriver.<init>(EdgeDriver.java:120)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.codeborne.selenide.webdriver.WebDriverFactory.createInstanceOf(WebDriverFactory.java:223)
    at com.codeborne.selenide.webdriver.WebDriverFactory.createWebDriver(WebDriverFactory.java:51)
    at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.createDriver(WebDriverThreadLocalContainer.java:244)
    at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.getAndCheckWebDriver(WebDriverThreadLocalContainer.java:117)
    at com.codeborne.selenide.WebDriverRunner.getAndCheckWebDriver(WebDriverRunner.java:128)
    at com.codeborne.selenide.impl.Navigator.navigateToAbsoluteUrl(Navigator.java:68)
    at com.codeborne.selenide.impl.Navigator.open(Navigator.java:31)
    at com.codeborne.selenide.Selenide.open(Selenide.java:81)
    at com.codeborne.selenide.Selenide.open(Selenide.java:55)
    at HogeTest.test(HogeTest.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

なにやら怒られていますが、どうやらwebdriver.edge.driverというシステムプロパティを定義しろということらしいです。ChromeやIEを使うときと同じですね。そこで、システムプロパティの設定を加えました。

System.setProperty("webdriver.edge.driver", "[パス]/MicrosoftWebDriver.exe");
Configuration.browser = "org.openqa.selenium.edge.EdgeDriver";

これで無事、動くようになりました。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away