当我使用selenium使用maven运行testng时出错

时间:2018-05-14 21:50:55

标签: maven selenium testng

我遇到使用selenium运行Page Factory with 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 个答案:

答案 0 :(得分:0)

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

  • ITestResult对象代表测试结果。
  • Method表示实际方法的对象。
  • ITestContext对象代表正在执行的<test>

有关所有这些内容的更多信息,请访问official documentation site

要解决您的问题,您有两种方法可以解决问题。

  1. 方法1(这不会让你并行运行测试)
  2. 更改@BeforeMethod
    @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");
    }
    

    您的代码正在使用WebDriver中的本地@BeforeMethod变量。改变后的版本基本上利用了数据成员,而@BeforeMethod只是实例化它。您还需要从WebDriver方法中删除@Test参数。

    1. 方法2
    2. 您使用方法1中提到的相同详细信息,但您使用的是ThreadLocal变体,而不是常规WebDriver对象。您可以从我的博客文章here中了解有关使用ThreadLocal变量的更多信息。