I tried to implement a selenium example to login github through Spring Boot.
When I ran the test method, I got this error shown below.
java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@3d3c886f testClass = com.github.selenium.process.GithubProcess, locations = [], classes = [com.github.selenium.SeleniumApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true", "server.port=0"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4310d43, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@27f981c6, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@771a660, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@9da1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@1ffaf86, org.springframework.boot.test.context.SpringBootTestAnnotation@169dd363], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Caused by: java.lang.ClassNotFoundException: org.springframework.web.util.UriTemplateHandler
How can I fix it?
Here is the code shown below.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class GithubProcess {
private static WebDriver driver;
String baseUrl = "https://github.com/login";
@BeforeAll
static void beforeAll() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
@Order(1)
public void login() throws InterruptedException {
// github login through selenium with username and password
driver.get(baseUrl);
WebElement usernameOrEmail = driver.findElement(By.id("login_field"));
WebElement password = driver.findElement(By.id("password"));
WebElement signInButton = driver.findElement(By.cssSelector("input[type='submit']"));
String usernameOrEmailField = "my-username-or-email";
String passwordField = "my-password";
usernameOrEmail.sendKeys(usernameOrEmailField);
password.sendKeys(passwordField);
signInButton.click();
Thread.sleep(1000);
}
}
Here are my dependency defined in pom.xml
<properties>
<java.version>17</java.version>
<selenium.version>4.6.0</selenium.version>
<webdrivermanager.version>5.3.1</webdrivermanager.version>
<junit.jupiter.version>5.9.1</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Selenium-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<!--Webdriver Manager-->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
<!--JUnit5-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
CodePudding user response:
After defining this dependency in the pom.xml, the issue disappeared.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>