java 使用 Selenium WebDriver 的 Spring Boot Web 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44781339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Web Application using Selenium WebDriver
提问by user3054735
I am trying to get my head around spring boot and I am having some issues trying to integrate selenium into my spring boot application. I am trying to achieve a simple web page, which has a input box and button. The input box will contain a URL and the button will then launch a selenium browser navigating to that URL entered.
我正在尝试了解 Spring Boot,但在尝试将 selenium 集成到我的 Spring Boot 应用程序中时遇到了一些问题。我正在尝试实现一个简单的网页,它有一个输入框和按钮。输入框将包含一个 URL,然后该按钮将启动一个 selenium 浏览器,导航到输入的 URL。
I currently have a simple spring boot application consisting of the following:
我目前有一个简单的 spring boot 应用程序,包括以下内容:
index.html
索引.html
Contains an input form (user types URL here) which is passed to myController.
包含传递给 myController 的输入表单(用户在此处键入 URL)。
myController.java
我的控制器
@Controller
public class myController {
@Autowired
private WebDriver driver;
....
}
pom.xml
pom.xml
Contains selenium..
含硒..
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
My error when I run my project:
运行项目时出现错误:
APPLICATION FAILED TO START
Description:
Field driver in com.project.myController required a bean of type 'org.openqa.selenium.WebDriver' that could not be found.
Action:
Consider defining a bean of type 'org.openqa.selenium.WebDriver' in your configuration.
I am trying to create an instance of Selenium WebDriver so that I can use it whenever I need to. I will only ever need it in this controller, so I declared it here. What am I missing? Any help will be appreciated. Thank you in advance.
我正在尝试创建 Selenium WebDriver 的实例,以便我可以在需要时使用它。我只会在这个控制器中需要它,所以我在这里声明了它。我错过了什么?任何帮助将不胜感激。先感谢您。
回答by Danny Yeshurun
You need to have an instance of WebDriver, e.g.:
您需要有一个 WebDriver 实例,例如:
@Bean
public WebDriver webDriver() {
return new WebDriver();
}
in one of your configuration classes. That would be anything annotated with @Configuration
or an annotation which includes this; in the most basic Spring Boot application (as used in Spring Boot examples), this would probably be something like this:
在您的配置类之一中。那将是任何带有注释的@Configuration
或包含此注释的注释;在最基本的 Spring Boot 应用程序中(在 Spring Boot 示例中使用),这可能是这样的:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// Your beans go here
}
because @SpringBootApplication
is annotated with @SpringBootConfiguration
which is annotated with @Configuration
.
因为用@SpringBootApplication
注释 用@SpringBootConfiguration
注释@Configuration
。
回答by Rafael B. Marcílio
package br.com.api.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "api/cenario/teste")
public class TesteCenarioController {
private WebDriver driver;
@GetMapping
public ResponseEntity<String> teste() {
setUpGeckoDriver();
FirefoxOptions options = new FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
options.setCapability("marionete", true);
this.driver = new FirefoxDriver(options);
System.err.println("DRIVER OK");
this.driver.get("https://www.phptravels.net/blog");
WebElement findElement = this.driver.findElement(By.xpath("//*[@id=\"body-section\"]/div/div/div[1]/div/div[1]"));
String testResult = "LATEST POSTS".equals( findElement.getText() ) ? "PASS" : "FAIL";
this.driver.quit();
return new ResponseEntity<String>(testResult, HttpStatus.OK);
}
private void setUpGeckoDriver() {
ClassPathResource classPathResource = new ClassPathResource("selenium/geckodriver.exe");
InputStream inputStream = null;
try {
inputStream = classPathResource.getInputStream();
File geckodriverFile = File.createTempFile("geckodriver", ".exe"); ;
FileOutputStream out = new FileOutputStream( geckodriverFile );
IOUtils.copy(inputStream, out);
System.err.println( geckodriverFile.getCanonicalPath());
System.setProperty("webdriver.gecko.driver", geckodriverFile.getCanonicalPath() );
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
}
On my resources directory I have selenium/geckodriver.exe
.
在我的资源目录中,我有selenium/geckodriver.exe
.
My pom is the following:
我的pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testes-regressao</groupId>
<artifactId>testes-regressao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testes-regressao</name>
<description>api spring boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
I can start spring boot from eclipse than make a GET call to the url localhost:8080/api/cenario/teste. So Selenium open the browser and execute the test cenarios on demand, than I show the results on a web page.
我可以从 eclipse 启动 spring boot,而不是对 url localhost:8080/api/cenario/teste 进行 GET 调用。所以 Selenium 打开浏览器并按需执行测试场景,而不是我在网页上显示结果。
But when I generate a jar file with the project, selenium fail to start the browser.
但是当我用项目生成一个jar文件时,selenium启动浏览器失败。