Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我使用selenium与maven一起运行testng时出现错误_Maven_Selenium_Testng - Fatal编程技术网

当我使用selenium与maven一起运行testng时出现错误

当我使用selenium与maven一起运行testng时出现错误,maven,selenium,testng,Maven,Selenium,Testng,我在使用selenium与maven运行Page Factory时遇到问题,如果有人能提供帮助,我将不胜感激。当我运行TestPage类时,我得到以下错误: org.testng.TestNGException: Cannot inject @Test annotated Method [SelectCourse] with [interface org.openqa.selenium.WebDriver]. For more information on native dependency

我在使用selenium与maven运行Page Factory时遇到问题,如果有人能提供帮助,我将不胜感激。当我运行TestPage类时,我得到以下错误:

org.testng.TestNGException: 
Cannot inject @Test annotated Method [SelectCourse] with [interface org.openqa.selenium.WebDriver].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection

4.0.0
亚硒酸甲酯
硒项目
0.0.1-快照
罐子
硒项目
http://maven.apache.org
1.10
1.8
org.testng
testng
6.14.3
测试
org.seleniumhq.selenium
硒爪哇
3.12.0
org.apache.maven.plugins
maven编译器插件
${jdk.level}
${jdk.level}
org.apache.maven.plugins
maven surefire插件
2.21.0
testng.xml

问题出在测试代码中。您的
@Test
方法签名表示它需要一个
WebDriver
对象作为参数,但您还没有告诉TestNG它应该如何查找实例,然后将其注入您的
@Test
方法。TestNG本身只能将以下内容作为参数注入到
@Test
方法中(这就是所谓的本机注入

  • ITestResult
    表示测试结果的对象
  • 方法
    表示实际方法的对象
  • ITestContext
    对象,表示正在执行的
有关所有这些以及更多信息,请访问

要解决问题,有两种方法

  • 方法1(这将不允许并行运行测试)
  • 将您的
    @before方法更改为

    @BeforeMethod
    public void setup (){
        System.setProperty("webdriver.chrome.driver", "D:/Drivers/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("wwww.hhg.com");
    }
    
    对这样的事情:

    private WebDriver driver = null;
    
    @BeforeMethod
    public void setup (){
        System.setProperty("webdriver.chrome.driver", "D:/Drivers/chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("wwww.hhg.com");
    }
    
    您的代码正在使用
    @before方法
    中的本地
    WebDriver
    变量。修改后的方法基本上利用了一个数据成员,而您的
    @beforethod
    只是实例化了它。您还需要从
    @Test
    方法中去掉
    WebDriver
    参数

  • 方法2

  • 您使用的细节与方法1中提到的相同,但您使用的是ThreadLocal变量,而不是常规的
    WebDriver
    对象。您可以从我的博客文章中阅读更多关于使用ThreadLocal变量的信息。

    我尝试了第一种方法,但没有成功,并且出现了相同的错误。感谢@Krishna Mahadevan private WebDriver=null@BeforeMethod public void setup(){System.setProperty(“webdriver.chrome.driver”,“D:/Drivers/chromedriver.exe”);driver=new chromedriver();driver.get(“wwww.hhg.com”);}您的测试方法是什么样的?您得到的错误是什么?我没有对测试方法做任何更改,因为我不明白你提到的第二种方法。我得到的错误是-无法使用[interface org.openqa.selenium.WebDriver]注入@Test注释的方法[SelectCourse]。您需要从
    @Test
    方法中删除
    WebDriver
    参数。您的测试方法现在必须从类级别属性中引用
    WebDriver
    。我想我的答案中也包括了这一部分。
    package TestClass;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    
    import pageObjectFactoryMaven.SelectCoursePage;
    import pageObjectFactoryMaven.SignInPage;
    
    public class TestPage {
    
        @BeforeMethod
        public void setup (){
            System.setProperty("webdriver.chrome.driver", "D:/Drivers/chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("wwww.hhg.com");
        }
    
        @Test
        public void SelectCourse (WebDriver driver) {
            SelectCoursePage POF1 = new SelectCoursePage (driver);
            POF1.studying().click();
            POF1.findacourse().click();
            POF1.entersearchitem().sendKeys("computer science");    
        }
    
        @Test
        public void SignIn (WebDriver driver) {
            SignInPage POF2 = new SignInPage (driver);
            POF2.enterusername().sendKeys("kff");
            POF2.enterpassword().sendKeys("jdnd");
            POF2.clickLogin().click();     
        }
    }
    
    <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>SeleniumExeterUni_POM</groupId>
      <artifactId>SeleniumProject</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>SeleniumProject</name>
      <url>http://maven.apache.org</url>
    
      <properties>
            <jre.level>1.10</jre.level>
            <jdk.level>1.8</jdk.level>
        </properties>
    
    
      <dependencies>
        <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>test</scope>
    </dependency>
    
         <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.12.0</version>
    </dependency>
      </dependencies>
    
         <build>
           <plugins>
                    <!-- Compiler plug-in -->
             <plugin>
        <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${jdk.level}</source>
                        <target>${jdk.level}</target>
                    </configuration>
    
    
       </plugin>
    
    
              <!-- Below plug-in is used to execute tests -->
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.21.0</version>
    
    
                    <configuration>
                        <suiteXmlFiles>
                            <!-- TestNG suite XML files -->
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
    
           </build>
    </project>
    
    @BeforeMethod
    public void setup (){
        System.setProperty("webdriver.chrome.driver", "D:/Drivers/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("wwww.hhg.com");
    }
    
    private WebDriver driver = null;
    
    @BeforeMethod
    public void setup (){
        System.setProperty("webdriver.chrome.driver", "D:/Drivers/chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("wwww.hhg.com");
    }