当前位置: 首页 > 知识库问答 >
问题:

我在使用selenium与maven一起运行testng时出错

燕鸿波
2023-03-14

我在使用selenium与maven运行页面工厂时遇到问题,如果有人能提供帮助,我将不胜感激。当我运行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
package pageObjectFactoryMaven;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class SelectCoursePage {

    WebDriver driver;
    public SelectCoursePage (WebDriver driver) {
        PageFactory.initElements(driver,this);
    }

    @FindBy (id = "navBlue")
    WebElement studying;

    @FindBy (css = "element")
    WebElement findacourse;

    @FindBy ( id = "query")
    WebElement entersearchitem;

    public WebElement studying(){
        return studying;
    }

    public WebElement findacourse(){
        return findacourse;
    }

    public WebElement entersearchitem(){
        return entersearchitem;
    }
}
package pageObjectFactoryMaven;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class SignInPage {

    WebDriver driver;

    public SignInPage (WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    @FindBy (id ="MUA_CODE")
    WebElement enterusername;

    @FindBy (id ="PASSWORD" )
    WebElement enterpassword;

    @FindBy (name = "OK.DUMMY.MENSYS.1")
    WebElement clickLogin;

    public WebElement enterusername() {
        return enterusername;   
    }

    public WebElement enterpassword() {
        return enterpassword;
    }

    public WebElement clickLogin() {
        return clickLogin;
    }
}
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>

共有1个答案

鲍向笛
2023-03-14

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

  • 表示测试结果的对象
  • 表示实际方法的对象
  • 表示<代码>

有关所有这些以及更多信息,请访问官方文档网站

要解决你的问题,你有两种方法。

  1. 方法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");
}

您的代码正在使用BeforeMethod中的本地Web驱动程序变量。修改后的方法基本上利用了数据成员,而您的BeforeMethod只是实例化了它。您还需要从测试方法中去掉WebDriver参数。

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

 类似资料: