2016-08-03 89 views
1

這是情況。我是QA自動化工程師,現在我負責在Jenkins建立CI框架,但現在我遇到了Maven問題。當我嘗試運行mvn test命令時,出現以下錯誤。然而,當作爲maven測試運行時,測試在eclipse中完美地工作。Mvn測試cmd行構建失敗,Eclipse中的測試工作

T E S T S 
------------------------------------------------------- 
Running TestSuite 
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configur 
[email protected] 
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configur 
[email protected] 
Tests run: 16, Failures: 1, Errors: 0, Skipped: 14, Time elapsed: 0.66 sec <<< F 
AILURE! 
beforeTest(fcstestingsuite.fsnrgn.LoginTest) Time elapsed: 0.442 sec <<< FAILU 
RE! 
java.lang.IllegalStateException: The path to the driver executable must be set b 
y the webdriver.chrome.driver system property; for more information, see https:/ 
/github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be dow 
nloaded from http://chromedriver.storage.googleapis.com/index.html 
     at com.google.common.base.Preconditions.checkState(Preconditions.java:19 
9) 
     at org.openqa.selenium.remote.service.DriverService.findExecutable(Drive 
rService.java:109) 
     at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDrive 
rService.java:32) 
     at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExe 
cutable(ChromeDriverService.java:137) 
     at org.openqa.selenium.remote.service.DriverService$Builder.build(Driver 
Service.java:296) 
     at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(C 
hromeDriverService.java:88) 
     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116) 

     at fcstestingsuite.fsnrgn.LoginTest.beforeTest(LoginTest.java:54) 


Results : 

Failed tests: beforeTest(fcstestingsuite.fsnrgn.LoginTest): The path to the dr 
iver executable must be set by the webdriver.chrome.driver system property; for 
more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. 
The latest version can be downloaded from http://chromedriver.storage.googleapis 
.com/index.html 

Tests run: 16, Failures: 1, Errors: 0, Skipped: 14 

正如您所看到的,它與我的chrome系統屬性/路徑有關。在我的項目中,我有一個測試包和頁面對象包。我在對象類中設置了我的chrome系統屬性,並將該類導入到在eclipse中正常工作的測試類中。我不太清楚爲什麼Maven有這個問題。看樣對象及以下

頁面類

package pageobjectfactory; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.support.FindBy; 
import org.testng.Assert; 
// URL = http://www.ourfsn.com/myfsn/ 

public class Ourfsnlogin { 

    @FindBy(id="ctl00_ContentPlaceHolder1_tbxUname") 
    WebElement login; 
    @FindBy(id="ctl00_ContentPlaceHolder1_tbxPword") 
    WebElement password; 
    @FindBy(id="ctl00_ContentPlaceHolder1_btnSubmit") 
    WebElement submit; 
    @FindBy(name="ctl00$ContentPlaceHolder1$rptAccounts$ctl01$AccountSwitch") 
    WebElement PETSMARTUS; 
    @FindBy(name="ctl00$ContentPlaceHolder1$rptAccounts$ctl02$AccountSwitch") 
    WebElement PETSMARTCAD; 
    @FindBy(name="ctl00$ContentPlaceHolder1$rptAccounts$ctl03$AccountSwitch") 
    WebElement PETSMARTPR; 
    @FindBy(id="ctl00_lblTopLogin") 
    WebElement PETSMARTUSASSERT; 
    @FindBy(id="ctl00_lblTopLogin") 
    WebElement PETSMARTCAASSERT; 
    @FindBy(id="ctl00_lblTopLogin") 
    WebElement PETSMARTPRASSERT; 
    @FindBy(id="ctl00_Menu1_16") 
    WebElement LogoutButton; 


    public static WebDriver driver; 
    public static void main(String[] args) { 
     System.setProperty("webdriver.chrome.driver","C:\\Users\\dmohamed\\Documents\\Testing Environment\\Testing Environment\\Web Drivers\\chromedriver_win32 (1)\\chromedriver.exe"); 
     WebDriver chromedriver = null; new ChromeDriver(); 


     driver= chromedriver; 


    } 


    //Send user name in textbox 
    public void sendUserName(String strUsername){ 
     login.sendKeys("ebluth");} 
    public void sendUserNameServiceCenter(String strUsername){ 
     login.sendKeys("servicecenter");} 
    public void sendUserNameSP(String strUsername){ 
     login.sendKeys("4328701"); 
    } 
    //Send Password 
    public void sendPassword(String strPassword){ 
     password.sendKeys("password");} 




    //submitting credentials 
    public void clicksubmit(){ 
     submit.click();} 
    //Checking US PAge 
    public void USAssertion(){ 
     PETSMARTUS.isEnabled(); 
    } 
    //Checking CAD page 
    public void CAAssertion(){ 
     PETSMARTCAD.isEnabled();} 
    //Checking PR Page 
    public void PRAssertion(){ 
     PETSMARTPR.isEnabled();} 
    //click us link 

    //Checking US PAge 
    public void USclick(){ 
     PETSMARTUS.click(); 
    } 
    //Checking CAD page 
    public void CAclick(){ 
     PETSMARTCAD.click();} 
    //Checking PR Page 
    public void PRclick(){ 
     PETSMARTPR.click();} 

    //Click on 
    public void USPageValidation(){ 
     Assert.assertTrue(PETSMARTUSASSERT.getText().contains("PETM-US"), "Incorrect Page [US,CA,PR]"); 

    } 
    public void PRPageValidation(){ 
     Assert.assertTrue(PETSMARTPRASSERT.getText().contains("PETM-PR"),"Incorrect Page [US,CA,PR]"); 
    } 
    public void CAPageValidation(){ 
     Assert.assertTrue(PETSMARTCAASSERT.getText().contains("PETM-CN"),"Incorrect Page [US,CA,PR]"); 
    } 
    //Log out 

    public void Logout(){ 
     LogoutButton.click(); 



    }} 

Test Class 
package fcstestingsuite.fsnrgn; 
import org.testng.annotations.Test; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.AfterMethod; 
import org.testng.annotations.AfterTest; 
import pageobjectfactory.Ourfsnlogin; 
import org.openqa.selenium.support.PageFactory; 
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory; 

public class LoginTest { 
    static WebDriver driver; 
    Ourfsnlogin LoginPage; 

    @Test (priority=1) 
    public void USPageTest() { 
     LoginPage.sendUserName("ebluth"); 
     LoginPage.sendPassword("password"); 
     LoginPage.clicksubmit(); 
     LoginPage.USclick(); 
     LoginPage.USPageValidation(); 
    } 

    @Test(priority=2) 
    public void CAPageTest(){ 
     LoginPage.sendUserName("ebluth"); 
     LoginPage.sendPassword("password"); 
     LoginPage.clicksubmit(); 
     LoginPage.CAclick(); 
     LoginPage.CAPageValidation(); 
    } 


    @Test (priority=3) 
    public void PRPageTest(){ 
     LoginPage.sendUserName("ebluth"); 
     LoginPage.sendPassword("password"); 
     LoginPage.clicksubmit(); 
     LoginPage.PRclick(); 
     LoginPage.PRPageValidation(); 
    } 


    @AfterMethod 
    public void aftermethod(){ 
     LoginPage.Logout(); 
    } 



    @BeforeTest 
    public void beforeTest() { 
     Ourfsnlogin.driver=new ChromeDriver(); 
     //setting global explicit wait 
     PageFactory.initElements(new AjaxElementLocatorFactory(driver, 20), this); 
     Ourfsnlogin.driver.get("http://www.ourfsn.com/myfsn"); 
     //initiating elements in page factory 
     LoginPage= PageFactory.initElements(Ourfsnlogin.driver, Ourfsnlogin.class); 

    } 

    @AfterTest 
    public void afterTest() { 
     Ourfsnlogin.driver.quit(); 


    } 

} 
+0

因此,也許'webdriver.chrome.driver'屬性是在您的Eclipse環境中設置的,但不是在您的操作系統上,所以命令行不會選擇它。檢查你的Eclipse首選項,我將從Maven運行配置開始。 – Tunaki

+0

你如何運行你的測試?通過XML? –

+0

是的,我正在通過xml運行測試。在做了一些挖掘之後,我發現你要把path.exe文件放到你的環境變量PATH中。所以我這樣做了,不是100%確定這是否做了什麼(做了那個和其他一些行動),但我懷疑它做了。 – Lamar

回答

0

測試類的測試的結構似乎不正確。你在LoginPage中有一個主要的方法 - 正在做驅動實例化。主要方法從哪裏調用?你的beforetest也有一個由testng調用的驅動實例代碼,但是你沒有設置chromedriver屬性。

理想情況下,驅動程序實例化代碼應該寫在一個地方並從其餘的測試用例中被使用。

+0

這是問題所在,我從來沒有在設置系統屬性的方法中調用,一旦我做了我的mvn測試cmd工作正常,並且測試代碼中的驅動程序實例化是多餘的。非常感謝 ! – Lamar