[英]Spring Boot Web Application using Selenium WebDriver
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.我正在尝试了解 spring boot 并且在尝试将 selenium 集成到我的 spring boot 应用程序中时遇到了一些问题。 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.输入框将包含一个 URL,然后该按钮将启动一个 selenium 浏览器,导航到输入的 URL。
I currently have a simple spring boot application consisting of the following:我目前有一个简单的 spring boot 应用程序,包括以下内容:
Contains an input form (user types URL here) which is passed to myController.包含传递给 myController 的输入表单(用户在此处键入 URL)。
@Controller
public class myController {
@Autowired
private WebDriver driver;
....
}
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.我正在尝试创建 Selenium WebDriver 的实例,以便我可以在需要时使用它。 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.提前谢谢你。
You need to have an instance of WebDriver, eg:您需要有一个 WebDriver 实例,例如:
@Bean
public WebDriver webDriver() {
return new FirefoxDriver();
//OR return new ChromeDriver();
}
in one of your configuration classes.在您的配置类之一中。 That would be anything annotated with @Configuration
or an annotation which includes this;那将是用@Configuration
或包含此注释的任何注释; in the most basic Spring Boot application (as used in Spring Boot examples), this would probably be something like this:在最基本的 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
注释的,而@SpringBootConfiguration
是用@Configuration
注释的。
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.我可以从 eclipse 启动 spring boot,而不是对 url localhost:8080/api/cenario/teste 进行 GET 调用。 So Selenium open the browser and execute the test cenarios on demand, than I show the results on a web page.所以 Selenium 打开浏览器并按需执行测试场景,而不是我在网页上显示结果。
But when I generate a jar file with the project, selenium fail to start the browser.但是当我用项目生成一个jar文件时,selenium无法启动浏览器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.