0

There is a requirement where I have to add extension in browser before running scripts. For that I decided to use Browser Options (EdgeOptions).Browser option is introduced in selenium 4.

All required dependencies I have already imported but now I am getting exception in PageFactory.initElements.

  • selenium-api-4.1.1
  • selenium-chrome-driver-4.1.1
  • selenium-chromium-driver-4.1.1
  • selenium-edge-driver-4.1.1
  • selenium-java-4.1.1
  • selenium-remote-driver-4.1.1
  • selenium-support-4.1.1

Do I have to add some other dependencies also?

Message: cucumber.runtime.CucumberException: Failed to instantiate class com.proj.stepdefinitions.common.ConfigTestData
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:40)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:26)
at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:59)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:222)
at cucumber.runtime.Runtime.runHooks(Runtime.java:210)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:200)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
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 cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:93)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:37)
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 cucumber.api.junit.Cucumber.run(Cucumber.java:98)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:34)
    ... 31 more
    Caused by: java.lang.NoSuchMethodError: org.openqa.selenium.support.PageFactory.initElements(Lorg/openqa/selenium/WebDriver;Ljava/lang/Class;)Ljava/lang/Object;
        at com.proj.stepdefinitions.common.ConfigTestData.<init>(ConfigTestData.java:115)
        ... 36 more

ConfigTestData code below :

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;

import com.step.AfterScenarioScript; //project class


public class ConfigTestData {
    
    //some constants
    
    public static WebDriver driver = null;

    //exception here    
    CommonMethods cm = PageFactory.initElements(driver, 
    CommonMethods.class);

    AfterScenarioScript afterScenarioScript = PageFactory.initElements(driver, AfterScenarioScript.class); 
    
    private CreateFile file = PageFactory.initElements(driver, CreateFile.class);
    
    @Before
    public void init(Scenario scenario) throws IOException {
        prop = Login.readPropertiesFile();
        
        
        //some code
    }

     

    @After
    public void afterScenario(Scenario scenario) throws Throwable {
        //some code
    }
    
    //some utility methods
    
}
3
  • 1
    Post the code you've tried so far. Commented Feb 14, 2023 at 13:52
  • Can you show us your ConfigTestData class ? Commented Feb 14, 2023 at 14:23
  • Hi, I have uploaded ConfigTestData code Commented Feb 15, 2023 at 11:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.