There is a class SeleniumWebDrivers:
public class SeleniumWebDrivers { public WebDriver getFirefox() { DesiredCapabilities capabilities = DesiredCapabilities.firefox(); return new FirefoxDriver(capabilities); } public WebDriver getChrome(String pathToBrowser) { System.setProperty("webdriver.chrome.driver", pathToBrowser); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); return new ChromeDriver(capabilities); } } Values ​​returned by any of these methods must be passed as a parameter when creating an object of the Parser_v2 class:
public class Parser_v2 implements Parser { private WebDriver driver; public Parser_v2(WebDriver driver) { driver = this.driver; // initDriver(); } // Other code ...... } I tried to do this:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="seleniumDrivers" class="com.api.SeleniumWebDrivers" scope="prototype" /> <bean id="parser" class="com.api.impl.parser.Parser_v2" scope="singleton"> <constructor-arg ref="#{seleniumDrivers.getFirefox()}" /> </bean> <bean id="config" class="com.api.PropertiesValues" scope="singleton"/> </beans> Result:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parser' defined in URL [file:src/main/resources/ApplicationContext.xml]: Cannot resolve reference to bean '#{seleniumDrivers.getFirefox()}' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'FirefoxDriver: firefox on WINDOWS (9ff62278-2ed7-4fed-9c11-6c2d0300254c)' is defined Also tried here so:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="seleniumDrivers" class="com.SeleniumWebDrivers" scope="prototype" /> <bean id="factoryDriver" factory-bean="seleniumDrivers" factory-method="getFirefox" /> <bean id="parser" class="com.impl.parser.Parser_v2" scope="singleton"> <constructor-arg ref="factoryDriver"/> </bean> <bean id="config" class="com.PropertiesValues" scope="singleton"/> </beans> Received:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parser' defined in URL [file:src/main/resources/ApplicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.api.impl.parser.Parser_v2]: Constructor threw exception; nested exception is java.lang.NullPointerException The 'getFirefox ()' method is called in both cases (since the browser is started), after which exceptions are thrown. In the first attempt, I don’t understand why the spring requires a bin with the name 'FirefoxDriver', and in the second case I don’t know why, instead of referring to the object, null is passed.
Question: how to create a Parser_v2 class bin (passing to it the constructor parameter returned by one of the methods of the SeleniumWebDrivers class)?
driver = this.driver;- is this line inParser_v2constructor not a typo? - zRrr