1

當我嘗試使用@dataprovider類將幾個值從Excel表單傳遞給頁面對象中的幾個方法時,我面臨'參數類型不匹配'錯誤。反過來,這些方法在@test類中調用。你能幫我解決這個問題嗎?代碼已在下面提到。在傳遞Selenium中的數據提供者類的值時出現'參數類型不匹配'錯誤

的DataProvider

@DataProvider(name="ProductInfo") 
 
    public static Object[][] productInfoDataprovider() throws Throwable { 
 
    
 
\t File file = new File("C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/QAToolsECommerce/ECommerce_Data.xlsx"); 
 
\t FileInputStream fis = new FileInputStream(file); 
 
\t XSSFWorkbook wb = new XSSFWorkbook(fis); 
 
\t XSSFSheet sheet = wb.getSheet("Sheet1"); 
 
\t int lastRowNum = sheet.getLastRowNum(); 
 
\t Object[][] obj = new Object[lastRowNum][5]; 
 
\t for(int i=0; i<lastRowNum; i++){ 
 
\t \t 
 
\t \t XSSFRow row = sheet.getRow(i+1); 
 
\t \t obj[i][0]= row.getCell(0).getNumericCellValue(); 
 
\t \t obj[i][1]= row.getCell(1).getStringCellValue(); 
 
\t \t obj[i][2]= row.getCell(2).getNumericCellValue(); 
 
\t \t obj[i][3]= row.getCell(3).getStringCellValue(); 
 
\t \t obj[i][4]= row.getCell(4).getStringCellValue(); 
 
\t } 
 
\t \t fis.close(); 
 
\t return obj; 
 
    }

PageObjects

public class QaToolsECommercePageObjects { 
 
\t 
 
\t WebDriver driver; 
 
\t 
 
\t /*Method to launch the browser and select the browser type */ 
 
\t public void setBrowser(int browser){ 
 
\t \t 
 
\t \t if(browser == '1'){ 
 
\t \t \t driver = new FirefoxDriver(); 
 
\t \t }else if(browser == '2'){ 
 
\t \t \t System.setProperty("webdriver.chrome.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/chromedriver.exe"); 
 
\t \t \t driver = new ChromeDriver(); 
 
\t \t }else if(browser == '3'){ 
 
\t \t \t System.setProperty("webdriver.ie.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/IEDriverServer.exe"); 
 
\t \t \t driver = new InternetExplorerDriver(); 
 
\t \t } 
 
\t \t //Maximize the window 
 
\t \t driver.manage().window().maximize(); 
 
\t \t 
 
\t \t driver.get("http://store.demoqa.com/"); 
 
\t \t //driver.get("http://toolsqa.com/"); 
 
\t \t 
 
\t \t //browser load time 
 
\t \t driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
 
\t } 
 

 

 

 
\t 
 
\t /* Searches the product required to purchase */ 
 
\t public void searchProduct(String product){ 
 
\t \t 
 
\t \t driver.findElement(By.name("s")).sendKeys(product); 
 
\t \t driver.findElement(By.name("s")).sendKeys(Keys.ENTER); 
 
\t \t //driver.findElement(By.linkText("Product Category")).click(); \t 
 
\t } 
 
\t 
 

 
\t 
 
\t /* Verifies the product name in the product search result and adds the product to the cart*/ 
 
\t public void productVerify(String product){ 
 
\t \t 
 
\t \t String productValue = driver.findElement(By.id("grid_view_products_page_container")).getText(); 
 
\t \t boolean val = productValue.contains(product); //Value from excel sheet 
 
\t \t if(val == true){ 
 
\t \t \t 
 
\t \t \t System.out.println("The product searched is found :" +productValue); 
 
\t \t \t //Click on add to cart 
 
\t \t \t driver.findElement(By.name("Buy")).click(); 
 
\t \t \t //Click on Go to check out 
 
\t \t \t driver.findElement(By.className("go_to_checkout")).click(); \t 
 
\t \t }else 
 
\t \t { 
 
\t \t \t System.out.println(" The product searched is not found :" +productValue); 
 
\t \t } 
 
\t \t 
 
\t } 
 
\t 
 
\t 
 
\t /* Verifies the product name, quantity, price and total price of the product */ 
 
\t 
 
\t public void checkoutCartVerify(String product, int quantity, String prices, String totalPrices){ 
 
\t \t 
 
\t \t WebElement cartTable = driver.findElement(By.className("checkout_cart")); 
 
\t \t List<WebElement> cartRows = cartTable.findElements(By.tagName("tr")); 
 
\t \t 
 
\t \t //Product name 
 
\t \t WebElement prodRow = cartRows.get(1); 
 
\t \t List<WebElement> prodCols = prodRow.findElements(By.tagName("td")); 
 
\t \t WebElement prodName = prodCols.get(1); 
 
\t \t String oriProdName = prodName.findElement(By.tagName("a")).getText(); 
 
\t \t 
 
\t \t //Comparing product name 
 
\t \t if(oriProdName.equals(product)){ 
 
\t \t \t System.out.println("The Product searched and added to the cart is correct: "+oriProdName); 
 
\t \t }else 
 
\t \t { 
 
\t \t \t System.out.println("The product searched and added to the cart is incorrect: "+oriProdName); 
 
\t \t } 
 
\t \t 
 
\t \t 
 
\t \t //Quantity 
 
\t \t WebElement quantityCombo = prodCols.get(2).findElement(By.tagName("form")); 
 
\t \t List<WebElement> quantityVals = quantityCombo.findElements(By.tagName("input")); 
 
\t \t String prodQuantity = quantityVals.get(0).getAttribute("value"); 
 
\t \t int pq = Integer.parseInt(prodQuantity); 
 
\t \t //Comparing product quantity 
 
\t \t if(pq == quantity){ 
 
\t \t \t System.out.println("The Product quantity added to the cart is correct: "+pq); 
 
\t \t }else 
 
\t \t { 
 
\t \t \t System.out.println("The product quantity added to the cart is incorrect: "+pq); 
 
\t \t } 
 
\t \t 
 
\t \t //Price 
 
\t \t String price = prodCols.get(3).getText(); 
 
\t \t String[] priceSplit = price.split("\\."); 
 
\t \t String prodPrice = priceSplit[0]; 
 
\t \t String priceFrac = priceSplit[1]; 
 
\t \t System.out.println(price); 
 
\t \t 
 
\t \t 
 
\t \t //Comparing price of the quantity 
 
\t \t if(priceFrac.equals("00")){ 
 
\t \t \t if(prodPrice.equals(prices)){ 
 
\t \t \t \t System.out.println("The Product price added to the cart is correct: "+prodPrice); 
 
\t \t \t }else{ 
 
\t \t \t \t System.out.println("The product price added to the cart is incorrect: "+prodPrice); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t }else 
 
\t \t { 
 
\t \t \t if(price.equals(prices)){ 
 
\t \t \t \t System.out.println("The Product price added to the cart is correct: "+price); 
 
\t \t \t }else{ 
 
\t \t \t \t System.out.println("The product price added to the cart is incorrect: "+price); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t //Total Price 
 
\t \t String totalPrice = prodCols.get(4).getText(); 
 
\t \t String[] totalpriceSplit = totalPrice.split("\\."); 
 
\t \t String prodTotalprice = totalpriceSplit[0]; 
 
\t \t String prodpriceFrac = totalpriceSplit[1]; 
 
\t \t System.out.println(totalPrice); 
 
\t \t 
 
\t \t //Comparing Total Price of the quantity 
 
\t \t if(prodpriceFrac.equals("00")){ 
 
\t \t \t if(prodTotalprice.equals(totalPrices)){ 
 
\t \t \t \t System.out.println("The Product Total price added to the cart is correct: "+prodTotalprice); 
 
\t \t \t }else{ 
 
\t \t \t \t System.out.println("The product Total price added to the cart is incorrect: "+prodTotalprice); 
 
\t \t \t } 
 
\t \t }else 
 
\t \t { 
 
\t \t \t if(totalPrice.equals(totalPrices)){ 
 
\t \t \t \t System.out.println("The Product Total price added to the cart is correct: "+totalPrice); 
 
\t \t \t }else{ 
 
\t \t \t \t System.out.println("The product Total price added to the cart is incorrect: "+totalPrice); 
 
\t \t \t } 
 
\t \t } 
 
\t \t 
 
\t }

測試類

public class QaToolsECommerceTest { 
 
    @Test(dataProvider = "ProductInfo", dataProviderClass = QaToolsECommerceDataProvider.class) 
 
    
 
    public void eCommerceProduct(int browser, String product, int quantity, String prices, String totalPrices) { 
 
\t QaToolsECommercePageObjects qaEpo = new QaToolsECommercePageObjects(); 
 
\t 
 
\t qaEpo.setBrowser(browser); 
 
\t qaEpo.searchProduct(product); 
 
\t qaEpo.productVerify(product); 
 
\t qaEpo.checkoutCartVerify(product, quantity, prices, totalPrices); 
 

 
    } 
 

 
}

錯誤:

FAILED: eCommerceProduct(2.0, "Magic Mouse", 1.0, "$150", "$150") java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)

回答

1

FAILED: eCommerceProduct(2.0, "Magic Mouse", 1.0, "$150", "$150") java.lang.IllegalArgumentException: argument type mismatch

其實在方法eCommerceProduct()你期待參數是爲intStringintStringString而實際參數是通過爲doubleStringdoubleStringString

所以,你應該期待參數作爲改變方法eCommerceProduct(): -

public void eCommerceProduct(double browser, String product, double quantity, String prices, String totalPrices) { 
    ------- 
    ------- 
} 

編輯: -

Running: C:\Users\chetan.k.thimmanna\AppData\Local\Temp\testng-eclips‌​e--1620381105\testng‌​-customsuite.xml 1 FAILED: eCommerceProduct(2, "Magic Mouse", 1, "$150", "$150") java.lang.NullPointerException at qaToolsECommerceExcel.QaToolsECommercePageObjects.setBrowser‌​(QaToolsECommercePag‌​eObjects.java:42)

是因爲您傳遞browser調用QaToolsECommercePageObjects.setBrowser(browser);eCommerceProduct()方法出現此錯誤值分爲intdouble,而在QaToolsECommercePageObjects.setBrowser(int browser)方法中您將其作爲比較if(browser == '1')表示字符串中錯誤。

你應該修改QaToolsECommercePageObjects.setBrowser(int browser)方法如下: -

public class QaToolsECommercePageObjects { 

    WebDriver driver; 

    public void setBrowser(int browser){ 
     WebDriver driver 
     if(browser == 1){ 
      driver = new FirefoxDriver(); 
     }else if(browser == 2){ 
      System.setProperty("webdriver.chrome.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/chromedriver.exe"); 
      driver = new ChromeDriver(); 
     }else if(browser == 3){ 
      System.setProperty("webdriver.ie.driver", "C:/Users/chetan.k.thimmanna/Documents/Selenium/Resources/IEDriverServer.exe"); 
      driver = new InternetExplorerDriver(); 
     } 
     //Maximize the window 
     driver.manage().window().maximize(); 

     driver.get("http://store.demoqa.com/"); 
     //driver.get("http://toolsqa.com/"); 

     //browser load time 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 
----- 
----- 
} 
+0

我試過上面的解決方案,舊的錯誤解決了。但現在我得到'失敗:eCommerceProduct(2,「Magic Mouse」,1,「$ 150」,「$ 150」)java.lang.NullPointerException'。在Excel表格中,我只有一行值,是因爲這個錯誤嗎?請幫助我。 –

+0

你能分享完整的stacktrace嗎? –

+0

[TestNG的]運行: C:\用戶\ chetan.k.thimmanna \應用程序數據\本地\ TEMP \ TestNG的-蝕 - 1620381105 \ TestNG的-customsuite.xml FAILED:eCommerceProduct(2, 「魔術鼠標」 ,如圖1所示, 「$ 150」, 「$ 150」) 顯示java.lang.NullPointerException \t在qaToolsECommerceExcel.QaToolsECommercePageObjects.setBrowser(QaToolsECommercePageObjects.java:42) \t在qaToolsECommerceExcel.QaToolsECommerceTest.eCommerceProduct(QaToolsECommerceTest.java:13) \t在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) \t at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) –

1

在您的數據提供程序功能productInfoDataprovider(),你有

obj[i][0]= row.getCell(0).getNumericCellValue(); 
    obj[i][2]= row.getCell(2).getNumericCellValue(); 

getNumericCellValue()已返回參數爲雙因您'獲取參數類型不匹配。

型投用

obj[i][0]= (int)row.getCell(0).getNumericCellValue(); 
    obj[i][2]= (int)row.getCell(2).getNumericCellValue(); 
+0

我試過上面的解決方案,解決了舊的錯誤。但現在我得到'失敗:eCommerceProduct(2,「Magic Mouse」,1,「$ 150」,「$ 150」) java.lang.NullPointerException'。在Excel表格中,我只有一行值,是因爲這個錯誤嗎?請幫助我。 –

+0

由於您檢查與'1','2','3'(字符串值)的相等性,因此您的'if if if'條件無法通過。由於這個原因,驅動程序對象不會被初始化。因此,驅動程序對象被拋出NullpointerException。請將值更改爲1,2,3並添加另一個{}以顯示錯誤消息或打開任何默認的Web驅動程序。 –

+0

謝謝你的解決方案,現在它按預期工作:) –

1

當你啓動瀏覽器,它爲int,你寫 「否則,如果(瀏覽器== '2')」。在這裏,你期望2作爲字符,但你正在證明數據爲int/Double。嘗試

else if(browser == 2) 

在此之前,將瀏覽器變量的double值轉換爲整數。

+0

現在它的工作,謝謝你的解決方案:) –

相關問題