【问题标题】:Getting 'argument type mismatch' error while passing values from a dataprovider class in Selenium从 Selenium 中的数据提供程序类传递值时出现“参数类型不匹配”错误
【发布时间】:2017-01-13 12:53:40
【问题描述】:

我在尝试使用 @dataprovider 类将 Excel 工作表中的几个值传递给页面对象类中的几个方法时遇到“参数类型不匹配”错误。反过来,这些方法在 @test 类中被调用。你能帮我解决这个问题吗?代码已经在下面提到了。

数据提供者

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

页面对象

public class QaToolsECommercePageObjects {
	
	WebDriver driver;
	
	/*Method to launch the browser and select the browser type */
	public void setBrowser(int browser){
		
		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);
	}



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

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

测试类

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

  }

}

错误:

失败:eCommerceProduct(2.0, "Magic Mouse", 1.0, "$150", "$150") java.lang.IllegalArgumentException:参数类型不匹配 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在 java.lang.reflect.Method.invoke(未知来源)

【问题讨论】:

    标签: java selenium selenium-webdriver selenium-ide selenium-chromedriver


    【解决方案1】:

    失败:eCommerceProduct(2.0, "Magic Mouse", 1.0, "$150", "$150") java.lang.IllegalArgumentException:参数类型不匹配

    实际上在eCommerceProduct() 方法中,您期望的参数是intStringintStringString,而实际参数是作为doubleString、@987654329 传递的@、StringString

    因此,您应该将方法 eCommerceProduct() 更改为:-

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

    已编辑:-

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

    发生此错误是因为您在 eCommerceProduct() 方法中调用 QaToolsECommercePageObjects.setBrowser(browser); 并将 browser 值传递给 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);
        }
     -----
     -----
    }
    

    【讨论】:

    • 我尝试了上述解决方案,旧错误已解决。但现在我得到了“失败:eCommerceProduct(2, "Magic Mouse", 1, "$150", "$150") java.lang.NullPointerException'。在excel表中我只有一行值,是因为这个错误吗?请帮助我。
    • 你能分享完整的堆栈跟踪吗??
    • [TestNG] 运行:C:\Users\chetan.k.thimmanna\AppData\Local\Temp\testng-eclipse--1620381105\testng-customsuite.xml 1 失败:eCommerceProduct(2, " Magic Mouse", 1, "$150", "$150") java.lang.NullPointerException 在 qaToolsECommerceExcel.QaToolsECommercePageObjects.setBrowser(QaToolsECommercePageObjects.java:42) 在 qaToolsECommerceExcel.QaToolsECommerceTest.eCommerceProduct(QaToolsECommerceTest.java:13) 在 sun.reflect。 Sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)处的 NativeMethodAccessorImpl.invoke0(本机方法)
    • 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80 ) 在 org.testng.internal.Invoker.invokeTestMethods(Invoker.java: org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 1231)在 org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    • 非常感谢,现在它按预期工作了。感谢帮助:)
    【解决方案2】:

    在您的 dataprovider 函数 productInfoDataprovider() 中,您有

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

    getNumericCellValue() 的返回参数为 double,因此您得到参数类型不匹配。

    通过使用将其类型转换为 int

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

    【讨论】:

    • 我尝试了上述解决方案,旧错误已解决。但现在我得到了“失败:eCommerceProduct(2, "Magic Mouse", 1, "$150", "$150") java.lang.NullPointerException'。在excel表中我只有一行值,是因为这个错误吗?请帮助我。
    • 当您检查与 '1' ,'2', '3' (字符串值)的相等性时,没有任何 'if else if' 条件通过。因此,驱动程序对象未初始化。因此,会为驱动程序对象抛出 NullpointerException。请将值更改为 1,2,3 并添加另一个 else {} 以显示错误消息或打开任何默认 Web 驱动程序。
    • 感谢您的解决方案,现在它按预期工作:)
    【解决方案3】:

    当你启动浏览器时,你写了“else if(browser == '2')”。在这里,您期望 2 作为字符,但您将数据证明为 int/Double。试试

    else if(browser == 2 )
    

    在此之前,将浏览器变量的双精度值转换为整数。

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多